All skills
Skillintermediate
/analyze-complexity - Analyze Code Complexity
Calculate cyclomatic complexity and identify overly complex functions.
Claude Code Knowledge Pack7/10/2026
Overview
/analyze-complexity - Analyze Code Complexity
Calculate cyclomatic complexity and identify overly complex functions.
Steps
- Detect the project language to select the appropriate complexity analysis tool
- Scan all source files excluding tests, vendor, and generated code
- Calculate cyclomatic complexity for each function and method
- Calculate cognitive complexity as a secondary metric
- Identify functions exceeding the threshold (default: cyclomatic > 10)
- Rank the top 20 most complex functions by complexity score
- For each complex function, identify the complexity drivers: nested conditionals, switch cases, loops
- Calculate file-level complexity averages and identify the most complex files
- Compare against industry benchmarks: low (1-5), moderate (6-10), high (11-20), very high (21+)
- Generate a formatted report with function name, file, line, complexity score
- Suggest specific refactoring strategies for the top 5 most complex functions
- Save the report and track complexity trends over time
Rules
- Use cyclomatic complexity as the primary metric
- Include cognitive complexity as a complementary measure
- Exclude generated code, migrations, and configuration files
- Threshold of 10 for warnings, 20 for critical flags
- Consider function length alongside complexity (long + complex = priority)
- Do not count simple switch/case with returns as highly complex
- Report both absolute values and percentile rankings