Code Complexity Analyzer

Analyze cyclomatic complexity, cognitive complexity, nesting depth, and maintainability of your source code. Supports JavaScript, TypeScript, Python, PHP, Java, Go, Ruby, C++, and C#.

  1. Home
  2. Developer Tools
  3. Code Complexity Analyzer

What Is Code Complexity Analysis?

Code complexity analysis measures how complicated your source code is using software metrics. The most common metric is cyclomatic complexity, developed by Thomas McCabe in 1976, which counts the number of linearly independent paths through a program's source code. Each decision point (if, else, switch case, loop, catch, ternary operator, logical operator) increases the complexity score.

This tool also calculates cognitive complexity (how hard code is to understand for humans), nesting depth (maximum level of nested control structures), and maintainability index (a composite score from 0-100 combining cyclomatic complexity, lines of code, and Halstead volume). Lower complexity scores indicate code that is easier to test, debug, and maintain. Industry best practices recommend keeping cyclomatic complexity under 10 per function and nesting depth under 4 levels.

How to Use This Code Complexity Analyzer

  1. Paste code — Paste your source code into the text area. Supports JavaScript, TypeScript, Python, PHP, Java, Go, Ruby, C++, and C#.
  2. Click Analyze — Press the Analyze button to instantly compute cyclomatic complexity, cognitive complexity, nesting depth, and maintainability index for each function.
  3. Review results — The overview shows aggregate metrics and an overall maintainability score (0-100). The functions table breaks down complexity per function with color-coded assessments.
  4. Try examples — Use Simple Example to see well-structured code with low complexity, or Complex Example to see how deeply nested code with many decision paths produces high scores.

Frequently Asked Questions

What is cyclomatic complexity?

Cyclomatic complexity, developed by Thomas McCabe in 1976, measures the number of linearly independent paths through a program's source code. Each decision point (if, else if, switch case, while, for, catch, ?: ternary, logical &&/||) adds 1 to the count. Best practice recommends keeping it under 10 per function.

What is the difference between cyclomatic and cognitive complexity?

Cyclomatic complexity counts every decision point equally. Cognitive complexity, introduced by SonarSource, weights decisions by how much they increase human comprehension difficulty. Deep nesting, boolean logic chains, and recursion increase cognitive complexity more than simple independent if statements.

What is a good maintainability index score?

The maintainability index ranges from 0-100. A score of 85+ is highly maintainable, 65-84 is moderately maintainable, and below 65 indicates low maintainability where significant refactoring is recommended.

What nesting depth is acceptable?

Best practices recommend keeping nesting depth at 4 levels or fewer. Depth of 1-2 is ideal, 3-4 is acceptable but worth reviewing, and 5+ indicates code that should be refactored by extracting inner blocks into separate functions or using early returns.

Is my code sent to a server?

No. All analysis is performed entirely in your browser. Your source code is never uploaded, stored, or transmitted. The complexity metrics are computed client-side using JavaScript.