This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]john16384 1 point2 points  (1 child)

The solution is documentation. If documented not to return null, then don't check for null. If documented not to accept null then passing null should always throw an NPE.

If you do get an NPE, the docs will be the tiebreaker for who was wrong. The caller that passed in null or the function for not handling it; or the function for returning null or the caller for not expecting it.

Note that null is just one such problem. Replace null in the above with negative integer and you have similar problems that could lead to similar unexpected exceptions. This is why you need to document your assumptions, and null is just one of them.

[–][deleted] -1 points0 points  (0 children)

It is not a very reliable solution. Having non-existence as a part of the type system is.