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

Code Smell 12 - Null is Schizophrenic and Does Not Exist in The Real-world

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

Problems 😔

  • Coupling between callers and 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 😃

  1. Avoid Null.
  2. Use the NullObject pattern to avoid ifs.
  3. Use Optionals.

https://hackernoon.com/null-the-billion-dollar-mistake-8t5z32d6?embedable=true

Refactorings ⚙️

https://hackernoon.com/code-refactoring-tips-no-015-remove-null?embedable=true

https://hackernoon.com/refactoring-029-how-to-replace-null-with-collection?embedable=true

https://hackernoon.com/refactoring-014-how-to-remove-if?embedable=true

Context 💬

When you use null, you encode multiple meanings into a single value.

Sometimes you want to represent an absence.

Sometimes you mean you have not loaded your objects yet.

Sometimes you mean error.

Callers must guess your intent and add conditionals to protect themselves.

You spread knowledge about internal states across your codebase.

Sample Code 📖

Wrong 🚫

class CartItem { constructor(price) { this.price = price; } } class DiscountCoupon { constructor(rate) { this.rate = rate; } } class Cart { constructor(selecteditems, discountCoupon) { this.items = selecteditems; this.discountCoupon = discountCoupon; } subtotal() { return this.items.reduce((previous, current) => previous + current.price, 0); } total() { if (this.discountCoupon == null) return this.subtotal(); else return this.subtotal() * (1 - this.discountCoupon.rate); } } cart = new Cart([ new CartItem(1), new CartItem(2), new CartItem(7) ], new DiscountCoupon(0.15)]); // 10 - 1.5 = 8.5 cart = new Cart([ new CartItem(1), new CartItem(2), new CartItem(7) ], null); // 10 - null = 10

class CartItem { constructor(price) { this.price = price; } } class DiscountCoupon { constructor(rate) { this.rate = rate; } discount(subtotal) { return subtotal * (1 - this.rate); } } class NullCoupon { discount(subtotal) { return subtotal; } } class Cart { constructor(selecteditems, discountCoupon) { this.items = selecteditems; this.discountCoupon = discountCoupon; } subtotal() { return this.items.reduce( (previous, current) => previous + current.price, 0); } total() { return this.discountCoupon.discount(this.subtotal()); } } cart = new Cart([ new CartItem(1), new CartItem(2), new CartItem(7) ], new DiscountCoupon(0.15)); // 10 - 1.5 = 8.5 cart = new Cart([ new CartItem(1), new CartItem(2), new CartItem(7) ], new NullCoupon()); // 10 - nullObject = 10

Detection 🔍

Most Linters can flag null usages and warn you.

Exceptions 🛑

You sometimes need to deal with null when you integrate with databases, legacy APIs, or external protocols.

You must contain null at the boundaries and convert it immediately into meaningful objects.

Tags 🏷️

  • Null

Level 🔋

[x] Intermediate

Why the Bijection Is Important 🗺️

When you use null, you break the bijection between your code and the MAPPER.

Nothing in the mapper behaves like null.

Absence, emptiness, and failure mean different things.

When you collapse them into null, you force your program to guess reality and you invite defects.

AI Generation 🤖

AI generators often introduce this smell.

They default to null when they lack context or want to keep examples short and also because it is widespread (but harmful) industry default.

AI Detection 🧲

You can instruct AI to remove nulls with simple rules.

When you ask for explicit domain objects and forbid nullable returns, generators usually fix the smell correctly.

Try Them! 🛠

Remember: AI Assistants make lots of mistakes

Without Proper Instructions 📵

  • ChatGPT
  • Claude
  • Perplexity
  • Copilot
  • You
  • Gemini
  • DeepSeek
  • Meta AI
  • Grok
  • Qwen

With Specific Instructions 👩‍🏫

  • ChatGPT
  • Claude
  • Perplexity
  • Copilot
  • You
  • Gemini
  • DeepSeek
  • Meta AI
  • Grok
  • Qwen

Conclusion 🏁

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

Relations 👩‍❤️‍💋‍👨

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xviii

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxii

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xix

https://hackernoon.com/how-to-get-rid-of-annoying-ifs-forever-zuh3zlo

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-viii-8mn3352

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxx

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xliii

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxix

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxvi

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xlii

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxii

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xx-we-have-reached-100

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-ix-7rr33ol

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-v-evj3zs9

More Information 📕

https://hackernoon.com/null-the-billion-dollar-mistake-8t5z32d6?embedable=true

Credits 🙏

Photo by Kurt Cotoaga on Unsplash


Tony Hoare

https://hackernoon.com/400-thought-provoking-software-engineering-quotes?embedable=true


This article is part of the CodeSmell Series.

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-i-xqz3evd?embedable=true

\

Market Opportunity
Notcoin Logo
Notcoin Price(NOT)
$0.0005969
$0.0005969$0.0005969
+0.40%
USD
Notcoin (NOT) Live Price Chart
Disclaimer: The articles reposted on this site are sourced from public platforms and are provided for informational purposes only. They do not necessarily reflect the views of MEXC. All rights remain with the original authors. If you believe any content infringes on third-party rights, please contact service@support.mexc.com for removal. MEXC makes no guarantees regarding the accuracy, completeness, or timeliness of the content and is not responsible for any actions taken based on the information provided. The content does not constitute financial, legal, or other professional advice, nor should it be considered a recommendation or endorsement by MEXC.

You May Also Like

Ethereum unveils roadmap focusing on scaling, interoperability, and security at Japan Dev Conference

Ethereum unveils roadmap focusing on scaling, interoperability, and security at Japan Dev Conference

The post Ethereum unveils roadmap focusing on scaling, interoperability, and security at Japan Dev Conference appeared on BitcoinEthereumNews.com. Key Takeaways Ethereum’s new roadmap was presented by Vitalik Buterin at the Japan Dev Conference. Short-term priorities include Layer 1 scaling and raising gas limits to enhance transaction throughput. Vitalik Buterin presented Ethereum’s development roadmap at the Japan Dev Conference today, outlining the blockchain platform’s priorities across multiple timeframes. The short-term goals focus on scaling solutions and increasing Layer 1 gas limits to improve transaction capacity. Mid-term objectives target enhanced cross-Layer 2 interoperability and faster network responsiveness to create a more seamless user experience across different scaling solutions. The long-term vision emphasizes building a secure, simple, quantum-resistant, and formally verified minimalist Ethereum network. This approach aims to future-proof the platform against emerging technological threats while maintaining its core functionality. The roadmap presentation comes as Ethereum continues to compete with other blockchain platforms for market share in the smart contract and decentralized application space. Source: https://cryptobriefing.com/ethereum-roadmap-scaling-interoperability-security-japan/
Share
BitcoinEthereumNews2025/09/18 00:25
Microsoft Corp. $MSFT blue box area offers a buying opportunity

Microsoft Corp. $MSFT blue box area offers a buying opportunity

The post Microsoft Corp. $MSFT blue box area offers a buying opportunity appeared on BitcoinEthereumNews.com. In today’s article, we’ll examine the recent performance of Microsoft Corp. ($MSFT) through the lens of Elliott Wave Theory. We’ll review how the rally from the April 07, 2025 low unfolded as a 5-wave impulse followed by a 3-swing correction (ABC) and discuss our forecast for the next move. Let’s dive into the structure and expectations for this stock. Five wave impulse structure + ABC + WXY correction $MSFT 8H Elliott Wave chart 9.04.2025 In the 8-hour Elliott Wave count from Sep 04, 2025, we saw that $MSFT completed a 5-wave impulsive cycle at red III. As expected, this initial wave prompted a pullback. We anticipated this pullback to unfold in 3 swings and find buyers in the equal legs area between $497.02 and $471.06 This setup aligns with a typical Elliott Wave correction pattern (ABC), in which the market pauses briefly before resuming its primary trend. $MSFT 8H Elliott Wave chart 7.14.2025 The update, 10 days later, shows the stock finding support from the equal legs area as predicted allowing traders to get risk free. The stock is expected to bounce towards 525 – 532 before deciding if the bounce is a connector or the next leg higher. A break into new ATHs will confirm the latter and can see it trade higher towards 570 – 593 area. Until then, traders should get risk free and protect their capital in case of a WXY double correction. Conclusion In conclusion, our Elliott Wave analysis of Microsoft Corp. ($MSFT) suggested that it remains supported against April 07, 2025 lows and bounce from the blue box area. In the meantime, keep an eye out for any corrective pullbacks that may offer entry opportunities. By applying Elliott Wave Theory, traders can better anticipate the structure of upcoming moves and enhance risk management in volatile markets. Source: https://www.fxstreet.com/news/microsoft-corp-msft-blue-box-area-offers-a-buying-opportunity-202509171323
Share
BitcoinEthereumNews2025/09/18 03:50
Gold continues to hit new highs. How to invest in gold in the crypto market?

Gold continues to hit new highs. How to invest in gold in the crypto market?

As Bitcoin encounters a "value winter", real-world gold is recasting the iron curtain of value on the blockchain.
Share
PANews2025/04/14 17:12