Code Smell 12 - Null

Subscribe to my newsletter and never miss my upcoming articles

Programmers use Null as different flags. It can hint an absence, an undefined value, en error etc. Multiple semantics lead to coupling and errors.

Problems

  • Coupling among the callers and the senders.

  • Mismatch among the callers and the senders.

  • If/Switch/Case Polluting.

  • Null is not polymorphic with real objects. Hence Null Pointer Exception

  • Null does not exist on real world. Thus it violates Bijection Principle

Solutions

Exceptions

  • APIs, Databases and external systems where NULL does exist.

Sample Code

Wrong

Detection

Most Linters can show null usages and warn us.

Tags

  • Null

Conclusion

  • Null is the billion dollar mistake. Yet, most program languages support them and libraries suggest its usage.

More info

Credits

Photo by Kurt Cotoaga on Unsplash


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.

Tony Hoare


This article is part of the CodeSmell Series.

Apoorv Tyagi's photo

I always felt that there is a huge gap when it comes to articles on software engineering in general

With you, Matt Moll & Mario Cervera coming i am glad those gaps now has been filled :)

Mario Cervera's photo

Glad to hear this Apoorv! You can definitely expect more articles on Software Engineering coming from me 😉

Maxi Contieri's photo

Thank you Apoorv Tyagi!

More articles are coming!

Syed Fazle Rahman's photo

Wow. This is a great series, mcsee. These will definitely help devs become 10x better. 🍻 Keep them coming.