Don't return null from Boolean functions


Link to this posting

Postby Ursego » 21 Feb 2013, 22:12

Boolean methods must always return true or false (but never null).

It's absolutely not guaranteed that the calling method (which must make a code branching decision) will check the returned result for null. In fact, usually there is no such check, especially when the Boolean method is directly placed inside an if (like if this.booleanMethod()...).

When a Boolean function, called in an if...else construction, returns null, the program flow silently goes to the else section and executes the logic, supposed to be run for false. That can produce a hidden bug.

Did you often see code like the following fragment?

Code: Select all
boolean passengerMustBeCheckedBySecurity;
boolean allowToBoard;

passengerMustBeCheckedBySecurity = securityController.passengerMustBeCheckedBySecurity();

if (passengerMustBeCheckedBySecurity = null) {
   // What to do??? He can be a terrorost!
}

if passengerMustBeCheckedBySecurity {
   allowToBoard = this.passengerPassedSecurityCheck();
} else {
   allowToBoard = true;
}

Probably, you didn't encounter such defensive code often - everybody call the Boolean function directly in the if, with no interim variable. And that is... very good! With the interim variable (and checking it for null), code elegancy would be very questionable. But what should we do if a Boolean method cannot answer the asked question?

On technical failure, Boolean methods should throw exceptions rather than return null.

But what to do if exceptions don't exist in the programming language (like T-SQL)? First of all, display an error message to prevent a hidden bug. But, in this case, our method still needs to return something. As well as in a situation when it's not a technical failure (for example, we don't have the information, required to make the decision, since the checked value in a DB table is null). So, what to return if we don't want to return null? On a non-technical failure to answer the question, follow this advice:

Think defensively. Use common sense to prevent the bad.

If the method checks an eligibility, return a value, saying "the entity is not eligible for what you asked". For example, if the method customerIsEligibleForDiscount() failed to connect to the database, it should return false (to prevent money loss by the company). But the method passengerMustBeCheckedBySecurity() should return true (to prevent a terrorist from boarding a plane).

If it is anticipated that the Boolean method will not be able to answer the question, then report success/failure using an additional output (by-reference) argument. The calling method will be forced to pass a variable, so the chances are small that the success/failure code, returned via that argument, will be forgotten to be analyzed.

From the book "PL/SQL Best Practices":

Never return NULL from Boolean functions.

A Boolean function should return only TRUE or FALSE. If a Boolean function returns a NULL, how should the user of that function interpret and respond to that value? Does it indicate you passed in invalid data? Should it be considered TRUE or FALSE? Or should the developer test explicitly for NULL? Well, we should do explicit tests for NULL if we are uncertain about the function's behavior, but we rarely remember to do so or feel it's necessary to make the effort. Instead, we check for TRUE or FALSE and thus allow bugs to creep into our code.

If the Boolean function can return NULL, you probably need to look at the implementation of the function to determine the action to take on a NULL return value. Yet you will not always be able to (or want to) look at the function's body.
User avatar
Ursego
Site Admin
 
Posts: 128
Joined: 19 Feb 2013, 20:33



IF you want to ((lose weight) OR (have unbelievable (brain function AND mental clarity))) THEN click:




cron
free counters

eXTReMe Tracker