What is Cyclomatic Complexity?
Cyclomatic complexity is a software metric that measures the number of linearly independent paths through a piece of code, derived from its control-flow graph, giving a quantitative indicator of how complex the code is and roughly how many test cases are needed to cover its branches.
Free to start · 7-day trial on paid plans
In depth.
Introduced by Thomas McCabe, cyclomatic complexity counts the decision points in code (if, while, for, case, and logical operators) to estimate how many distinct paths run through it. A simple formula is complexity = number of decision points + 1. A function with no branches has complexity 1; each added decision increases it. Higher numbers mean more paths, more ways to go wrong, and more tests needed to exercise them.
For testers, the metric is doubly useful. First, it signals risk: highly complex functions are harder to understand, more defect-prone, and harder to maintain, so they are good candidates for extra testing, review, or refactoring. Second, it informs coverage: the cyclomatic complexity number is the minimum number of test cases needed to achieve full branch (basis path) coverage of that code, so it directly relates to how much testing a function requires.
It is part of static analysis and maintainability assessment, not something you run the software to measure. Teams often set thresholds (for example, flag functions above 10) to catch complexity creep early. The metric does not capture everything (it ignores data complexity and readability), so it is a useful signal among several, not a sole measure of quality.
Why interviewers ask about this.
Cyclomatic complexity connects code structure to test effort and risk, a sign of deeper, SDET-level understanding. Knowing that it estimates independent paths (and thus minimum tests for branch coverage) and flags refactoring candidates shows you can use metrics to target testing where it matters.
Example scenario.
Static analysis flags a payment-routing function with cyclomatic complexity 18, far above the team's threshold of 10. It has many nested conditions, so it is both hard to fully test and defect-prone. The team prioritizes refactoring it into smaller functions and ensures each branch is covered, reducing both complexity and risk.
Interview tip.
Define cyclomatic complexity as a count of independent paths through code (roughly decision points + 1) from its control-flow graph. Tie it to testing: it estimates the minimum test cases for full branch coverage and flags complex, defect-prone functions for extra testing or refactoring. Note it is a static-analysis signal, not the whole picture.
Frequently asked questions.
How does cyclomatic complexity relate to test coverage?
The cyclomatic complexity number equals the minimum number of test cases needed to cover every independent path (basis path / full branch coverage) through that code. So a function with complexity 8 needs at least 8 well-chosen test cases to exercise all its branches, directly linking the metric to required test effort.
What is a "good" cyclomatic complexity value?
There is no universal threshold, but many teams treat values up to about 10 as manageable and flag anything significantly higher as complex and defect-prone, warranting extra testing, review, or refactoring. It is a guideline and a signal among several metrics, not a hard rule or a complete measure of quality.
Related Terms
Explore related glossary terms to deepen your understanding.
Related Resources
Dive deeper with these related interview prep pages.
Free QA career tools, no account needed
Instant and private, everything runs in your browser. Try them before you sign up.
QA Resume Checker
Instant 0-100 score on automation keywords, impact, and ATS formatting.
QA Cover Letter Generator
A tailored 3-paragraph QA cover letter from your resume and a job post.
QA Application Tracker
Drag-and-drop kanban to track every QA application from Applied to Offer.
QA Take-Home Test Generator
A realistic take-home assignment with a scenario, tasks, and a rubric.
QA LinkedIn Headline Generator
A recruiter-searchable headline, About section, and skills list.
QA STAR Story Builder
Structure a QA behavioral answer with the STAR method and instant checks.
QA Bug Report Generator
Build a clean, reproducible bug report for Markdown, Jira, or plain text.
Boundary Value Analysis Generator
Generate boundary value and equivalence partitioning test cases from a range.
QA Metrics Calculator
Calculate DRE, defect leakage, defect density, and pass rate with interpretation.
QA Test Plan Generator
Build a structured test plan (scope, approach, criteria, risks) in Markdown.
Ready to Ace Your QA Interview?
Practice explaining cyclomatic complexity and other key concepts with our AI interviewer.
Join 1,200+ QA engineers already practicing with AssertHired.
Start your free QA interview