What this calculator does
Cyclomatic complexity counts the independent paths through a piece of code. The formula, M = E − N + 2P, comes from graph theory: E is the edges in the control-flow graph, N is the nodes, and P is the number of connected components, which is 1 for a single routine. The result is also the minimum number of test cases needed to exercise every path at least once.
In practice the number is usually reached by a shortcut rather than by drawing the graph, since M also equals the count of decision points plus one. Every if, every loop, every case in a switch and every short-circuiting boolean operator adds one. The two routes give the same answer, and the shortcut is why the metric is cheap enough to compute over an entire codebase.
The formula
The calculator applies M = E − N + 2P directly. Edges are the transfers of control between statements, nodes are the statements or blocks themselves, and P is the number of separate connected pieces in the graph, which is 1 unless several independent routines are being measured together. The minimum independent test path count equals M, and the risk band shown is the conventional interpretation rather than anything the formula itself implies.
| Term | Meaning |
|---|---|
| M | Cyclomatic complexity, the number of linearly independent paths through the code. |
| E | Edges: the possible transfers of control from one node to another. |
| N | Nodes: the statements or basic blocks the control flow passes through. |
| P | Connected components, 1 for a single routine and higher only when unconnected graphs are measured as a set. |
The inputs explained
| Field | What to enter |
|---|---|
| Edges (E) | The number of edges in the control-flow graph. |
| Nodes (N) | The number of nodes. It must be at least 1. |
| Connected components (P) | The number of connected components. Leave this at 1 unless you are measuring several independent routines together. |
When to use it
Deciding what to refactor first
Ranking functions by complexity gives a defensible starting order for refactoring work, since the highest scores are both the hardest to reason about and the ones carrying the most untested paths.
Sizing a testing effort
M is the minimum number of test cases needed for full path coverage of a routine. Summing it across a module gives a floor on how many tests thorough coverage would require, which is often a more useful figure than a line count.
Setting a limit in a code review standard
Many teams cap complexity per function and fail the build above it. Knowing where a particular routine sits relative to that cap is the practical use of the number day to day.
Worked examples
Every figure in the tables below is produced by this page’s own calculator at build time, so the numbers and the tool always agree. Select any row to load that scenario.
How does complexity rise as branches are added?
The node count held at 7 while the number of edges rises, which is what adding branches to a routine does.
| Edges (E) | Cyclomatic complexity (M) | Minimum independent test paths | Testing risk |
|---|---|---|---|
| 6 | 1 | 1 | Low, simple to test |
| 9 | 4 | 4 | Low, simple to test |
| 12 | 7 | 7 | Low, simple to test |
| 16 | 11 | 11 | Moderate |
| 26 | 21 | 21 | High, hard to test thoroughly |
Questions
What is a reasonable cyclomatic complexity limit?
McCabe originally suggested 10 as a practical ceiling per routine, and that figure is still the most common default in linters and review standards. It is a convention rather than a finding, and teams working in styles that produce many small branches often set it higher deliberately.
Can I calculate it without drawing the graph?
Yes, and almost everyone does. M equals the number of decision points plus one. Count each if, each loop, each case in a switch and each && or || that short-circuits, then add one. The answer matches the graph formula exactly.
Does a low complexity mean the code is good?
No. It means the control flow is simple, which is one desirable property among many. Code can score 1 and still be unreadable, wrong or badly structured. The metric is useful for spotting the worst outliers, not for judging quality across the board.
When is P anything other than 1?
Only when several disconnected graphs are measured as one. For a single function it is always 1. Measuring a whole class or module as a unit, with each method a separate component, is the usual case where it is higher.
For another calculation that sits between mathematics and computing, see the Luhn checksum calculator. For how signed values are represented at the machine level, see the two’s complement calculator.