reduce-complexity

Measure and reduce cyclomatic and cognitive complexity in Python code through guided refactoring.

Updated May 3, 2017
One-click install
npx skills add https://github.com/arnonmoscona/dot_files --skill reduce-complexity-arnonmoscona
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: reduce-complexity
Source: https://github.com/arnonmoscona/dot_files/tree/main/wsl/.claude/skills/reduce-complexity
Command: npx skills add https://github.com/arnonmoscona/dot_files --skill reduce-complexity-arnonmoscona

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Python codebases accumulate overly complex functions, deeply nested logic, and long files that are hard to maintain and test. This Skill measures cyclomatic and cognitive complexity, identifies the worst offenders, and applies proven refactoring patterns to simplify the code safely. ## Core Features & Use Cases - Complexity Measurement: Uses tools like pyscn, radon, lizard, complexipy, xenon, and wily to score functions and track complexity trends over time. - Refactoring Patterns: Provides concrete patterns including extract function, guard clauses, lookup tables, comprehensions, magic number extraction, and type alias consolidation. - Safety-First Workflow: Enforces prerequisites like full test coverage and a clean git tree before refactoring, plus a verification checklist afterward. - Use Case: A developer runs pyscn on a legacy module, finds a function with cognitive complexity of 25, and uses the extract-function and guard-clause patterns to bring it under the threshold while keeping all tests green. ## Quick Start Analyze this Python project for overly complex functions and refactor the worst ones while keeping all tests passing.

Frequently Asked Questions about reduce-complexity

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

FAQPage Schema
How do I reduce cyclomatic complexity in Python functions?

Measure complexity with tools like pyscn or radon, then apply refactoring patterns such as extract function, guard clauses, and lookup tables. Ensure the test suite is fully green before refactoring and verify complexity scores improve afterward.

What tools measure cognitive complexity in Python code?

pyscn is the preferred tool, covering multiple complexity measures plus dead code detection. Alternatives include complexipy and lizard for cognitive complexity, radon for cyclomatic complexity and maintainability index, and wily for tracking trends across git history.

radon vs lizard for Python complexity analysis?

radon measures cyclomatic complexity and a maintainability index, flagging functions at complexity 11 or higher. lizard focuses on cognitive complexity, which better reflects readability, and warns above a threshold of 15.

When should I not refactor a complex function?

Do not refactor functions lacking good unit test coverage or when the test suite has any failures. Create tests first, ensure a clean git tree, and only then refactor so behavior preservation can be verified.

What complexity thresholds should trigger refactoring?

General guidance is to refactor at cyclomatic complexity of 11 or higher, cognitive complexity above 15, maintainability index below 65, and code files significantly over 500 lines. pyscn provides its own high, medium, and low severity scoring.