How to use the Cyclomatic Complexity Calculator
- 1. Choose inputPaste a function, list flow-graph edges, enter E, N and P, or the number of decisions.
- 2. CalculateThe tool counts decisions or graph parts and applies McCabe's formula.
- 3. Use V(G)See V(G), how it was worked out, and the risk band. V(G) is the number of basis paths to test.
Three ways to calculate V(G)
McCabe's cyclomatic complexity counts the linearly independent paths through a program. It is also the least number of test cases needed for basis path testing. For a control flow graph:
- V(G) = E − N + 2P, where E is edges, N is nodes and P is connected parts (1 for a single function).
- V(G) = number of predicate (decision) nodes + 1, counting each binary decision once.
- V(G) = number of regions in a planar flow graph, including the outside region.
In code, each if, else if, for, while, case, catch and each extra condition joined by && or || adds one decision. The ternary ?: also adds one.
Worked example
The default C function has: if (att < 75); if (m >= 90 && att >= 90), which counts as 2 (the if plus the &&); else if (m >= 75); one for loop; one while loop; and one ?: operator. That is 7 decisions, so V(G) = 7 + 1 = 8.
For the default flow graph (edges 1–2, 1–3, 2–4, 3–4, 4–5, 4–6, 5–7, 6–7) there are 8 edges, 7 nodes and 1 part. V(G) = 8 − 7 + 2 = 3. Nodes 1 and 4 each have two exits, so 2 decisions + 1 = 3 as well. You need 3 test cases to cover every basis path.
What the number means
McCabe's guide: 1–10 is simple and low risk, 11–20 is moderate, 21–50 is complex, and above 50 is very hard to test. Many teams refactor functions above 10. Tools differ slightly. Some do not count && and || (this gives the plain McCabe number), and some count each case label. Follow your syllabus. For test planning, see the test coverage calculator.
Frequently asked questions
Why does && count as a decision?
Short-circuit evaluation makes a && b two separate branches in the compiled code. Each extra condition adds a path. This is the extended McCabe count.
What if E − N + 2P and the predicate count differ?
The graph is probably not a proper flow graph. Check for unreachable nodes, missing edges or more than one exit node.
Does it handle several functions at once?
Code mode adds up all decisions and adds 1, which treats the code as one function. Paste one function at a time for per-function values.
How is V(G) used in testing?
It gives the number of independent paths. Basis path testing writes at least that many test cases so that every path is run once.