Last Revision — April 19, 2022
2 Min Read
---
Obfuscators
Unnecessary Complexity
Within a Class
- Special Case (family)
- Clever Code (co-exist)
- Afraid to Fail (co-exist)
- Mutable Data (co-exist)
- Binary Operator in Name (co-exist)
- Loops (caused)
Marcel Jerzyk in thesis (2022): "Code Smells: A Comprehensive Online Catalog and Taxonomy"
Status Variables are mutable primitives that are initialized before an operation to store some information based on the process and are later used as a switch for some action.
The Status Variables can be identified as a distinct code smell, although they are just a signal for five other code smells:
They come in different types and forms, but common examples are the success: bool = False
-s before performing an operation block or i: int = 0
before a loop statement. The code that has them increases in complexity by a lot, and usually for no particular reason because there most likely exists a proper solution using first-class functions. Sometimes, they clutter the code, demanding other methods or classes to make additional checks before execution resulting in Required Setup/Teardown Code.
The developer might have special cases that could be handled only inside a loop and could not figure out a better solution.
It is more difficult to understand the inner workings of a method than the declarative solution.
def find_foo_index(names: list[str]):
found = False
i = 0
while not found:
if names[i] == 'foo':
found = True
else:
i += 1
return i
Solution, which removes the usage of Status Variables.
def find_foo_index(names: list[str]):
for index, value in enumerate(names):
if value == 'foo':
return index
Solution, which removes the usage of Status Variables and Clever Code.
def find_foo_index(names: list[str]):
return names.index('foo')