McCabe Metrics

[Français][Deutsch]

Measurement of McCabe Metrics with Testwell CMT++ (Complexity Measures Tool)

Purpose and Origin

Cyclomatic complexity (also referred as program complexity or McCabe´s complexity) is the most widely used member of a class of static software metrics. It may be considered a broad measure of soundness and confidence for a program.
The cyclomatic complexity has been introduced by Thomas McCabe in 1976. It measures the number of linearly-independent paths through a program module.
The McCabe complexity is one of the more widely-accepted software metrics, it is intended to be independent of language and language format.

Interpreting McCabe Metrics

McCabe's Cyclomatic number v(G) shows the complexity of the flow of control through a piece of code.
v(G) is the number of conditional branches in the flowchart. v(G) = 1 for a program consisting of only sequential statements.

For a single function; v(G) is one less than the number of conditional branching points in the function.
The greater the cyclomatic number is the more execution paths there are through the function, and the harder it is to understand. Note, that the cyclomatic number is insensitive to the complexity of data structures, data flows, and module interfaces.

When dynamic testing is concerned, the cyclomatic number v(G) is one of the most important complexity measures.
Because the cyclomatic number describes the control flow complexity, it is obvious that modules and functions having high cyclomatic number need more test cases than modules having a lower cyclomatic number. As a rule of thumb, each function should have at least as many test cases as indicated by its cyclomatic number.

The cyclomatic number of a function should be less than 15. If a function has a cyclomatic number of 15, there are at least 15 (but probably more) execution paths through it.
More than 15 paths are hard to identify and test. Functions containing one selection statement with many branches make up an exception.

A reasonable upper limit Cyclomatic number of a file is 100.

How McCabe Metrics are calculated with CMT++

McCabe Cyclomatic number v(G) is calculated on (member) function definitions and on class/struct declarations. It is also calculated on the entire source file.

Each if-statement introduces a new branch to the program and therefore increases v(G) by one.
Iteration constructs such as for- and while-loops also introduce branches. Each case ...: part in the switch-statement increase the v(G) by one.
Case branch does not increase v(G), because it does not increase the number of branches in the control flow. If there are two or more case ...: parts that have no code in between, the McCabe measure is increased only with one for all those case ...: parts.
Each catch (...) part in a try-block increases v(G) by one.
Construction exprl ? expr2 : expr3 increases v(G) by one.

It should be noted that v(G) is insensitive to unconditional branches like goto-, return- and break-statements although they surely increase complexity.

In CMT++ the branches generated by conditional compilation directives are also counted to v(G). Even if conditional compilation directives do not add branches to the control flow of the executable program, they increase the complexity of the program file that the user sees and edits.

In summary, the following language constructs increase the cyclomatic number by one: if (...), for (...), while (...), case ...:, catch (...), &&, ||, ?, #if, #ifdef, #ifndef, #elif.

further information about CMT++


last updated: 13.01.2005

© 1996-2005 Testwell Oy / Verifysoft Technology GmbH
CTA++, CTC++, CMT++ and CMTJava are products of Testwell Oy, Tampere (Finland)
all other trademarks of this site are the property of their respective owners.