Code Smell 07 – Avoid Boolean Variables

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 ] IntermediateWhy 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
\