Cyclomatic Complexity
Calculate McCabe cyclomatic complexity M = E - N + 2P for software code analysis. Free online developer tool to measure code complexity, risk level, and test coverage requirements.
About This Calculator
The Cyclomatic Complexity Calculator implements McCabe's cyclomatic complexity metric, a quantitative measure of the number of linearly independent paths through a program's source code. The formula is M = E - N + 2P, where E is the number of edges in the control flow graph, N is the number of nodes, and P is the number of connected components (typically 1 for a single function or method).
Cyclomatic complexity is one of the most widely used software quality metrics. It helps developers identify overly complex code that is difficult to test, maintain, and understand. The metric directly correlates with the minimum number of test cases needed to achieve branch coverage.
To use this calculator, construct a control flow graph of your function or program, count the edges (control flow transitions) and nodes (code blocks), and enter them into the fields. The calculator returns both the raw M value and a risk-level classification (Low, Moderate, High, or Very High) to help you interpret the result.
Frequently Asked Questions
What is cyclomatic complexity?
Cyclomatic complexity is a software metric developed by Thomas McCabe that measures the number of linearly independent paths through a program's source code. It is calculated as M = E - N + 2P, where E is the number of edges, N is the number of nodes, and P is the number of connected components in the control flow graph.
What do the cyclomatic complexity values mean?
M <= 10 indicates low risk, well-structured code. 11-20 is moderate risk and may benefit from refactoring. 21-50 is high risk and strongly indicates the code needs restructuring. M > 50 is very high risk -- the code is extremely complex and should be broken down into smaller functions for maintainability and testability.
How does cyclomatic complexity relate to testing?
The cyclomatic complexity number defines the minimum number of test cases needed to achieve branch coverage (also called McCabe coverage). If M = 8, you need at least 8 test cases to cover all independent paths through the code.
How do I count edges and nodes in a control flow graph?
Each statement or code block is a node. Each possible transfer of control (sequential flow, if/else branches, loop iterations, case statements) is an edge. Count all nodes and all arrows connecting them in your control flow graph diagram.
What is considered good cyclomatic complexity?
Most coding standards recommend keeping cyclomatic complexity below 10 per function or method. Many teams set a hard limit of 15 or 20, with anything above that requiring a refactor or justification. Some industries (like safety-critical systems) require M <= 5 for certified code.
Is this cyclomatic complexity calculator free?
Yes, all calculators on Calculy are completely free to use with no registration or usage limits.