Software Complexity Metrics

Simulation
Introduced by
Thomas J. McCabe
Year
1976
Type
quantitative measurement
Family
Code quality assessment

What does it do?

Software complexity metrics are numerical measures of how structurally difficult code is to understand, test and change.

The best-known example is McCabe's cyclomatic complexity, introduced in 1976, which is derived from the decision structure of a program's control flow graph. Teams use such measures to flag code regions that may need closer review, testing or refactoring.

The intuition behind the method

Code tends to become harder to follow as it accumulates branches, loops, nested logic and dependencies between modules. Metrics turn this impression into counts, such as decision points, independent paths through a function, nesting depth, or links between classes. A high count does not prove that a defect exists, but it marks where reasoning about the code becomes more demanding. Because metrics are proxies for difficulty, their meaning depends on what is counted and why.

History

Thomas McCabe introduced cyclomatic complexity in 1976, defining it on the control flow graph of a program. Chidamber and Kemerer published a metrics suite for object-oriented design in 1994, which includes measures such as WMC, DIT, CBO and RFC, among others. Later analysis tools, such as SonarQube, CAST and Understand, automated many of these measures for industrial use.

When to use it

Use complexity metrics when you need a consistent, automatable way to locate code that may be hard to test or maintain, for example to prioritise test effort, to review risky changes in a continuous integration pipeline, or to get an overview of an unfamiliar legacy codebase. They suit code whose control flow and class structure can be parsed reliably. Do not use them as a substitute for functional correctness testing, runtime performance analysis or an assessment of domain difficulty, and do not adopt a threshold as a quality standard until it has been checked against your own defect or maintenance records. McCabe's measure deliberately ignores the number of statements, so size measures such as lines of code are usually read alongside structural measures rather than replaced by them.

Limitations and considerations

  • Different metrics capture different properties, such as decision structure, size or coupling, and there is no agreed single set that suits every purpose.
  • Cyclomatic complexity does not register data complexity, algorithmic cost or the conceptual difficulty of the problem domain.
  • Language constructs change what is counted, so values from different languages, or from a polyglot system, are not directly comparable without care.
  • The relationship between metric values and defects or maintenance effort varies between projects, so an association estimated in one codebase cannot be assumed for another.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Once the counting rules are fixed, two analysts applying the same rules with the same parser should obtain the same value for a given function.
  • McCabe's graph-theoretic measure depends only on the decision structure of a program, not on the number of functional statements, as shown in the original paper.
  • The Chidamber and Kemerer suite was designed for object-oriented design and was accompanied by an automated collection tool, which allows its measures to be gathered across class structures without manual counting.

Applications

  • Prioritising code review and test effort for modules with high decision-structure complexity, particularly in safety-critical or regulated software where the number of independent paths bears on how much testing is needed.
  • Flagging high-complexity functions in a continuous integration pipeline so that reviewers see them when they change.
  • Assessing an inherited legacy codebase to locate the modules most in need of refactoring before further development.
  • Supplying input features for defect-risk models, where metric values are combined with historical defect records and the model is checked on data not used to build it.

How it works

How it works

1

Collect the source code to be measured, usually from a version control repository or a release snapshot. Most structural metrics require the code to be parsed into an abstract syntax tree (AST) or a control flow graph.

2

Parse the code with a language-specific front end so that the same counting rules apply to every module. Generated code, third-party libraries and test code are often excluded or analysed separately, because they can distort the results.

3

Compute metric values such as cyclomatic complexity (for a control flow graph, M = E - N + 2P, where E is edges, N is nodes and P is connected components), lines of code, nesting depth, coupling and cohesion. Each metric counts a different structural property, so they are computed separately and not merged at this stage.

4

Relate metric values to observed outcomes, such as defect counts or maintenance effort, where historical data exists. Pair each metric with the outcome it is meant to predict, and estimate the association from the team's own records; a metric value on its own does not establish a defect probability. Check any fitted relationship on data that was not used to estimate it before relying on it.

5

Compare results with thresholds or rankings chosen for the context and flag modules that deviate from them. Flags direct human attention to code for review; they are not verdicts on the quality of that code.

Common pitfalls

  • Treating a single metric as definitive. Use several measures that capture different properties.
  • Adopting threshold values by convention without checking them against your own defect or change history. Commonly quoted ranges are starting points only.
  • Comparing raw values across teams or languages whose counting rules or coding styles differ.
  • Penalising deliberate complexity. A dispatch table or state machine can have a high decision count and still be easy to read, so a flag should prompt inspection rather than automatic refactoring.

Frequently asked questions

What is cyclomatic complexity and how do I calculate it?

Cyclomatic complexity counts the number of linearly independent paths through code using the formula 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. For a single function, a common shortcut is to count the decision points (such as if, while, for and case) and add 1. Higher values mean more independent paths, so more test cases are needed to cover them.

What complexity threshold should I enforce in my codebase?

No universal threshold exists. Tools and guides often propose conventional bands of complexity values, but these are not validated limits for any particular codebase. Measure your current codebase, compare the values with defect or change history where you have it, and set limits that match your team's tolerance for risk. A tool's default setting is not evidence that a threshold is appropriate for your code.

How do complexity metrics relate to defects and maintenance cost?

Higher complexity is associated with greater defect risk and greater maintenance effort, but the strength of that association varies between projects, so it should be estimated from your own records rather than assumed. Complex code also takes more cognitive effort to read, debug and modify, which adds to maintenance labour. The metrics are therefore most useful for ranking modules relative to one another within a codebase.

Can I use complexity metrics for performance prediction?

Complexity metrics measure structural difficulty, not runtime performance. They are distinct from algorithmic complexity (Big O notation), which describes how the cost of an algorithm grows with its input. Use structural metrics to judge maintainability and algorithmic analysis to reason about running time. Hard-to-read code can still run efficiently, and an efficient algorithm can be hard to maintain.

Related methods

Sources

  1. McCabe, T. J. (1976). A complexity measure. IEEE Transactions on Software Engineering, 2(4), 308–320.

    Open DOI reference
  2. Chidamber, S. R., & Kemerer, C. F. (1994). A metrics suite for object-oriented design. IEEE Transactions on Software Engineering, 20(6), 476–493.

    Open DOI reference
  3. Halstead, M. H. (1977). Elements of Software Science. Elsevier.

    Open publication reference