Code Complexity Analyzer

Measure cyclomatic and cognitive complexity across multiple programming languages

Analyze source code quality with cross-language complexity heuristics.

Checks included

  • Cyclomatic complexity
  • Cognitive complexity
  • Long functions
  • Deep nesting
  • Duplicate code windows
  • Improvement suggestions

Example Results

2 examples

Analyze a branch-heavy JavaScript function

Measure cyclomatic and cognitive complexity for a nested pricing function using tight thresholds.

{
  "summary": {
    "language": "javascript",
    "functionCount": 1,
    "averageCyclomaticComplexity": 5,
    "averageCognitiveComplexity": 4,
    "hotspotCount": 0,
    "longFunctionCount": 0,
    "deepNestingCount": 1,
    "duplicateClusters": 0
  },
  "functions": [
    {
      "name": "score",
      "cyclomaticComplexity": 5,
      "deepNesting": true
    }
  ]
}
View input parameters
{ "sourceCode": "function score(order) {\n if (!order) return 0;\n let total = 0;\n for (const item of order.items) {\n if (item.discount) {\n total += item.price * 0.8;\n } else if (item.vip) {\n total += item.price * 0.9;\n } else {\n total += item.price;\n }\n }\n return total;\n}", "language": "javascript", "longFunctionThreshold": 8, "nestingThreshold": 2, "duplicateWindow": 4 }

Spot duplicate normalization logic in Python

Detect repeated helper-style functions and confirm duplicate windows across similar code blocks.

{
  "summary": {
    "language": "python",
    "functionCount": 2,
    "duplicateClusters": 1
  },
  "duplicates": [
    {
      "occurrences": 2,
      "lines": [
        1,
        6
      ]
    }
  ]
}
View input parameters
{ "sourceCode": "def normalize_user(row):\n email = row[\"email\"].strip().lower()\n if not email:\n return None\n return {\"email\": email, \"team\": row.get(\"team\", \"general\")}\n\ndef normalize_admin(row):\n email = row[\"email\"].strip().lower()\n if not email:\n return None\n return {\"email\": email, \"team\": row.get(\"team\", \"general\")}", "language": "python", "longFunctionThreshold": 20, "nestingThreshold": 3, "duplicateWindow": 3 }

Key Facts

Category
Developer & Web
Input Types
textarea, select, number
Output Type
json
Sample Coverage
4
API Ready
Yes

Overview

The Code Complexity Analyzer evaluates source code quality by measuring cyclomatic and cognitive complexity across JavaScript, TypeScript, Python, Java, and Go. It identifies long functions, deep nesting levels, and duplicate code blocks using configurable thresholds to help developers prioritize refactoring efforts and maintain clean code standards.

When to Use

  • Preparing for code reviews to flag high-complexity functions that need simplification before merging
  • Refactoring legacy codebases to identify hotspots with excessive branching or nesting depth
  • Enforcing quality gates in development workflows to prevent complex code from entering production

How It Works

  • Paste your source code into the input field and select the programming language, or use auto-detect for supported languages
  • Configure thresholds for long function length, maximum nesting depth, and duplicate code window size to match your team's standards
  • The analyzer calculates cyclomatic complexity by counting branches and cognitive complexity by assessing human readability for each function
  • Receive a JSON report highlighting complexity hotspots, duplicate clusters, and specific improvement suggestions

Use Cases

Identifying refactoring candidates in legacy monoliths with undocumented complexity debt and untested branching logic
Automating code quality checks during pull request reviews to prevent technical debt accumulation in shared libraries
Educating development teams on complexity metrics by demonstrating concrete improvement opportunities in existing code

Examples

1. Simplify Nested JavaScript Pricing Logic

Senior Frontend Developer
Background
A legacy checkout module contains a pricing function with multiple nested conditionals for discounts and VIP status, making it difficult to unit test and maintain safely.
Problem
The function has hidden complexity and deep nesting that increases bug risk during feature updates, but the exact metrics are unknown.
How to Use
Paste the score function code into the Source Code field, select JavaScript as the language, and set the nesting threshold to 2 to catch deep conditional blocks.
Example Config
{
  "language": "javascript",
  "longFunctionThreshold": 8,
  "nestingThreshold": 2,
  "duplicateWindow": 4
}
Outcome
Analysis reveals cyclomatic complexity of 5 and flags deep nesting, prompting the team to extract discount logic into separate functions for improved testability and reduced cognitive load.

2. Deduplicate Python Data Normalization Helpers

Backend Engineer
Background
A data processing pipeline contains two nearly identical helper functions for normalizing user and admin records, created through copy-paste development over multiple sprints.
Problem
Duplicate logic increases maintenance burden and risks inconsistent updates when business rules change, but the similarity was not obvious during manual review.
How to Use
Paste both normalize_user and normalize_admin functions into the input, set language to Python, and reduce the duplicate window to 3 to catch similar logic blocks.
Example Config
{
  "language": "python",
  "longFunctionThreshold": 20,
  "nestingThreshold": 3,
  "duplicateWindow": 3
}
Outcome
The analyzer detects one duplicate cluster spanning both functions, recommending consolidation into a single parameterized helper to eliminate redundancy.

Try with Samples

development

Related Hubs

FAQ

What is cyclomatic complexity?

Cyclomatic complexity measures the number of linearly independent paths through a function's source code by counting branches such as if statements, loops, and case clauses. Higher values indicate more test cases are required for full coverage.

Which programming languages are supported?

The analyzer supports JavaScript, TypeScript, Python, Java, and Go. You can manually select the language or use auto-detect to identify the syntax automatically.

How does cognitive complexity differ from cyclomatic complexity?

While cyclomatic complexity counts branches mathematically, cognitive complexity assesses how difficult code is for humans to understand, penalizing nested structures and logical jumps more heavily than simple sequential branches.

What does the duplicate window setting control?

The duplicate window defines the minimum number of consecutive tokens that must match to flag code as potentially duplicated. Lower values catch smaller similarities, while higher values identify only substantial copy-pasted blocks.

Can I analyze multiple files or entire projects?

Currently, the tool analyzes one code snippet at a time via paste input. For project-wide analysis, run the tool separately on individual files or aggregate critical modules into a single paste.

API Documentation

Request Endpoint

POST /en/api/tools/code-complexity-analyzer

Request Parameters

Parameter Name Type Required Description
sourceCode textarea Yes -
language select No -
longFunctionThreshold number No -
nestingThreshold number No -
duplicateWindow number No -

Response Format

{
  "key": {...},
  "metadata": {
    "key": "value"
  },
  "error": "Error message (optional)",
  "message": "Notification message (optional)"
}
JSON Data: JSON Data

AI MCP Documentation

Add this tool to your MCP server configuration:

{
  "mcpServers": {
    "elysiatools-code-complexity-analyzer": {
      "name": "code-complexity-analyzer",
      "description": "Measure cyclomatic and cognitive complexity across multiple programming languages",
      "baseUrl": "https://elysiatools.com/mcp/sse?toolId=code-complexity-analyzer",
      "command": "",
      "args": [],
      "env": {},
      "isActive": true,
      "type": "sse"
    }
  }
}

You can chain multiple tools, e.g.: `https://elysiatools.com/mcp/sse?toolId=png-to-webp,jpg-to-webp,gif-to-webp`, max 20 tools.

If you encounter any issues, please contact us at [email protected]