Status Variable — Code Smells Catalog Skip to content

Status Variable

Obfuscators Unnecessary Complexity Code Smell Within Class

found = False. Then a loop. Then found = True somewhere inside. Then a check after. Mutable flags that complicate control flow when a direct return or a built-in would express the same logic in a...

2 min read 1 source

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

📖
Comprehensibility

It is more difficult to understand the inner workings of a method than the declarative solution.

Example

Solution, which removes the usage of Status Variables.
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 i
PYTHON

Refactoring

  • Replace with Built-In
  • Extract Method
  • Remove Status Variables

Sources

Browse All 56 Smells