All skills
Skillintermediate
/simplify-fn - Simplify Complex Function
Refactor a complex function to reduce cyclomatic complexity.
Claude Code Knowledge Pack7/10/2026
Overview
/simplify-fn - Simplify Complex Function
Refactor a complex function to reduce cyclomatic complexity.
Steps
- Read the target function and calculate its current complexity score
- Identify the primary complexity drivers: nested ifs, loops, switch statements
- Map the function's control flow to understand all execution paths
- Apply Extract Method refactoring: pull out logical blocks into named functions
- Replace nested conditionals with guard clauses (early returns)
- Convert switch statements to lookup tables or strategy pattern where appropriate
- Replace complex boolean expressions with named boolean variables
- Decompose loops with multiple responsibilities into separate loops or higher-order functions
- Apply polymorphism to replace type-checking conditionals when applicable
- Verify the refactored code preserves all original behavior by running tests
- Calculate the new complexity score and report the improvement
- Ensure the refactored code is readable and properly named
Rules
- Preserve all existing behavior; this is refactoring, not feature change
- Run tests after each refactoring step to catch regressions immediately
- Target a complexity score of 5 or below for the main function
- Keep extracted functions small (under 15 lines) and single-purpose
- Use descriptive names for extracted functions that explain the "why"
- Do not introduce new dependencies or external libraries
- If tests do not exist, write them before refactoring