Code Smell 07 - Boolean Variables
Using boolean variables as flags, exposes accidental implementation and pollutes the code with Ifs.
Comparison in some languages
- If Boolean maps to a real world entity is safe. Otherwise model as a State to favor Extensibility. This also follows Open/Closed Principle.
- Real world true/false rules
Automatic detection can warn for boolean usage but this can yield false positives.
Some languages have issues with boolean comparators.
In these coupled with accidental complexity languages, booleans are a common error source.
Also Known as
- Flag Abuser
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.
These tweets inspired this code smell:
This article is part of the CodeSmell Series.