Eliminating NullPointerExceptions.
Every reference in Java is either null, or valid. In the development stage of many applications, and even during production phase, we can still see a lot of null pointer exceptions being thrown. This is usually due to failure to check a return value from a function call.
The null result is often returned from a function to indicate "I couldn't", or "that object was not available". It is very common to want to return this result, and exceptions shouldn't be used to signify it. We don't want to incur the overhead of generating an exception.
It would be interesting to be able to mark a method as "returns null", in the same manner that we can throw an exception. The compiler can then check to see if the value returned is checked for null, before being used. If the value is returned from the function, that function must be marked the same way, forcing callers of it to check, or declare to throw.
Yes, we can always put the null value into a class, and there are more convolutions of this principle that could get at that...
It is an interesting idea, nonetheless. Extending it further, we can note that we might declare multiple return paths for any function, and the calling of that function becomes like a switch statement, where the type of the result directs the path of execution, much like a type-inferring language like Haskell or ML.
And maybe that is what I really want. For now, though -- enforced null checks!
4:01:23 PM
|