4.4 McCabe’s cyclomatic complexity metrics tegies
a 9.4.4 McCabe’s cyclomatic complexity metrics tegies
The cyclomatic complexity metrics developed by McCabe (1976) measures the complexity of a program or module at the same time as it determines the maximum number of independent paths needed to achieve full line cov- erage of the program. The measure is based on graph theory and is thus calculated according to the program characteristics as captured by its pro- gram flow graph.
An independent path is defined with reference to the succession of inde- pendent paths accumulated, that is: “Any path on the program flow graph that includes at least one edge that is not included in any of the former inde- pendent paths”.
To illustrate this definition, let us refer once again to Figure 9.3. A set of independent paths that achieves full line coverage of the program is listed in Table 9.5.
Table 9.4: The Imperial Taxi example – the minimum number of paths No.
The path 1 1-2-3-5-6-8-9-11-12-17
23 1-2-4-5-7-8-10-11-13-14-15-17 24 1-2-4-5-7-8-10-11-13-14-16-17
Table 9.5: The ITS example – the set of independent paths to achieve full coverage
Path The path Edges added by the path Number of edges no.
added by the path 1 1-2-3-5-6-8-9-11-12-17
1-2, 2-3, 3-5, 5-6, 6-8, 8-9, 9
9-11, 11-12, 12-17
2 1-2-3-5-6-8-9-11-13-14-15-17 11-13, 13-14, 14-15, 15-17 4 3 1-2-3-5-6-8-9-11-13-14-16-17
2 4 1-2-4-5-7-8-10-11-13-14-15-17
14-16, 16-17
2-4, 4-5, 5-7, 7-8, 8-10, 10-11 6
As mentioned above, the cyclomatic complexity metric V(G) also deter- 195 mines the maximum number of independent paths that can be indicated in
the program flow graph.
The cyclomatic complexity metric (V(G)) is expressed in three different ways, all of which are based on the program flow graph:
White bo
(1) V(G) = R (2) V(G) = E – N + 2
tes ting
(3) V(G) = P + 1
In these equations R is the number of regions in the program flow graph. Any enclosed area in the graph is considered a region. In addition the area around the graph not enclosed by it is counted as one additional region. E is the number of edges in the program flow graph, N is the number of nodes in the program flow graph, and P is the number of decisions contained in the graph, represented by nodes having more than one leaving edge.
Example Applying the above to the ITS taximeter module example described above, we can obtain the values of the above parameters from Figure 9.3. We find that R = 6, E = 21, N = 17, and P = 5. Substituting these values into the met- rics formulae we obtain:
(1) V(G) = R = 6 (2) V(G) = E – N + 2 = 21 – 17 + 2 = 6 (3) V(G) = P + 1 = 5 + 1 = 6
The resulting metrics calculations indicate that the maximum number of independent paths in the example is six. One realization of a maximal set of six independent paths is shown in Table 9.6.
Several empirical studies of the relationships between the cyclomatic complexity metrics and quality and testability characteristics have been car-
Table 9.6: The ITS example – the maximum set of independent paths Path The path
Number of edges no.
Edges added by the path
added by the path 1 1-2-3-5-6-8-9-11-12-17
1-2, 2-3, 3-5, 5-6, 5-8, 8-9,
9-11, 11-12, 12-17
2 1-2-4-5-6-8-9-11-12-17
2 3 1-2-3-5-7-8-9-11-12-17
2-4, 4-5
2 4 1-2-3-5-6-8-10-11-12-17
5-7, 7-8
2 5 1-2-3-5-6-8-9-11-13-14-15-17 11-13, 13-14, 14-15, 15-17
8-10, 10-11
4 6 1-2-3-5-6-8-9-11-13-14-16-17 14-16, 16-17
196 ried out over the years. Some of the findings are summarized by Jones (1996) as follows: “Empirical studies reveal that programs with cyclomat-
9 ic complexities of less than 5 are generally considered simple and easy to Softw
understand. Cyclomatic complexities of 10 or less are considered not too difficult; if 20 or more, the complexity is perceived as high. When the
are tes
McCabe value exceeds 50, the software for practical purposes becomes untestable.” Other publications report no confirmation of the relationship between the cyclomatic complexity metrics and the quality of the software,
ting – str
or that the relationships found have not been supported statistically (Fenton, 1995, pp. 279–281).