Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Code Smell 12 — Null

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

Photo by Kurt Cotoaga on Unsplash

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

Right

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

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

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com

No responses yet

Write a response