Number of Paths
Estimated static path count
Description
Note
Use Bug Finder instead of Code Prover for computing code metrics. Support for computing code metrics in Code Prover will be removed in a future release. See Version History.
This metric measures the number of static paths in a function.
The recommended upper limit for this metric is 80. If the number of paths is high, the code is difficult to read and can cause more orange checks. Try to limit the value of this metric.
To enforce limits on metrics, see Compute Code Complexity Metrics Using Polyspace.
Computation Details
The number of paths is calculated according to these rules:
If the statements in a function do not break the control flow, the number of paths is one.
Even an empty statement such as
;
or empty block such as{}
counts as one path.A control flow statement introduces branches and adds to the original one path.
if-else if-else
: Eachif
keyword introduces a new branch. The contribution from anif-else if-else
block is the number of branches plus one (the original path). If a catch-allelse
is present, all paths go through the block; otherwise, one path bypasses the block.For instance, a function with an
if(..) {} else if(..) {} else {}
statement has three paths. A function with oneif() {}
only has two paths, one that goes through theif
block and one that bypasses the block.switch-case
: Eachcase
label introduces a new branch. The contribution from aswitch
block is the number ofcase
labels plus one (the original path). If a catch-alldefault
is present, all paths go through the block; otherwise, one path bypasses the block.For instance, a function with a statement
switch (var) { case 1: .. break; case 2: .. break; default: .. }
has three paths, all going through theswitch
block. If you omit thedefault
, the function still has three paths, two going through theswitch
block and one bypassing the block.for
andwhile
: Each loop statement introduces a new branch. The contribution from a loop is two - a path that goes through the loop and a path that bypasses the loop.do-while
: Eachdo-while
statement introduces a new branch except when the condition of thewhile
statement is explicitly false. Statements written asdo{/*..*/}while(0)
do not function as loops. Such statements are often used for enclosing multiple lines of macros within braces. For instance, thisdo-while
statement serves to encapsulate the multiline macro rather than create a new path:Polyspace® considers such statements to be a single path.#define myfunc(x) do{ ...\\ ...\\ ...}while(0);
Note that a statement with a ternary operator such as
is not considered as a statement that breaks the control flow.result = a > b ? a : b;
If more than one control flow statement are present in a sequence without any nesting, the number of paths is the product of the contributions from each control flow statement.
For instance, if a function has three
for
loops and twoif-else
blocks, one after another, the number of paths is 2 × 2 × 2 × 2 × 2 = 32.If many control flow statements are present in a function, the number of paths can be large. Nested control flow statements reduce the number of paths at the cost of increasing the depth of nesting. For an example, see Function with Nested Control Flow Statements.
The software displays specific values in cases where the metric is not calculated:
If
goto
statements are present in the body of the function, Polyspace cannot calculate the number of paths and shows the number of paths asNot Computed
instead.If the number of paths reaches an upper limit of 1,000,000,000, Polyspace stops the calculation and displays just the upper limit. The actual value might be higher.
The number of path is calculated statically. This statically calculated number represents the upper limit of the possible path in a function. Typically, the number of possible path during run time is lower than the number Polyspace reports.
Polyspace ignores compiler generated conditional statements in C++ when calculating the number of paths. The compiler might generate implicit conditional statements in operations such as initializing static variables or calling virtual functions.
Examples
Metric Information
Group: Function |
Acronym: PATH |
HIS Metric: Yes |