cyclomatic-complexity

Measures and reduces cyclomatic complexity in functions through structured refactoring.

Updated Nov 1, 2024
One-click install
npx skills add https://github.com/mlorentedev/dotfiles --skill cyclomatic-complexity-mlorentedev
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cyclomatic-complexity
Source: https://github.com/mlorentedev/dotfiles/tree/main/harness/skills/cyclomatic-complexity
Command: npx skills add https://github.com/mlorentedev/dotfiles --skill cyclomatic-complexity-mlorentedev

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Deeply nested logic, god functions, and heavily branched code are hard to test, review, and maintain. This Skill measures cyclomatic complexity, ranks hotspots, and applies a disciplined refactoring sequence to bring functions back under maintainable thresholds without changing behavior. ## Core Features & Use Cases - Complexity Measurement: Uses deterministic AST tools like radon, ruff, gocyclo, golangci-lint, ESLint complexity rule, or lizard, with manual decision-point counting as fallback. - Ordered Refactoring Hierarchy: Applies guard clauses, function extraction, lookup tables, named predicates, polymorphism, and loop flattening in a strict preference sequence. - Behavior Preservation: Requires running tests before and after refactoring, protects public API signatures, and forbids gaming the metric with dense one-liners. - Use Case: Before submitting a PR with a 200-line function full of nested if/else chains, use this Skill to get a baseline CC report, refactor the worst functions, and finish with a before/after complexity table proving zero regressions. ## Quick Start Ask the AI to measure the cyclomatic complexity of the functions in your changed files and refactor the highest-complexity ones using guard clauses and function extraction.

Frequently Asked Questions about cyclomatic-complexity

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I reduce cyclomatic complexity in my code?

Measure each function's decision points first, then refactor in order of preference: guard clauses to invert conditions, extract functions for sub-tasks, lookup tables to replace if/else chains, and named predicates for compound booleans. Re-run tests after each change to confirm behavior is preserved.

What tools measure cyclomatic complexity in Python, Go, and JavaScript?

For Python use radon cc or ruff check --select C901. For Go use gocyclo or golangci-lint with cyclop and gocyclo enabled. For TypeScript and JavaScript use the ESLint complexity rule, and lizard works as a polyglot option across many languages.

What cyclomatic complexity threshold is too high?

Scores of 1-5 are low and fine, 6-10 are moderate and worth watching, 11-15 are high and should be refactored, and 16 or above must be split into smaller units. Project linter configs like .eslintrc, golangci.yml, or ruff.toml override these defaults.

Can I refactor complex code without existing tests?

Refactoring without tests is risky because behavior changes can go undetected. The safe approach is to declare the gap, add baseline regression tests first, or refactor conservatively, then run the test suite before and after to verify zero regressions.

Why is nesting ternaries or comprehensions a bad way to lower complexity?

Packing branches into dense one-liners hides complexity instead of removing it, making code harder to read and debug. Complexity should move into well-named single-responsibility functions, not disappear into syntactic cleverness.