Cyclomatic Complexity Calculator

Calculate McCabe cyclomatic complexity from edges, nodes, and connected components. Classify code complexity and risk levels.

Cyclomatic Complexity (M)
5.00
M = E(15) - N(12) + 2P(1)
Classification
Very Simple
Very low risk - No refactoring needed
Avg Complexity per Method
1.7
Risk: Very low risk
Minimum Test Cases
5.00
For complete path coverage
Decision Density
50%
6 decisions across 12 nodes
Ideal Code Coverage
100%
Test effort: Low
Maintainability
Excellent
Based on M = 5
Refactoring Priority
None
Under control

Complexity Gauge

1 (Simple)10205060+ (Very Complex)

Component Breakdown

Total Complexity
5
Per-Method Avg
1.7
Decision Density
50

Complexity Thresholds Reference

RangeClassificationRisk LevelMin TestsAction
1-5Very SimpleVery Low1-5None
6-10SimpleLow6-10None
11-20ModerateMedium11-20Review
21-50ComplexHigh21-50Refactor
51+Very ComplexVery High51+Rewrite
Planning notes, formulas, and examples

About the Cyclomatic Complexity Calculator

Cyclomatic complexity, introduced by Thomas McCabe in 1976, measures the number of linearly independent paths through a program's source code. It is one of the most widely used software metrics for estimating code complexity, testability, and maintenance risk.

The metric is calculated from the control flow graph of a function: M = E โˆ’ N + 2P, where E is the number of edges, N is the number of nodes, and P is the number of connected components. In practice, it equals the number of decision points (if, else, for, while, case) plus one.

This calculator lets you compute cyclomatic complexity and classifies the result into risk categories. Functions with complexity under 10 are simple and easy to test. Those between 11 and 20 are moderately complex. Above 20, the code becomes difficult to understand and test thoroughly.

When This Page Helps

Cyclomatic complexity is the standard metric for identifying functions that are too complex to maintain safely. It gives classification and testing guidance based on the metric value.

How to Use the Inputs

  1. For the graph method: enter the number of edges (E) in the control flow graph.
  2. Enter the number of nodes (N) in the control flow graph.
  3. Enter the number of connected components (P), usually 1 for a single function.
  4. Alternatively, count the decision points in your function and add 1.
  5. Review the complexity score and risk classification.
  6. Use the minimum test cases recommendation for thorough path coverage.
Formula used
M = E โˆ’ N + 2P where E = edges, N = nodes, P = connected components Simplified: M = decision_points + 1 Minimum test cases needed = M

Example Calculation

Result: Complexity = 5 (Simple, low risk)

M = 15 โˆ’ 12 + 2(1) = 5. A complexity of 5 is classified as simple (1โ€“10 range). The function needs at least 5 test cases for complete path coverage. This is well-structured, maintainable code.

Tips & Best Practices

  • Keep function complexity under 10 for maintainability; refactor above 15.
  • Each decision point (if, for, while, case, &&, ||) adds 1 to complexity.
  • High complexity functions are harder to test โ€” they need more test cases for path coverage.
  • Use the complexity score to identify refactoring candidates in code reviews.
  • Guard clauses (early returns) can simplify nested logic without reducing complexity.
  • Extract complex conditions into well-named boolean functions for clarity.

The McCabe Complexity Metric

Thomas McCabe's cyclomatic complexity has been a cornerstone of software quality measurement since 1976. Its enduring relevance comes from its simplicity and strong correlation with defect rates. Functions with complexity above 10 consistently show 2โ€“3ร— higher defect density in empirical studies.

Complexity Classification Thresholds

The standard thresholds are: 1โ€“10 (simple, low risk), 11โ€“20 (moderate, medium risk), 21โ€“50 (complex, high risk), and 50+ (untestable, very high risk). These thresholds are widely adopted in static analysis tools and coding standards across the industry.

Practical Application

Integrate complexity checks into your CI/CD pipeline. Set a threshold (typically 10โ€“15) and flag functions that exceed it during code review. This prevents complexity from creeping up over time and keeps the codebase maintainable.

Sources & Methodology

Last updated:

Frequently Asked Questions

  • 1โ€“10 is simple and low risk. 11โ€“20 is moderate complexity. 21โ€“50 is high complexity with elevated testing difficulty. Above 50 is very high risk and should be refactored immediately.