Nested If
ID |
python.nested_if |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Complexity |
Language |
Python |
Tags |
complexity |
Description
Reports an if statement whose nesting depth exceeds a configurable threshold (default 4). Deep nesting hides the actual control flow under indentation; the same logic almost always reads better as a chain of guard clauses or a dispatch dict.
def classify(x):
if x > 0:
if x > 10:
if x > 100:
if x > 1000:
if x > 10000: # FLAW (depth 5 with limit 4)
return "huge"
elif chains do not deepen the nest — they are sequential branches of the surrounding if.
Rationale
Past depth 4, readers cannot keep the entry conditions in working memory and the function becomes a bug magnet. A guard-clause refactor or a dispatch dict almost always restores readability.