Cyclomatic Complexity Calculator
Calculate McCabe's cyclomatic complexity from a control flow graph — enter edges, nodes and connected components to find M, see how many independent test paths exist, and assess where the code falls on the risk scale.
Simple, well-structured code — low risk.
- 1
E − N (edges minus nodes)
8 − 7 = 1 - 2
2P (connected components term)
2 × 1 = 2 - 3
Cyclomatic complexity M = E − N + 2P
1 + 2 = 3M equals the minimum number of test cases for full branch coverage.
How does this calculator work?
Cyclomatic complexity M = E − N + 2P counts independent execution paths through code. Quick formula: M = decision points + 1. M ≤ 10 is low-risk; above 20 signals a refactoring need; above 50 is nearly untestable. M also equals the minimum test cases needed for full branch coverage.
Formula
How this is calculated
Cyclomatic complexity was introduced by Thomas McCabe in 1976 as a graph-theoretic measure of structural code complexity. The program's control flow graph (CFG) represents each basic block as a node and each branch as a directed edge. The formula M = E − N + 2P counts the linearly independent paths through the graph, where E is edges, N is nodes, and P is connected components — typically 1 for a single routine.
For a single connected function the formula simplifies to M = decision points + 1. Decision points include if/else-if, while, for, do-while, switch-case, catch blocks, and short-circuit logical operators (&&, ||). A straight-line function with no branches has M = 1; every branch adds one.
M equals the minimum number of unit test cases needed for 100% branch (decision) coverage. Industry conventions rate M ≤ 10 as low-risk and easy to test; 11–20 as moderate; 21–50 as high-risk where refactoring is advisable; above 50 as effectively untestable. Static analysis tools such as SonarQube and ESLint enforce a configurable threshold — commonly 10 or 15 per function.
Frequently asked questions
Yes. For a single connected function, M = number of decision points + 1. Decision points include if, else if, while, for, do-while, switch case, catch, && and || (in short-circuit languages). This avoids drawing the full CFG and gives the same result.
Most coding standards cap it at 10. Some allow 15 for complex business logic. Above 20 is a refactoring priority; above 50 the function is considered untestable. Many CI pipelines fail the build if any routine exceeds the configured threshold.
M is the exact number of linearly independent paths through the code — the minimum test cases for 100% branch coverage. Lower M means fewer tests, fewer hiding places for bugs, and lower maintenance cost over time.
Also known as
TG we-Calculate Editorial Team. (2026). Cyclomatic Complexity Calculator [Online calculator]. TG we-Calculate. https://we-calculate.com/calculator/cyclomatic-complexity-calculator
TG we-Calculate Editorial Team. "Cyclomatic Complexity Calculator." TG we-Calculate. 2026. https://we-calculate.com/calculator/cyclomatic-complexity-calculator.
TG we-Calculate Editorial Team, "Cyclomatic Complexity Calculator," TG we-Calculate, 2026. [Online]. Available: https://we-calculate.com/calculator/cyclomatic-complexity-calculator
@misc{wecalculate_cyclomatic_complexity_calculator, title = {Cyclomatic Complexity Calculator}, author = {{TG we-Calculate Editorial Team}}, howpublished = {\url{https://we-calculate.com/calculator/cyclomatic-complexity-calculator}}, year = {2026}, note = {TG we-Calculate} }
Did this calculator help you?
