The Problem With Null Pointer
My first programming class was taught in Java. In the course, I learned that a method should always validate its parameters.
public void method(MyObj a, MyObj b) {
if (a == null || b == null) {
throw new IllegalArgumentException(“Bad arguments!”);
}
// Do real useful stuff with a and b
}I took this for granted ever since then, and found parallels in many other languages. The fact that (almost) everything is an object and any object can be assigned a null pointer seems to be a universal truth in almost all popular languages including Java, Python, Objective C, and Javascript.
That is why I didn’t understand it at first when I read about Tony Hoare’s remark that null pointer is a ‘billion dollar mistake’. For me at the time, it just seemed too trivial. But the more I code, the more I appreciated such smaller details. Now I genuinely believe that you should try to get rid of null pointer as much as possible.
The existence of null pointer encourages paranoid behaviors. An ideal code should be one where no error is possible. For example, if you know the array is always going to have exactly two elements, you should just use a pair structure to hold the elements. As a good programmer, whenever a pointer can potentially be null, you just have to check if it is really null or not.
If a method takes 3 parameters, you would have to check every one of the parameters to make sure it is not null, just as the Java example aboce. The check would have to be replicated in many other methods because even if you made sure the parameters are not null in the first method, you have to check again when you pass them to the second method to really make sure.
I never liked the idea of putting null-checks everywhere in the code. Some people call it “defensive programming” and check even when it intuitively doesn’t make sense. It may prevent some crashes, but it surely demonstrates that you have already lost control of the code. Instead of controlling the data you get from the parameters, you now only passively respond to bad inputs.
// Is this defensive programming or pure desperation?
if (obj != null) {
// do stuff
if (obj != null) {
// do stuff with obj
}
}A non-null pointer can make this program go away. With the compiler’s support provided by the language, you can be more confident even without all the defensive checking. If your language supports that, there is just no reason why you shouldn’t use it.
But what if you are working on a code base that does not have such language support? There are still some options you can choose.
Get Guanqing Yan’s stories in your inbox
Join Medium for free to get updates from this writer.
The first one is to use Optionals to replace nullable pointers. An optional has two states: A ’Some’ state indicates there is a valid value that can be used and a ‘None’ state that indicates there is no valid value. Optional forces you to check for null pointers before the user can access it, therefore it eliminates the paranoia to manually check for every pointer by yourself.
The second option is to rethink default parameters. Conceptually speaking, an object either represents a piece of data or encapsulates a piece of logic. A null pointer thus either indicates ‘no data’ or ‘do nothing’. In both cases, we can potentially use a default constructed object to replace the use of null pointers. We can use an empty data structure to represent that there is no data, and an empty function to represent that we want to do nothing.
For example, if your code takes an optional array as parameter, then your code should behave the same when the array is either null or empty. Since they both represent a ‘negative default’, any deviance from this principle can potentially make the user confused. Whenever you are tempted to pass or receive a null pointer as a parameter, think about whether it is possible to use a dummy object to replace it.
A kitten dies every time a null array and an empty array do not behave the same. — — Me
This is also one of my favorite things about C++. The reliance and enforcement of a default constructed value make programming a lot easier.
// javaMap is a null pointer!
Map<String, Integer> javaMap;// cppMap is a default constructed empty map object!std::map<std::string, int> cppMap;
The third option is to have a coding convention and clear documentation. You can benefit from having a coding convention even if you are just working on your personal project by yourself. A good convention can make the code more consistent, thus minimize the room for error. Some examples are:
- It is always the caller’s responsibility to make sure the parameters are not null.
- No pointer should be null unless clearly documented otherwise.
- Only check for null in the public interface of a class, not in the private helpers.
Controlling the use of null pointers is not a silver bullet. It will neither make your software more performant nor improve your productivity by 10x. But it is a concrete step that you can take to make fewer mistakes and write better code. The truth is, no one really wants to deal with null pointers, and you don’t really have to if you make some effort.








