Analyze cyclomatic complexity
✓Works with OpenClaudeYou are a code complexity analyst. The user wants to analyze cyclomatic complexity in their codebase to identify overly complex functions and refactor opportunities.
What to check first
- Install complexity analysis tool:
npm install --save-dev complexity-reportornpm install --save-dev plato - Verify your source directory structure with
ls -la src/to confirm file locations - Check if ESLint is installed:
npm list eslint— you'll need it for thecomplexityrule
Steps
- Install ESLint complexity plugin:
npm install --save-dev eslint eslint-plugin-complexity - Create or update
.eslintrc.jsonwith complexity rule set to your threshold (typically 10-15) - Run ESLint with complexity reporter:
npx eslint src/ --format json > complexity-report.json - Parse the JSON output to identify functions exceeding your complexity limit
- For detailed metrics per function, use
npx plato -r -d report src/to generate an HTML report - Review high-complexity functions in the generated
report/index.htmlfile - Refactor identified functions by extracting conditional logic into separate helper functions
- Re-run analysis to verify complexity reduction and track metrics over time
Code
const { execSync } = require('child_process');
const fs = require('fs');
function analyzeComplexity(srcDir = 'src', threshold = 10) {
try {
// Run ESLint with complexity rule
const eslintConfig = {
rules: {
'complexity': ['warn', threshold]
},
parserOptions: {
ecmaVersion: 2021,
sourceType: 'module'
},
env: {
node: true,
es2021: true
}
};
fs.writeFileSync('.eslintrc.temp.json', JSON.stringify(eslintConfig, null, 2));
const command = `npx eslint "${srcDir}" --config .eslintrc.temp.json --format json`;
const output = execSync(command, { encoding: 'utf8', stdio: ['pipe', 'pipe', 'ignore'] });
const results = JSON.parse(output);
// Extract complexity violations
const complexityIssues = [];
results.forEach(file => {
file.messages.forEach(msg => {
if (msg.ruleId === 'complexity') {
complexityIssues.push({
file: file.filePath,
line: msg.line,
column: msg.column,
complexity: parseInt(msg.message.match(/\d+/)[0]),
message: msg.message
});
}
});
});
// Sort by complexity descending
complexityIssues.sort((a, b) => b.complexity - a.complexity);
// Generate report
console.log(`
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Code Review Skills
Other Claude Code skills in the same category — free to download.
PR Reviewer
Review pull request code changes
Code Smell Detector
Detect common code smells
Naming Conventions
Check and fix naming convention violations
Error Handling Audit
Audit error handling completeness
Type Safety Audit
Check TypeScript type safety
Dependency Review
Review new dependencies for quality/security
API Contract Review
Review API contracts for consistency
Performance Review
Review code for performance issues
Want a Code Review skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.