Information Technology Reference
In-Depth Information
the number of “New Line” hits in the file excluding comments, blank lines, and lines
with only delimiters.
5.3.1
McCabe's Cyclomatic Number
The cyclomatic complexity of a section of source code is the count of the number of
linearly independent paths through the source code. For instance, if the source code
contained no decision points such as IF statements or FOR loops, the complexity
would be 1 because there is only a single path through the code. If the code has
a single IF statement containing a single condition, then there would be two paths
through the code: one path where the IF statement is evaluated as TRUE, and one
path where the IF statement is evaluated as FALSE.
This is a complexity metric. The premise is that complexity is related to the control
flow of the software. Using graph theory (e.g., control flow graphs), we can calculate
the cyclomatic number ( C ) as follows:
C
=
e
n
+
1
(5.1)
where e is the number of arcs and n is the number of nodes.
McCabe uses a slightly different formula
C
=
e
n
+
2 p
(5.2)
where p is the number of strongly connected components (usually assumed to be 1).
In a control flow graph, each node in the graph represents a basic block (i.e., a
straight-line piece of code without any jumps or jump targets; jump targets start a
block, and jumps end a block). Directed edges are used to represent jumps in the
control flow. There are, in most presentations, two specially designated blocks: the
entry block, through which control enters into the flow graph, and the exit block,
through which all control flow leaves. The control flow graph is essential to many
compiler optimizations and static analysis tools.
For a single program (or subroutine or method), p is always equal to 1. Cyclomatic
complexity may, however, be applied to several such programs or subprograms at the
same time (e.g., to all methods in a class), and in these cases, p will be equal to the
number of programs in question, as each subprogram will appear as a disconnected
subset of the graph.
It can be shown that the cyclomatic complexity of any structured program with
only one entrance point and one exit point is equal to the number of decision points
(i.e., “if ” statements or conditional loops) contained in that program plus one (Belzer
et al., 1992).
Cyclomatic complexity may be extended to a program with multiple exit points;
in this case, it is equal to
+
s
2
(5.3)
Search WWH ::




Custom Search