Three ways to avoid null in object oriented languages (Java)


The most common line of code that I’ve seen must be:

It’s purpose is usually to avoid the most common exception that I’ve seen thrown in runtime, NullPointerException.


Null checks are a necessity sometimes but when I see pervasive null checks I consider them being a code smell. I can justify null checks on what I call the “end-points” of software. Let’s say that I am building a simple web-service using some business logic and a database. I find it appropriate to have null checks when receiving external requests to my service and when working with my database. Null checks in the business layer are pointless since that’s already been done.*

* Only applies to code you own. A map that can return null will always need a null check.

Continue reading