Reduce Cyclomatic Complexity
Workflow for simplifying overly complex functions
CLAUDE.md
When reducing the complexity of a function:
- Measure the current complexity. Count the number of if/else, switch, loop, and try/catch blocks.
- Extract guard clauses: move validation and edge-case checks to the top of the function with early returns.
- Extract helper functions for self-contained blocks of logic within the function.
- Replace complex if/else chains with lookup tables, polymorphism, or strategy patterns.
- Break the function into smaller functions that each do one thing.
- After each extraction, run the tests to verify behavior is preserved.
- Target: no function should have more than 3-4 levels of nesting or more than 20 lines.
Copy this workflow into your CLAUDE.md or agent config file so your agent follows this process automatically.