Question about a simple design problem

Posted by Uri on Programmers See other posts from Programmers or by Uri
Published on 2012-06-01T13:39:51Z Indexed on 2012/06/01 16:50 UTC
Read the original article Hit count: 249

Filed under:
|

At work I stumbled uppon a method. It made a query, and returned a String based on the result of the query, such as de ID of a customer. If the query didn't return a single customer, it'd return a null. Otherwise, it'd return a String with the ID's of them. It looked like this:

String error = getOwners();
if (error != null) {
    throw new Exception("Can't delete, the flat is owned by: " + error);
}
...

Ignoring the fact that getCustomers() returns a null when it should instead return an empty String, two things are happening here. It checks if the flat is owned by someone, and then returns them.
I think a more readable logic would be to do this:

if (isOwned) {
    throw new Exception("Can't delete, the flat is owned by: " + getOwners());
}
...

The problem is that the first way does with one query what I do with two queries to the database.
What would be a good solution involving good design and efficiency for this?

© Programmers or respective owner

Related posts about java

Related posts about design