Saved

Press the star next to a tool's name to save it.

All my tools

Cyclomatic Complexity Calculator

Use this cyclomatic complexity calculator to find McCabe's measure from edges and nodes, a count of decision points, or an estimate from pasted source code.

Restored your last values ·

For a control-flow graph, enter the number of edges, nodes and connected components. For a count of decision points, enter it and the number of exits. To scan source code, paste it into the box and the calculator counts the decision points.

Everything is worked out in your browser. Nothing you type is sent anywhere.

–

–

Choose what you have, fill in the boxes, then press Calculate.

A cyclomatic complexity calculator works out the cyclomatic complexity of a program, McCabe's measure of the number of independent paths through its code. You can enter the edges and nodes of its control-flow graph, a count of decision points, or paste source code for an estimate. It gives the number and a risk band. The formulas follow the Wikipedia page on cyclomatic complexity, and the work is done in your browser.

How to use the cyclomatic complexity calculator

  1. Choose what you have: a graph, a count of decisions or code.
  2. Enter the edges, nodes and components, the decision points, or paste the code.
  3. Press Calculate.

The graph formula

The page defines cyclomatic complexity with the control-flow graph of the program, in which the nodes are blocks of code and the edges are the jumps between them. It is M = E − N + 2P, where E is the number of edges, N the number of nodes and P the number of connected components. For a single subroutine, P is 1, so M = E − N + 2. The page's first example is a graph with 9 edges and 8 nodes, which gives 9 − 8 + 2, or 3. The page also gives a variation for a graph in which every exit is linked back to the entry, where M = E − N + P. A graph with 10 edges and 8 nodes of this kind has M = 10 − 8 + 1, which is 3 again. The calculator offers both.

The decision-point shortcut

For a structured program with one entry and one exit, the page says that M is the number of decision points plus one. A decision point is a place where the flow splits, such as an if, a loop condition or a case. Two if-else blocks one after the other have two decision points, so M = 3. The page confirms it with the graph, which has 8 edges and 7 nodes, so M = 8 − 7 + 2 = 3. It adds that this means 2 tests are enough for branch coverage and 4 for coverage of every path. For a function with several exits, the page gives M = π − s + 2, where π is the number of decision points and s the number of exits. The calculator uses this when you give more than one exit.

Scanning source code

For a quick estimate, paste your code. The calculator removes comments and strings, so keywords inside them are not counted, and then counts the decision points: the words if, else if, elif, for, foreach, while, case and catch or except, the operators && and ||, and the ternary ? operator. The estimate is the count plus one. For the code if (a && b) with a for loop inside and an else if, the decisions are two ifs, one for and one &&, so the complexity is 5. Different tools count some constructs differently, so treat this as a guide.

Risk bands

The page quotes the risk bands from McCabe's work: a value from 1 to 10 is a simple program without much risk, 11 to 20 is more complex with moderate risk, 21 to 50 is complex with high risk, and above 50 is untestable. The calculator reports the band with the number. A common practice is to keep every function at 10 or below and to split any that exceed it into smaller functions.

Why the number matters

A higher number means more paths to test and more places for mistakes. It is a quick, objective way to spot the functions that deserve a review or more tests. It does not measure everything: a long function with no branches has a low score and can still be hard to read. Use it together with other checks, such as code review and test coverage.

A worked example with code

Consider a function that turns a score into a grade. It has three tests: if the score is at least 90, else if it is at least 80, else if it is at least 70, and then a final return. There are three decision points, so the complexity is 3 + 1, which is 4. Paste the function into the code option to see the count. Now consider a loop with a condition inside: a for loop containing an if whose test uses both an and and an or. The decisions are the for, the if, the and and the or, which is four, so the complexity is 5. Each short-circuit operator adds a path, since the second part is only evaluated in some cases.

Reducing complexity

If a function scores above 10, there are standard ways to bring it down. Split it into smaller functions, each with one job. Replace a long chain of if and else if tests with a lookup table or a switch on data. Return early on error cases to avoid nesting. Move complicated conditions into functions with a name that explains them. Each of these makes the code easier to test, since fewer paths need a test case.

Typing tips

For a graph, count the edges and nodes of the control-flow graph and enter them. If the program has more than one separate part, such as several functions in one graph, enter the number of connected components. For code, paste a single function at a time to get the complexity of that function, since a whole file combines all of its functions.

Common mistakes

  • Forgetting the plus one. A function with no decisions has a complexity of 1.
  • Counting the entry and exit as decisions. Only branching points count.
  • Counting && and || as nothing. Short-circuit operators create paths.
  • Mixing the two graph formulas. Use the linked form only when exits return to the entry.
  • Treating the code scan as exact. It is an estimate.

What next

Use this often? Add Cyclomatic C to your Home screen.

Frequently asked questions

What is a cyclomatic complexity calculator?

A tool that works out McCabe's cyclomatic complexity, a measure of the number of independent paths through a program.

What is the cyclomatic complexity formula?

M = E − N + 2P for edges, nodes and connected components. For a graph with 9 edges and 8 nodes, M = 3.

How do I count it from decision points?

Add one to the number of decision points in a function with a single exit. Two if-else blocks give 3.

What is a good cyclomatic complexity?

1 to 10 is simple, 11 to 20 moderate, 21 to 50 high risk and above 50 untestable.

How does the code scan work?

It strips comments and strings and counts if, loops, case, catch, && , || and ?, then adds one. It is an estimate.

What went wrong?

We only receive the reason you pick, never what you typed into the tool.

Thanks for your feedback.

More Developer Tools