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.
- 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:
Deni Moka⚡@dmokafaAvoid passing boolean as a function parameter!
It makes your code harder to read. It is also a sign that your function has multiple responsibilities, so you violate the Single Responsibility Principle.
Keep in mind that boolean as a parameter is a code smell!12:00 PM - 22 Oct 2020