- Introduced by
- Thomas McCabe
- Year
- 1976
- Type
- Control-flow complexity metric
- Family
- Software Metrics
What does it do?
Cyclomatic Complexity (CC), introduced by Thomas McCabe in 1976, is a graph-theoretic metric that counts the linearly independent paths through a function's control-flow graph.
McCabe's paper shows that this count depends only on a program's decision structure, not on its physical size. Many teams use a limit of 10 per function as a maintainability convention; that limit is a practice, not a value derived from the formula.
The intuition behind the method
Each decision point in a function (an if, a loop, a case branch) splits the flow of control, so the number of distinct routes through the function grows as decisions are added. CC summarises this as one number. For a function with one entry and one exit, CC equals the number of decision points plus one, so a function with no branches has CC = 1 and each added decision raises the count by one. CC does not count all possible execution paths, which can grow much faster; it counts the independent paths that a test set should aim to exercise. Higher values therefore indicate more branching to reason about and test.
History
Thomas McCabe's 1976 paper 'A Complexity Measure' in IEEE Transactions on Software Engineering defines the measure using graph theory and illustrates it on the control graphs of several Fortran programs. The paper reports a correlation between intuitive and graph-theoretic complexity in these examples and proves that the measure is independent of physical size and depends only on decision structure. Later use in testing and quality tools builds on this definition.
When to use it
Choose cyclomatic complexity when the question concerns how many independent paths a function contains: for example, to identify functions that need more test cases, to flag candidates for decomposition in code review, or to set a simple automated quality gate. It is best read alongside other measures, such as test coverage. Do not rely on it for code whose difficulty lies in data transformation rather than branching, such as long linear processing pipelines, where CC stays low regardless of effort. It is also not a measure of readability on its own; a separate measure such as cognitive complexity addresses that question.
Limitations and considerations
- It counts only control-flow branching. Complexity arising from data flow, such as a loop with an intricate invariant, can leave CC low.
- Sequential and nested conditions with the same number of decisions receive the same score, so the measure does not distinguish deeply nested logic from flat logic.
- Control transfers that are not shown in the control-flow graph, such as exceptions, callbacks or concurrency, are not reflected unless a tool models them.
- The threshold of 10 is a convention rather than a result of the formula, so teams should adopt and calibrate their own limit.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- The count follows directly from the control-flow structure, so the same counting rules applied to the same function give the same number.
- Because the measure ignores physical size, it isolates decision structure from line count, a property that McCabe's paper proves.
- Many static analysis tools compute it for a range of languages, so it can be enforced in continuous integration; each tool's counting rules should be checked before comparing figures.
- The metric is defined on a control-flow graph rather than on specific syntax, so the same counting idea applies to any language with branching constructs.
Applications
- Testing strategy: prioritising functions for unit testing, since functions with more independent paths need more test cases.
- Refactoring prioritisation: targeting functions above an agreed threshold for decomposition.
- Code review: flagging complex functions as candidates for closer inspection.
- Team training: using high-scoring functions as examples of branching that can be simplified with guard clauses or extracted helpers.
- Standards-driven projects: complexity figures can serve as supporting evidence in projects governed by a safety or quality standard. The standard itself should be checked for what it requires, since it may not call for this metric at all.
How it works
How it works
Represent the function as a control-flow graph in which nodes are statements or blocks of statements and edges are the possible transfers of control between them.
Identify the decision points: conditional branches, loop headers, case labels and, depending on the tool, exception handlers and short-circuit logical operators (&& and ||). Tools differ in which of these they count, so results are comparable only between tools that apply the same rules.
Compute CC = E - N + 2P, where E is the number of edges, N the number of nodes and P the number of connected components (P = 1 for a single function). For a single-entry, single-exit function this equals the number of decision points plus one. Worked example: a function containing one if statement (without else) and one while loop has a control-flow graph with seven nodes (start, the if condition, the then-block, the join, the loop header, the loop body and the exit) and eight edges (start to condition, condition to then-block, condition to join, then-block to join, join to loop header, header to body, body back to header, header to exit). CC = 8 - 7 + 2 = 3, which matches two decision points plus one. If the condition is written as a && b and the tool counts short-circuit operators, the condition splits into two nodes, so the graph gains one node and two edges. CC = 10 - 8 + 2 = 4, which again matches the decision count (if, &&, while) plus one. This CC is not the graph-theory circuit rank m - n + k, which is also called the cyclomatic number; the circuit rank equals CC - P and differs from CC by one for a single function.
Compare the result with the thresholds a team has adopted. The bands used in this text are up to 10 (acceptable), 11 to 20 (moderate risk) and above 20 (high risk). These are conventions, not outputs of the formula. The abstract of McCabe's 1976 paper does not specify a threshold, so any value proposed in the full paper should be checked there.
Common pitfalls
- Extracting branches into many very small functions lowers each function's CC but can spread the logic across the codebase; review the module as a whole as well.
- A low CC does not mean a function is easy to understand; deeply nested loops with complex conditions can remain hard to follow.
- Setting the limit too high removes its value as a warning. The bands above are a starting point, and teams should calibrate them against their own review or defect data.
- Penalising necessary complexity, such as a parser or state machine that must branch on many inputs, can lead to refactoring correct code. Focus on branches that are avoidable.
Frequently asked questions
Is cyclomatic complexity the same as the number of decision points?
For a single-entry, single-exit function, CC equals the number of decision points plus one, so a function with no branches has CC = 1. Each if, loop or case label adds one. Whether logical operators such as && and || add points depends on the tool.
How do I reduce cyclomatic complexity in a function?
Extract branches into helper functions, each of which starts with its own CC of 1. Replace long switch statements with polymorphism or a lookup where the design allows it. Use guard clauses (early returns) to reduce nesting. Combine conditions with && and || only where this does not make them harder to read.
Should I worry about CC in lambdas and short callbacks?
Usually not much: short lambdas and callbacks contribute little branching. Longer functions and the main logic paths deserve more attention.
How does CC relate to testing effort (number of test cases)?
CC is the number of linearly independent paths, so it indicates how many paths a basis set of tests would need to exercise. It does not guarantee full path coverage, because the number of possible execution paths can be much larger than CC. Boundary-value and error-case tests are needed in addition.
Related methods
Sources
McCabe, T. J. (1976). A complexity measure. IEEE Transactions on Software Engineering, SE-2(4), 308–320.
Open DOI referenceCampbell, G. H. (1986). Defining a good metric, a software testing perspective. ASQ Software Quality Conference.
Open publication referenceNagy, C., & Kriebel, K. (2001). Achieving optimal complexity and reliability. SAMS Publishing.
ISBN: 06723222852015_Book_DataMining, p. 573