Measure code complexity using cyclomatic complexity (McCabe metric). Analyze decision points, control flow graphs, and get risk assessments, testing recommendations, and industry compliance reports for better software maintainability.
Count all decision points in your code: if, else if, for, while, do-while, switch cases, catch blocks, ternary operators (? :), logical AND (&&), logical OR (||), and null coalescing (??). Each adds one path through the code.
You might also find these calculators useful
Analyze and compare algorithm time and space complexities
Analyze code patterns and estimate algorithm runtime
Analyze memory usage for algorithms and data structures
Convert between binary, decimal, hex & octal
Cyclomatic complexity is a quantitative measure of the number of linearly independent paths through a program's source code. Developed by Thomas J. McCabe in 1976, this metric helps developers understand code complexity, estimate testing effort, and identify modules that may be difficult to maintain. Our calculator supports both the decision-point method and control flow graph method for accurate complexity measurement.
Cyclomatic complexity (CC) measures the structural complexity of code by counting the number of independent paths through a module. A higher complexity indicates more decision points, making the code harder to understand, test, and maintain. The metric is computed using control flow graphs where edges represent transitions and nodes represent statements.
McCabe's Formula
M = E - N + 2P (Graph) or M = π + 1 (Decision Points)The complexity number equals the minimum number of test cases needed for full branch coverage, helping QA teams plan testing efforts accurately.
High complexity often correlates with higher defect rates. Identifying complex modules helps prioritize refactoring efforts.
Complex code is harder to understand and modify. Tracking complexity helps maintain readable, maintainable codebases.
Industry standards use complexity thresholds to identify high-risk code that may require additional review or restructuring.
Use complexity metrics during code reviews to identify functions that may be too complex and need refactoring into smaller, more manageable units.
Plan unit test suites by knowing the minimum number of test cases required for complete path coverage of each module.
Track complexity over time to identify accumulating technical debt and prioritize refactoring efforts in sprint planning.
Implement complexity thresholds in CI/CD pipelines to prevent overly complex code from being merged into production branches.
Most industry standards recommend: 1-10 (low risk, good), 11-20 (moderate risk, review needed), 21-50 (high risk, consider refactoring), and above 50 (very high risk, requires immediate attention). NIST recommends keeping complexity below 10 for individual functions.
Count each: if statement, else if clause, for loop, while loop, do-while loop, switch case (each case), catch block, ternary operator (?:), logical AND (&&), and logical OR (||). Each represents a branching point in your code.
The decision-point method (M = π + 1) is simpler and works well for single-exit functions. The graph method (M = E - N + 2P) is more precise and handles multiple exit points. For single-entry, single-exit programs, both give the same result.
The complexity number equals the minimum number of test cases needed to execute every branch in the code at least once. A complexity of 10 means you need at least 10 test cases for full branch coverage.
Extract complex conditions into separate functions, replace nested conditionals with guard clauses, use polymorphism instead of switch statements, apply the strategy pattern, and break large functions into smaller single-responsibility functions.
Popular tools include: SonarQube, ESLint (with complexity rule), PyLint, Checkstyle (Java), Code Climate, and most modern IDEs. These can be integrated into CI/CD pipelines for automated quality gates.