Reduce Nesting

Flatten deeply nested conditionals into readable, early-return code. Use on any function with 3+ levels of indentation.

archive228 22ae703 760 B Updated

File contents

Reduce Nesting

Deep nesting hides bugs in the branches you didn't read.

  • Guard clauses — handle the invalid/edge cases first and return early. The happy path drops to the left margin.
  • Invert conditionsif (!valid) return; instead of wrapping the whole body in if (valid) {...}.
  • Extract — a nested block doing one thing becomes a named function.
  • Replace flag-then-branch — return the result directly instead of setting a variable to return later. Target: no function deeper than 2-3 levels. If you still need more, the function is doing too much — split it.

archive228/loopkit/tree/main/skills/reduce-nesting commit 22ae7033d7

Frequently asked questions

npx skillmds@latest add archive228/reduce-nesting