Question about a simple design problem
- by Uri
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?