Arrow Code / Deep Nesting
Excessive indentation levels.
“Flattening Arrow Code.” – Jeff Atwood, Coding Horror
Code looks like an arrow pointing right: >>>>>>.
Signs of Use (Symptoms)
- 4+ levels of indentation.
- Code “slides” across the screen.
Why it is bad (Consequences)
- Readability: Hard to track which
}belongs to which{. - Complexity: Usually indicates too many conditions.
Why it happens (Root Cause)
Nested if statements are the natural way to express conditions.
When it might be okay (Exceptions)
- Complex, unavoidable state machines (but consider refactoring).
Explanation
Problem
The Flaw
Cognitive load is high. Each indent is another thing to remember.
Real world analogy
Russian nesting dolls. Opening one reveals another, and another…
Refactoring Solution
- Guard Clauses: Invert conditions and return early.
- Extract Method: Break into smaller functions.
Pros and Cons (of the Antipattern)
| Pros (Why people do it) | Cons (The price you pay) |
|---|---|
Comparison
- Related Antipatterns: Long Method.
- Related Principles: KISS.
Code example
Typescript
Bad (The Antipattern)
Good (The Fix)