Workflow Refactoring

Reduce Cyclomatic Complexity

Workflow for simplifying overly complex functions

refactoringcomplexityreadability
CLAUDE.md

When reducing the complexity of a function:

  1. Measure the current complexity. Count the number of if/else, switch, loop, and try/catch blocks.
  2. Extract guard clauses: move validation and edge-case checks to the top of the function with early returns.
  3. Extract helper functions for self-contained blocks of logic within the function.
  4. Replace complex if/else chains with lookup tables, polymorphism, or strategy patterns.
  5. Break the function into smaller functions that each do one thing.
  6. After each extraction, run the tests to verify behavior is preserved.
  7. 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.

get crystl