code-complexity-analysis
Measuring and reducing cyclomatic complexity and cognitive complexity to improve maintainability.
by sethdford★ 51 GitHub stars
Install code-complexity-analysis in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
About this skill
Code Complexity Analysis
Quantifying and reducing the inherent difficulty of code.
Context
You are helping the engineer measure and reduce code complexity. If code is provided, calculate metrics and suggest decomposition strategies. Complexity is a primary predictor of bugs.
Domain Context
- Cyclomatic Complexity: Number of independent paths through code; each decision point adds 1
- Cognitive Complexity: How hard is it for a human to understand? Nesting, jumps, and abstractions add cost
- Threshold: Cyclomatic complexity > 10 is a smell; > 15 demands refactoring
- Testing Cost: Each decision path requires a test; high complexity means high test burden
- Maintenance Cost: High complexity → higher bug rate, slower changes
Instructions
- Count Decisions: Count if/else, switch, loops, operators; each adds 1 to cyclomatic complexity
- Check Nesting: Each nested level adds cognitive cost; avoid nesting > 3 levels deep
- Identify Polymorphism Opportunities: Replace switch/if chains with strategy pattern
- Extract Complex Conditions: Move boolean logic to named methods or variables
- Break Long Methods: Extract sub-methods even if called once; improves understanding
- Use Early Returns: Guard clauses reduce nesting and improve readability
- Reduce Parameters: Complex signatures add cognitive load; use parameter objects
- Test Complexity: If > 10 tests needed for one method, it's too complex
Anti-Patterns
- Dismissing complexity metrics as "not real"; high complexity correlates with bug density (empirically proven)
- Reducing complexity by extracting methods without naming them clearly; you just moved the complexity
- Creating single-responsibility methods that are still cognitively hard to understand; sometimes you need simpler logic
- Ignoring cyclomatic complexity because "the code works"; it works until the edge case you didn't test
- Using code golf or clever tricks to reduce line count; they increase cognitive complexity and invite bugs
Further Reading
- Thomas J. McCabe, "A Complexity Measure" (1976, cyclomatic complexity)
- Sonar, Cognitive Complexity whitepaper
- John Ousterhout, A Philosophy of Software Design, on complexity
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the code-complexity-analysis skill do?
Measuring and reducing cyclomatic complexity and cognitive complexity to improve maintainability.
How do I install the code-complexity-analysis skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the code-complexity-analysis skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
skill-creator
AnthropicCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an…
discernment-nudge
AnthropicAfter you give a substantive answer or draft that the user may act on — advice or recommendations, drafted artifacts such as goals, plans, pitches, proposals…
data-consolidate
AnthropicJoins the plant's scheduled exports and connector data into one working dataset — ERP items (NetSuite or Infor M3), sales orders, production work orders…
inspection-to-capa
AnthropicRuns the quality chain end to end: writes the nonconformance against the requirement it violates, searches history at the moment of writing to say whether…