cover-img

Code Smell 12 - Null

Null is schizophrenic and does not exist in real world. His creator regretted and programmers around the world suffer it. Don't be a part of it.

10 October, 2022

4

4

0

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

TL;DR: Null is schizophrenic and does not exist in real world. His creator regretted and programmers around the world suffer it. Don't be a part of it.

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

Avoid Null. 

Use NullObject pattern to avoid ifs.

Sample Code

Wrong

Right

Detection

Most Linters can show null usages and warn us.

Exceptions

APIs, Databases and external systems where NULL does exist.

Tags

Null

Conclusion

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

More info

Credits

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.

cleancode

null

nullsafety

codesmell

4

4

0

cleancode

null

nullsafety

codesmell

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.