By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: Code Smell 07 – Avoid Boolean Variables | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > Code Smell 07 – Avoid Boolean Variables | HackerNoon
Computing

Code Smell 07 – Avoid Boolean Variables | HackerNoon

News Room
Last updated: 2025/10/17 at 9:12 AM
News Room Published 17 October 2025
Share
Code Smell 07 – Avoid Boolean Variables | HackerNoon
SHARE

Using Boolean variables as flags introduces accidental implementation complexity and pollutes the code with Ifs.

TL;DR: avoid Boolean variables, they lead to conditional logic and force you to write Ifs. Create polymorphic states instead.

Problems 😔

  • Extensibility
  • Comparison in some languages
  • Missed polymorphism
  • Limited semantics
  • Primitive obsession

Solutions 😃

  • If Boolean maps to a real-world entity is safe.
  • Model Booleans as a State to favor Extensibility following the Open/Closed Principle.
  • Create intention-revealing objects
  • Replace Booleans with polymorphism

Refactorings ⚙️

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

Examples 📚

  • Flags
  • Status indicators

Context 💬

Boolean variables tempt you to oversimplify complex domains.

When you use a Boolean, you force two states where many might exist.

This creates coupling because you hardcode behavior based on true/false checks scattered throughout your code.

Booleans also hide intent. A variable named flag tells you nothing about what it represents in the real world.

Even descriptive names like isActive or hasPermission leak implementation details instead of revealing domain concepts.

The problems multiply when you need a third state. You cannot extend a Boolean without breaking existing code.

You end up with multiple Boolean combinations that create implicit states, making your code fragile and hard to understand.

Real-world entities rarely have just two states.

A traffic light isn’t “on” or “off”—it’s red, yellow, or green. An order isn’t just “paid” or “unpaid”—it might be pending, processed, shipped, or delivered.

Sample Code 📖

Wrong 🚫

<?

function processBatch(
    bool $useLogin,
    bool $deleteEntries,
    bool $beforeToday) {
    // ...
}

Right 👉

<?

function processBatch(
    LoginStrategy $login,
    DeletionPolicy $deletionPolicy,
    Date $cutoffDate) {
    // ...
}

Detection 🔍

  • Manual

Automatic detection can warn for B usage, but this can yield false positives.

You can search for boolean attributes in classes, Boolean parameters in methods, and conditional statements that check Boolean values.

Look for variables with “is”, “has”, “can”, or “flag” in their names.

Watch for multiple Boolean combinations that represent different states.

Exceptions 🛑

  • Real-world true/false rules

  • Some languages have issues with Boolean comparators.

In these coupled with accidental complexity languages, Booleans are a common error source.

Tags 🏷️

  • IFs

Level 🔋

[x ] Intermediate

Why the Bijection Is Important 🗺️

Your code should always mirror reality.

When you use a Boolean to represent an employee’s status, you break the bijection between your software and the MAPPER.

Real employees don’t have a true/false state. They have concrete statuses like “on vacation” or “working remotely.”

This broken mapping creates a gap between how domain experts think and how your code works. Business people talk about “vacation status” or “remote work,” not about “isonvacation equals true.”

When you model with Booleans, you force translation between business language and code, increasing cognitive load and error potential.

The bijection violation compounds when requirements change. Adding a new employee status means modifying all the boolean logic scattered across your codebase.

With proper domain modeling, you simply add a new status class. The broken mapping makes your code rigid where reality is flexible.

AI Generation 🤖

AI code generators frequently create Boolean flags because they optimize for brevity and simplicity.

They often suggest isactive or haspermission flags when generating boilerplate code.

You need to explicitly request domain modeling to avoid this pattern.

AI Detection 🧲

AI tools can detect Boolean variables with medium accuracy when you provide context.

You need to specify that you want domain-driven design and state pattern usage.

Simple prompts might not catch the smell without explicit instructions to avoid Booleans.

Try Them! 🛠

Remember: AI Assistants make lots of mistakes

Suggested Prompt: correct=Replace Boolean variables with polymorphism

| Without Proper Instructions | With Specific Instructions |
|—-|—-|
| ChatGPT | ChatGPT |
| Claude | Claude |
| Perplexity | Perplexity |
| Copilot | Copilot |
| You | You |
| Gemini | Gemini |
| DeepSeek | DeepSeek |
| Meta AI | Meta AI |
| Grok | Grok |
| Qwen | Qwen |

Conclusion 🏁

Take extra care when declaring something Boolean. Flags are difficult to maintain and extend.

Learn more about the domain. Try migrating to state design pattern. Use polymorphism instead of ifs/switch/cases.

Relations 👩‍❤️‍💋‍👨

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

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

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

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

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

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

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

https://hackernoon.com/code-smell-270-boolean-apis?embedable=true

More Information 📕

https://martinfowler.com/bliki/FlagArgument.html?embedable=true

Also Known as 🪪

  • Flag Abuser

Credits 🙏

Photo by Phil Hearing on Unsplash


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

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Toonstar’s new ‘Uncle Roger’ cartoon embraces AI—but slop it’s not Toonstar’s new ‘Uncle Roger’ cartoon embraces AI—but slop it’s not
Next Article Easily expand your Xbox Series X storage on the cheap Easily expand your Xbox Series X storage on the cheap
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

Cyber Monday Apple deal still live: At ,349, the 2025 M5 MacBook Pro is cheaper than the 2024 M4 version
Cyber Monday Apple deal still live: At $1,349, the 2025 M5 MacBook Pro is cheaper than the 2024 M4 version
News
Your Apple TV Is Probably Plugged Into The Wrong HDMI Port – Here’s Why – BGR
Your Apple TV Is Probably Plugged Into The Wrong HDMI Port – Here’s Why – BGR
News
The 15 best tech gifts in the US for moms, as requested by moms
The 15 best tech gifts in the US for moms, as requested by moms
News
Google Photos 2025 Recap is here with the ability to hide unwanted faces
Google Photos 2025 Recap is here with the ability to hide unwanted faces
News

You Might also Like

ByteDance’s first Doubao-assisted AI phone sells out at 5 as second-hand prices rise by at least 0 · TechNode
Computing

ByteDance’s first Doubao-assisted AI phone sells out at $495 as second-hand prices rise by at least $210 · TechNode

1 Min Read
Intel LASS, SGX EUPDATESVN & Microcode Staging Features Land In Linux 6.19
Computing

Intel LASS, SGX EUPDATESVN & Microcode Staging Features Land In Linux 6.19

3 Min Read
Iron Vision HUD: My Take on Tony Stark’s Iconic Helmet Interface | HackerNoon
Computing

Iron Vision HUD: My Take on Tony Stark’s Iconic Helmet Interface | HackerNoon

0 Min Read
The hot new thing at AWS re:Invent has nothing to do with AI
Computing

The hot new thing at AWS re:Invent has nothing to do with AI

4 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?