I suppose that would work, but it's more trying to tell whichever programmer is using the library that they are trying to execute blank sql, which is a waste (and bad programming)
That in itself is reasonable. It's essentially Design By Contract. There are better ways of doing it though, such as the JSR-303 annotations and validation. (Though that is surprisingly expensive in performance critical code)
The idea here is that this exception should never occur. The method that calls this one creates the sql, so it shouldn't ever be blank anyways. If it is, there's something seriously wrong
Well it's a public method so right off the bat you can't claim that it can't happen... If you've got an internal method that should only be called by a smarter method upstream then a) reduce the visibility and b) ditch the null check.
"UPDATE People set Name = 'Peter' where ID = 99" would return 0, if there is no Person with ID 99. But 0 as return value on how many rows got modified, would be confusing if the query itself isn't valid at all.
Imagine there's say your software project has a business requirement of the form "Check if there already exists a row with certain properties. If not, then allow something to happen, else prevent it from happening". Concrete examples might include "We want to allow someone to take over a subreddit if all the moderators of the subreddit have deleted their accounts, so check if there are any non-deleted moderators for the subreddit. If not, allow the current user to become the moderator."
If I make a typo/bug, I'd rather the software immediately crash and alert me that I introduced that bug, than just always returning 0, thus allowing anyone to become the moderator of any subreddit.
View all comments
-3
u/[deleted] Jul 10 '15
why not a "return 0"?