A code that either returns or no returns at all
TL;DR: Check carefully your boolean expressions
-
Readability
-
Possible Defects
- Refactor and remove obsolete code
When a function is designed to return an invariant value, it may be poor design, but it shouldn’t adversely affect the outcome of your program. However, when it happens on all paths through the logic, it is likely a mistake.
This rule raises an issue when a function contains several return statements that all return the same value.
# Gratuitous boolean expressions
if a > 0 and True:
# This code was left on debugging and passed
# by mistake during code reviews
print("a is positive")
else:
print("a is not positive")
if a > 0:
print("a is positive")
else:
print("a is not positive")
[X] Automatic
Many linters can detect this problem by parsing execution trees.
- Complexity
Boolean expressions should be straightforward to read and understand.
Code Smell 101 - Comparison Against Booleans
How to Get Rid of Annoying IFs Forever
Code Smells are just my opinion.
Photo by Jungwoo Hong on Unsplash
The central enemy of reliability is complexity.
Daniel Geer
Software Engineering Great Quotes
This article is part of the CodeSmell Series.