Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Code Reviewintermediate

Complexity Analyzer

Share

Analyze cyclomatic complexity

Works with OpenClaude

You 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-report or npm 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 the complexity rule

Steps

  1. Install ESLint complexity plugin: npm install --save-dev eslint eslint-plugin-complexity
  2. Create or update .eslintrc.json with complexity rule set to your threshold (typically 10-15)
  3. Run ESLint with complexity reporter: npx eslint src/ --format json > complexity-report.json
  4. Parse the JSON output to identify functions exceeding your complexity limit
  5. For detailed metrics per function, use npx plato -r -d report src/ to generate an HTML report
  6. Review high-complexity functions in the generated report/index.html file
  7. Refactor identified functions by extracting conditional logic into separate helper functions
  8. 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

Quick Info

CategoryCode Review
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
reviewcomplexitymetrics

Install command:

curl -o ~/.claude/skills/complexity-analyzer.md https://claude-skills-hub.vercel.app/skills/code-review/complexity-analyzer.md

Related Code Review Skills

Other Claude Code skills in the same category — free to download.

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.