Overview
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.
Causation
The developer might have special cases that could be handled only inside a loop and could not figure out a better solution.
Problems
It is more difficult to understand the inner workings of a method than the declarative solution.
Example
1def find_foo_index(names: list[str]):
2 found = False
3 i = 0
4 while not found:
5 if names[i] == 'foo':
6 found = True
7 else:
8 i += 1
9 return iRefactoring
- Replace with Built-In
- Extract Method
- Remove Status Variables
Sources
- ORIGIN