How cyclomatic complexity is calculated
McCabe defined it on the control flow graph of a function: v(G) = e − n + 2, where e is the number of edges and n the number of nodes. When every decision is a yes or no, that works out to the number of decision points plus one (NIST SP 500-235, section 4.1), which is what this page counts.
What counts as a decision point:
if,else if(elif,elsif,elseif),unlessfor,foreach,while,until- each
case(whenin Ruby) but notdefault catch(except,rescue)&&,||(and,orin Python and Ruby) and??- the ternary
? :
Plain else, try, finally and switch itself add nothing. A decision inside a function nested in another is counted for the inner function only. Optional chaining (?.) is not counted here, although ESLint counts it, so the two can differ.
A worked example
function validateUser(user) {
if (!user) { return invalid; } // +1
if (!user.email || !user.email.includes('@')) { return invalid; } // +1 if, +1 ||
if (!user.password || user.password.length < 8) { return invalid; } // +1 if, +1 ||
if (user.age && (user.age < 13 || user.age > 120)) { return invalid; } // +1 if, +1 &&, +1 ||
return valid;
}
Eight decision points plus one gives a complexity of 9. That is under 10, but every extra condition in that function adds a path someone has to test. Use the "Moderate" example above to see the calculator return the same number.
What is a good score?
NIST's guide to structured testing says the limit of 10 proposed by McCabe has significant supporting evidence, that limits as high as 15 have been used successfully, and that the right number "remains somewhat controversial". ESLint's complexity rule uses 20 by default.
| Our band | Complexity |
|---|---|
| low | 1 to 10 |
| moderate | 11 to 20 |
| high | 21 to 30 |
| very high | 31 and over |
The bands are our rule of thumb, built on those two numbers; they are not a standard. Nesting depth here counts every pair of braces (or indented levels in Python and Ruby) inside the function, so a function body is depth 1 and an object literal counts as a level.
Lowering complexity
- Return early (guard clauses) instead of wrapping the rest in an
if. - Pull a long condition into a well-named function or variable.
- Replace a long chain of
ifandelse ifwith a lookup table orswitch, if each branch is one line. - Split a function that does two jobs into two functions.
Questions
What is code complexity?
A measure of how hard code is to understand and test. The most common measure is cyclomatic complexity: the number of independent paths through a function. More paths mean more tests to cover them and more places for a bug to hide.
What is cyclomatic complexity?
Thomas McCabe's measure of the independent paths through a piece of code. For one function it is the number of decision points plus one: every if, loop, case, catch, && and || adds a path.
How do I calculate cyclomatic complexity?
Count the decision points in the function and add one. A function with no branches is 1. One if makes 2. An if with an && in its condition makes 3. Paste your code above to have it counted per function.
What is a good cyclomatic complexity score?
McCabe proposed a limit of 10 per function, and NIST's guide says that limit has significant supporting evidence, though limits as high as 15 have been used successfully. ESLint's complexity rule defaults to 20. Treat these as prompts to look again, not as pass or fail.
Which languages does the calculator support?
JavaScript, TypeScript, PHP, Java, C#, Go, Python and Ruby, or let it detect the language. It reads the code with a scanner, not a full parser, so it can be wrong on unusual syntax, such as regular expressions or text blocks that contain quotes. Check any score that matters with the tool built for your language. A tool wired into your build, such as the ones below, reads your code exactly.
What are the best code complexity analysis tools?
For a build or editor, use the one for your language: ESLint's complexity rule for JavaScript and TypeScript, radon for Python, PHPMD for PHP, and SonarQube for many languages at once. This page is for a quick check of a snippet. Jasper's AI code review reads each pull request and leaves review comments.
Does my code leave my browser?
No. The analysis runs in your browser and nothing you paste is sent to a server.
Sources
Last checked 3 November 2026.
- NIST Special Publication 500-235, Structured Testing: A Testing Methodology Using the Cyclomatic Complexity Metric (Watson and McCabe, 1996)
- ESLint: the complexity rule (default maximum of 20)
- radon: code metrics for Python
- PHPMD: PHP mess detector
- SonarQube documentation
Changes: 3 November 2026: counting rewritten to work per function and per language.