Cyclomatic Complexity — What It Is and How to Reduce It
Cyclomatic complexity (CC) measures how many independent execution paths exist through a function. Higher CC means more test cases needed and more places for bugs to hide. This guide explains the metric and shows how to keep it low.
Code Complexity Analyzer
Paste JavaScript or TypeScript code to analyze cyclomatic complexity, cognitive complexity, nesting depth, and maintainability index per function. 100% client-side — your code never leaves your browser.
How cyclomatic complexity is calculated
Cyclomatic complexity was introduced by Thomas McCabe in 1976. It counts the number of linearly independent paths through a program's control flow graph. In practice, CC = 1 + the number of decision points: each if, else if, case, for, while, do, catch, ternary (?:), logical AND (&&), logical OR (||), and nullish coalescing (??) adds 1 to the count. A function with no branches has CC=1.
Industry thresholds
Most static analysis tools use these thresholds: 1-5 is simple and low risk, 6-10 is moderate and should be reviewed, 11-20 is complex and hard to test thoroughly, and above 20 is very high risk — the function should be refactored. ESLint's complexity rule defaults to a maximum of 20. SonarQube recommends keeping CC under 15. The key insight is that each branch doubles the number of test cases needed for full coverage.
How to reduce cyclomatic complexity
Extract conditional logic into named helper functions. Replace if-else chains with early returns (guard clauses). Use lookup objects or Maps instead of switch statements. Replace complex boolean expressions with well-named variables. Apply the strategy pattern for behavior that varies by type. Each refactoring should make individual functions simpler and more testable without adding overall complexity.
Frequently Asked Questions
What is a good cyclomatic complexity score?
Under 10 per function is generally considered good. 1-5 is simple and easy to test. 6-10 is moderate. Above 10 indicates the function is complex and should be refactored into smaller pieces. ESLint defaults to flagging functions with CC > 20.
Does cyclomatic complexity apply to all programming languages?
Yes. Cyclomatic complexity is language-agnostic — it measures control flow, not syntax. Every language has if statements, loops, and logical operators. The thresholds (under 10 = good) apply universally. This tool analyzes JavaScript and TypeScript specifically.
How is cyclomatic complexity related to test coverage?
A function with CC=N requires at minimum N test cases to achieve full branch coverage. A function with CC=15 needs at least 15 tests to cover every path. This is why high-complexity functions are expensive to test thoroughly and prone to undertested edge cases.
Related Inspect Tools
Color Contrast Checker
Check WCAG 2.1 color contrast ratios for AA and AAA accessibility compliance
JSON Schema Validator
Validate JSON data against JSON Schema (Draft 07) with detailed error reporting and schema generation
IP / CIDR Toolkit
Subnet calculator, VLSM divider, IP range to CIDR converter, and IP address classifier
Open Graph Preview
Preview and debug Open Graph, Twitter Card, and SEO meta tags for social sharing