The BIllion Dollar Mistake | NPE

TL;DR: null pointers will ruin your life.

If you've used Java on a large scale project, you're lying if you haven't faced this dreaded million dollar mistake, first introduced to the world of programming in 1965. It's uglier than a Windows backslash, odder than a ===, far more common than the latest release of a JS library, more confusing than understanding a GC, ever so slightly harder to debug than interger overflows - I'm talking about a programmer's guilty temptation, the horrid null pointer exception.

Homer, and computer programmers all around the globe.

Tony Hoare, the creator of null very casually says "I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years."

Very true, Mr. Hoare. Last week, I (finally) re-released Lifehacks on the playstore - having forgotten to perform one vital test. That very silly mistake caused over 11,000 crash reports.


The first four rows - they're all a part of the utility - a function returned a null value, and I never checked for it. PS: Ignore the number of chrome tabs/windows open.
It's a terrible mistake, and I've received hundreds of complaints because of this tiny error. Note to self: should've added units tests. But I didn't have the time, really.

So here's how to not use null in the future.

1. Never return null from a method. Just never do that. Instead, return an empty String, Collection, or whatever your method is returning. Just never a return null;
2. Use the @NotNull,@Nullable annotations.
3. If you're on Java 1.8+, use optionals. Read more in this article.

-
Reference : The initial half of this post is taken (with modifications) from this article.

Comments

Popular posts from this blog

Finding My Parter

Two Worlds, Two Lives

Keeping Fit in India