Refactor deeply nested code by applying "Never Nester" principles: early returns, guard clauses, function extraction, and condition inversion. Use this skill whenever the user asks to reduce nesting, flatten code, fix arrow-shaped code, apply guard clauses, use early returns, refactor deeply nested if/else or try/catch blocks, or when the user pastes code that has 3+ levels of indentation and asks to clean it up. Also trigger when users mention the "Never Nester" concept by name, or ask to make code more readable by reducing indentation. Works across all languages.
Eliminate deep nesting to produce flat, readable, linear code. Inspired by CodeAesthetic's "Why You Shouldn't Nest Your Code" principle.
Deeply nested code (the "arrow anti-pattern") is hard to read because:
Invert the condition and return (or throw) immediately. Preconditions are checked up front; the happy path runs at the base indentation level.
Before:
def process(user, data):
if user:
if user.is_active:
return save(data)
After:
def process(user, data):
if not user: return None
if not user.is_active: return None
return save(data)
When the main logic lives in the if-branch and the else is short, flip them.
Before:
if is_valid(x):
# 20 lines of logic
return result