Skip to content

skillCode quality

Cyclomatic Complexity Reduction

Flattens deeply nested conditionals with guard clauses, lookup tables and extraction, and proves with tests that the behavior did not change.

npx workspacetemplates add cyclomatic-complexity-reduction --tool claude-code

Writes 1 file to .claude/skills/cyclomatic-complexity-reduction/

.claude/skills/cyclomatic-complexity-reduction/SKILL.md32 lines · 1.8 KBFile
---
name: cyclomatic-complexity-reduction
description: Flattens deeply nested conditionals with guard clauses, lookup tables and extraction, and proves with tests that the behavior did not change.
---

# Cyclomatic complexity reduction

Change the shape, never the behavior. Tests decide whether a step is allowed to stay.

## Steps

1. Find the target: the function the user named, or the worst offenders from the project's linter (`eslint --rule 'complexity: [error, 10]'`, `radon cc -s`, `gocyclo`, or similar). Record the current score.
2. Check for tests that reach every branch. If a branch is untested, write a characterization test for it first and see it pass on the unchanged code.
3. Apply these moves, one per commit-sized step, in this order:
   - Guard clauses: invert the condition and return or throw early, so the happy path sits at the lowest indentation.
   - Remove else after return.
   - Replace nested conditions on the same value with a lookup table or a map of handlers.
   - Name complicated boolean expressions by extracting them into predicates (`isEligibleForRefund`).
   - Extract the body of each remaining branch into a function when it exceeds about 8 lines.
   - Replace a repeated type switch with polymorphism only if it appears in three or more places.
4. Run the tests after each move. If one fails, undo that move and try a smaller one.
5. Recount complexity and confirm it fell.

## Rules

- Keep evaluation order and short-circuit behavior; a reordered condition can change which side effect runs.
- Do not merge branches that look alike but differ in a subtle way; flag them.
- No new dependencies and no renamed public functions.

## Report

Before and after complexity score, the moves applied, test results, and branches you left alone with the reason.

What it does

A function with six levels of if and else is not only ugly, it hides which branch handles which case. This skill reduces nesting step by step, running the tests between steps so a refactoring never turns into a rewrite.

It measures complexity before and after with a tool already in your project when one exists, and with a manual count of branches when not.

Example

you › Reduce the complexity of shippingCost() in src/shipping.js

agent › Complexity 19 down to 6. Added 5 characterization tests first (all passed before changes). Applied 4 guard clauses, replaced the country if-chain with a rates table and extracted isOversized(). Tests green after each step. Left the holiday surcharge branch untouched: two conditions look duplicated but differ on weekends.

Questions

What if the function has no tests?
It writes tests that record today's output first. Without that safety net the skill will not restructure anything.
Is a lower number always better?
No. The goal is code you can read. If a split makes the flow harder to follow, the skill keeps the larger function.
  • API Deprecation Handling

    SkillCode quality

    Free
  • Code Duplication Removal

    SkillCode quality

    Free
  • Code Smell Detection

    SkillCode quality

    Free
  • Concurrency Bug Analysis

    SkillCode quality

    Free