Simplifying conditional logic
For this section, we will also rely on Martin Fowler and try to explain some of the refactorings to what we consider to be the most common problems. The selection is arbitrary and based solely on our experience. For further details, we refer you to the Further reading section.
Returning a special case instead of null
Do not return null
. This is a mantra that everyone – even engineers with years of experience – sometimes forgets. There are cases where a method should return a result but cannot: some error in the execution flow; some exceptional cases. Java and many other languages allow returning null
, but it’s preferable not to do so for obvious reasons – among them, avoiding a NullPointerException
in the caller or forcing it to check every time that the method’s result is not null
.
Tip
Tony Hoare introduced Null references in ALGOL W back in 1965 “simply because it was so easy to implement,”...