A Complexity Measure
A Complexity Measure
A Complexity Measure
4, DECEMBER 1976
A Complexi-ty Measure
THOMAS J. MCCABE
Authorized licensed use limited to: University of London: Online Library. Downloaded on May 14,2023 at 10:53:09 UTC from IEEE Xplore. Restrictions apply.
MC CABE: A COMPLEXITY MEASURE 309
Theorem 1 is applied to G in the following way. Imagine that CONTROL STRUCTURE CYCLOMATIC COMPLEXITY
*v = e - n + 2p
the exit node (f) branches back to the entry node (a). The
SEQUENCE v =1 - 2 + 2 =1
control graph G is now strongly connected (there is a path
joining any pair of arbitrary distinct vertices) so Theorem 1 IF THEN ELSE v = 4 - 4 + 2 = 2
Authorized licensed use limited to: University of London: Online Library. Downloaded on May 14,2023 at 10:53:09 UTC from IEEE Xplore. Restrictions apply.
310 IEEE TRANSACTIONS ON SOFTWARE EN(GINEERING, DECEMBER 1976
V(G)=3
CONNECTIVITY MATRIX
1 2 3 4 5 6 7
OOOOO1O
1 011 0 0 0 0
2 0
23 O 1 0 0 0
4 0 0 0 1 1 0 0
5 0 0 0 0 01
6 0 0 0 0 0 0 1
7 0 000000 1 6 5
1 2 3 4 5 6 7
1 0 1 1 1 1 1 1
2 0 0 0 0 0 1 1
3 0 0 0 1 1 1 1
4 0 0 0 1 1 1 1 7
5 0 0 0 0 0 1 1
6 0 0 0 0 0 0 1
7 0000000 8
,END
V(G)=6
At this point a few of the control graphs that were found in
live programs will be presented. The actual control graphs
from FLOW appear on a DATA DISK CRT but they are hand
drawn here for purposes of illustration. The graphs are pre-
sented in increasing order of complexity in order to suggest
the correlation between the complexity numbers and our in-
tuitive notion of control flow complexity.
Authorized licensed use limited to: University of London: Online Library. Downloaded on May 14,2023 at 10:53:09 UTC from IEEE Xplore. Restrictions apply.
MC CABE: A COMPLEXITY MEASURE 311
Authorized licensed use limited to: University of London: Online Library. Downloaded on May 14,2023 at 10:53:09 UTC from IEEE Xplore. Restrictions apply.
312 IEEE TRANSACTIONS ON SOFTWARE ENGINEERING, DECEMBER 1976
Authorized licensed use limited to: University of London: Online Library. Downloaded on May 14,2023 at 10:53:09 UTC from IEEE Xplore. Restrictions apply.
MC CABE: A COMPLEXITY MEASURE 313
One of the more interesting aspects of the automatic approach is that although FLOW could be implemented much more effi-
ciently in a compiler level language, it is still possible to go through a year's worth of a programmer's Fortran code in about 20
min. After seeing several of a programmer's control graphs on a CRT one can often recognize "style" by noting similar patterns
in the graphs. For example, one programmer had an affinity for sequencing numerous simple loops as in
V(G) =10
Authorized licensed use limited to: University of London: Online Library. Downloaded on May 14,2023 at 10:53:09 UTC from IEEE Xplore. Restrictions apply.
314 IEEE TRANSACTIONS ON SOFTWARE ENGINEERING, DECEMBER 1976
It was later revealed that these programs were eventually to ponents2 as MUAUB. Now, since p = 3 we calculate complex-
run on a CDC6600 and the "tight" loops were designed to stay ity as
within the hardware stack.
These results have been used in an operational environment v(MUAUB)=e- n+2p= 13- 13+2X3=6.
by advising project members to limit their software modules This method with p*l can be used to calculate the complex-
by cyclomatic complexity instead of physical size. The par- ity of a collection of programs, particularly a hierarchial nest
ticular upper bound that has been used for cyclomatic com- of subroutines as shown above.
plexity is 10 which seems like a reasonable, but not magical, Notice that v(MUAUB) = v(M) + v(A) + v(B) = 6. In general,
upper limit. Programmers have been required to calculate the complexity of a collection C of control graphs with k con-
complexity as they create software modules. When the com- nected components is equal to the summation of their com-
plexity exceeded 10 they had to either recognize and modularize plexities. To see this let Ci, l<i6k denote the k distinct con-
subfunctions or redo the software. The intention was to keep nected components, and let ei and ni be the number of edges
the "size" of the modules manageable and allow for testing all and nodes in the ith connected component. Then
the independent paths (which will be elaborated upon in
Section VII.) The only situation in which this limit has seemed k k
v(C) = e - n + 2p= ei - ni +2k
unreasonable is when a large number of independent cases 1 1
followed a selection function (a large case statement), which
was allowed. k k
It has been interesting to note how individual programmer's =E(ei
1
- ni +2) = Ev(Q.)
1
style relates to the complexity measure. The author has been
delighted to find several programmers who never had formal V. SIMPLIFICATION
training in structured programming but consistently write Since the calculation v = e - n + 2p can be quite tedious for
code in the 3 to 7 complexity range which is quite well struc- a programmer an effort has been made to simplify the com-
tured. On the other hand, FLOW has found several program- plexity calculations (for single-component graphs). There are
mers who frequently wrote code in the 40 to 50 complexity two results presented in this section-the first allows the com-
range (and who claimed there was no other way to do it). On plexity calculations to be done in terms of program syntactic
one occasion the author was given a DEC tape of 24 Fortran constructs, the second permits an easier calculation from the
subroutines that were part of a large real-time graphics system. graph form.
It was rather disquieting to fmd, in a system where reliability In [7] Mills proves the following: if the number of function,
is critical, subroutines of the following complexity: 16, 17, predicate, and collecting nodes in a structured program is 0, r,
24, 24, 32, 34, 41, 54, 56, and 64. After confronting the and y, respectively, and e is the number of edges, then
project members with these results the author was told that
the subroutines on the DEC tape were chosen because they e = 1 + 0 + 3ir.
were troublesome and indeed a close correlation was found Since for every predicate node there is exactly one collecting
between the ranking of subroutines by complexity and a rank- node and there are unique entry and exit nodes it follows that
ing by reliability (performed by the project members).
n = 0 + 2ir + 2.
IV. DECOMPOSITION
Assuming p = 1 and substituting in v = e - n + 2 we get
The role of p in the complexity calculation v =e - n + 2p
will now be explained. Recall in Definition 1 that p is the v=(l +0 +3rr)- (0 + 2ir+ 2)+2 =r+ 1.
number of connected components. The way we defined a pro- This proves that the cyclomatic complexity of a structured
gram control graph (unique entry and exit nodes, all nodes program equals the number of predicates plus one, for exam-
reachable from the entry, and the exit reachable from all ple in
nodes) would result in all control graphs having only one con-
nected component. One could, however, imagine a main pro-
gram M and two called subroutines A and B having a control
structure shown below:
A:
M:
B:
Authorized licensed use limited to: University of London: Online Library. Downloaded on May 14,2023 at 10:53:09 UTC from IEEE Xplore. Restrictions apply.
MC CABE: A COMPLEXITY MEASURE 315
complexity v(G) = IT + 1 = 3 + 1 = 4. Notice how in this case written with only these constructs. One of the difficulties
complexity can be computed by simply counting the number with this approach is it does not define for programmers what
of predicates in the code and not having to deal with the con- constructs they should not use, i.e., it does not tell them what
trol graph. a structured program is not. If the programming population
In practice compound predicates such as IF "Cl AND C2" had a notion of what constructs to avoid and they could see
THEN are treated as contributing two to complexity since the inherent difficulty in these constructs, perhaps the notion
without the connective AND we would have of structuring programnming would be more psychologically
IF Cl THEN IF C2 THEN palatable. A clear defmition of the constructs that structured
which has two predicates. For this reason and for testing pur- programming excludes would also sensitize programmers to
their use while programs are being created, which (if we be-
poses it has been found to be more convenient to count con- lieve in structured programming) would have a desirable effect.
ditions instead of predicates when calculating complexity.3 One of the reasons that the author thinks this is important
It has been proved that in general the complexity of any (un- is that as Knuth [4] points out-there is a time and a place
structured) program is fT + 1. when an unstructured goto is needed. The author has had a
The second simplification of the calculation of e - n + 2p similar experience structuring Fortran jobs-there are a few
reduces the calculation of visual inspection of the control very specific conditions when an unstructured construct works
graph. We need Euler's formula which is as follows. If G is a best. If it is the case that unstructured constructs should only
connected plane graph with n vertices, e edges, and r regions, be allowed under special circumstances, one need then to dis-
then tinguish between the programmer that makes judicious use of
n - e + r = 2. a few unstructured goto's as compared to the programmer that
Just changing the order of the terms we get r = e - n + 2 so the is addicted to them. What would help is first the definition of
number of regions is equal to the cyclomatic complexity. the unstructured components and second a measure of the
Given a program with a plane control graph one can therefore structureness of a program as well as the complexity of a
calculate v by counting regio-ns, as in program.
Rao Kasaraju [5] has a result which is related-a flow graph
is reducible5 to a structured program if and only if it does not
contain a loop with two or more exits. This is a deep result
G~~~~~
but not, however, what we need since many programs that are
reducible to structured programs are not structured programs.
In order to have programmers explicitly identify and avoid
v (G) = 5 unstructured code we need a theorem that is analogous to a
theorei like Kuratowski's theorem in graph theory. Kuratow-
ski's theorem states that any nonplanar graph must contain at
least one of two specific nonplanar graphs that he describes.
The proof of nonplanarity of a graph is then reducible to
VI. NONSTRUCTURED PROGRAMMING locating two specific subgraphs whereas showing nonplanarity
The main thrust in the recent popularization of structured without Kuratowski's result is, in general, much more difficult.
programming is to make programmers aware of a few syntactic The following four control structures were found to generate
constructs4 and tell them that a structured program is one all nonstructured programs.
3For the CASE construct with N cases use N-i for the number of A number of theorems and results will be stated below with-
conditions. Notice, once -again, that a simulation of case with IF'S WiU out proof.
have N-1 conditions.
4The usual ones used (sometines called D-structures) are Result 1: A necessary and sufficient condition that a pro-
gram6 is nonstructured (one that is not written with just
------
0-
5Reducibility here means the same function is computed with the
same actions and predicates although the control structure may differ.
6Assuming the program does not contain unconditional GOTO'S.
Authorized licensed use limited to: University of London: Online Library. Downloaded on May 14,2023 at 10:53:09 UTC from IEEE Xplore. Restrictions apply.
316 IEEE TRANSACTIONS ON SOFTWARE ENGINEERING, DECEMBER 1976
D-structures) is that it contains as a subgraph either a), b), is branched into. So in this case we have a c) graph along with
or c). the original b) graph.
The reason why graph d) was slighted in Result 1 is that any Case 2: E is "after" the loop. The control graph would ap-
3 of the 4 graphs will generate all the unstructured programs- pear as follows:
this will be illustrated later. It is convenient to verbalize the
graphs a)-d), respectively, as follows:
a) branching out of a loop;
b) branching into a loop; b)
c) branching into a decision; and
d) branching out of a decision. a)
The following version of Result 2 may seem more intuitively
appealing. Notice how a type a) graph must appear.
A structured program can be written by not "branching out Case 3: E is independent of the loop. The control graph
of or into a loop, or out of or into a decision." would look as follows:
The following result gives insight into why a nonstructured
program's logic often becomes convoluted and entwined.
Result 2: A nonstructured program cannot be just a little
nonstructured. That is any nonstructured program must con- b)
tain at least 2 of the graphs a)-d). Part of the proof of Result s d)
2 will be shown here because it helps to illustrate how the con-
trol flow in a nonstructured program becomes entangled. We
show, for an example, how graph b) cannot occur alone. As-
suming we have graph b): The graph c) must now be present with b). If there is another
path that can go to a node after the loop from E then a type
d) graph is also generated. Things are often this bad, and in
fact much worse.
Similar arguments can be made for each of the other non-
( 9 ~~~b) structured graphs to show that a)-d) cannot occur alone. If
one generates all the possible pairs from a)-d) it is interesting
to note that they all reduce to 4 basic types:
-~~~~~~ a
d
(a,b) (a,d) (b, c) (c,d) 0
rC
a' M
d
the entry node E occurs either before, after, or from a node which leads us the following result.
independent of the loop. Each of these three cases will be Result 3: A necessary and sufficient condition for a pro-
treated separately. gram to be nonstructured is that it contains at least one of: (a,
Case 1: E is "before" the loop E is on a path from entry to b), (a, d), (b, c), (c, d). Result 4 is now obvious.
the loop so the program must have a graph as follows: Result 4: The cyclomatic complexity if a nonstructured
program is at least 3. It is interesting to notice that when the
orientation is taken off the edges each of the 4 basic graphs
a)- d) are isomorphic to the following nondirected graph.
c)
\) b)
Authorized licensed use limited to: University of London: Online Library. Downloaded on May 14,2023 at 10:53:09 UTC from IEEE Xplore. Restrictions apply.
MC CABE: A COMPLEXITY MEASURE 317
Also if the graphs (a, b) through (c, d) have their directions Notice in the nonstructured graphs below, however, that such
taken off they are all isomorphic to: a reduction process is not possible.
GI: G2:
v=6 v=6
/\ ,
--Nm c
v = 4 v = 3 v = 2 v = 1
Authorized licensed use limited to: University of London: Online Library. Downloaded on May 14,2023 at 10:53:09 UTC from IEEE Xplore. Restrictions apply.
318 IEEE TRANSACTIONS ON SOFTWARE ENGINEERING, DECEMBER 1976
G2':
v(G2') =5 Suppose that ac = 2 and the two tested paths are [E, a,, b, c2,
x] and [E, a2, b, cl, x]. Then given that paths [E, a,, b, cl,
x] and [E, a2, b, c2, x] cannot be executed we have ac<v
so case 2 holds and G can be reduced by removing decision b
as in
G3':
GI:
2 C
v(G3') =4
Notice how in G v = ac and the complexity of Gl is less than
the complexity of G.
This last result is stated for completeness. In experience this approach is most helpful when program-
Result 8: The essential complexity of a structured program mers are required to document their flow graph and complex-
is one. ity and show explicitly the different paths tested. It is often
the case when the actual number of paths tested is compared
VII. A TESTING METHODOLOGY with the cyclomatic complexity that several additional paths
The complexity measure v is designed to conform to our in- are discovered that would normally be overlooked. It should
tuitive notion of complexity and since we often spend as much be noted that v is only the minimal number of independent
as 50 percent of our time in test and debug mode the measure paths that should be tested. There are often additional paths
should correlate closely with the amount of work required to to test. It should also be noted that this procedure (like any
test a program. In this section the relationship between test- other testing method) will by no means guarantee or prove the
ing and cyclomatic complexity will be defined and a testing software-all it can do is surface more bugs and improve the
methodology will be developed. quality of the software.
Let us assume that a program P has been written, its com- Two more examples are presented without comment.
plexity v has been calculated, and the number of paths tested
is ac (actual complexity). If ac is less than v then one of the
following conditions must be true:
1) there is more testing to be done (more paths to be tested);
2) the program flow graph can be reduced in complexity by Gl:
v-ac (v-ac decisions can be taken out); and
3) portions of the program can be reduced to in line code
(complexity has increased to conserve space). TESTS:
Authorized licensed use limited to: University of London: Online Library. Downloaded on May 14,2023 at 10:53:09 UTC from IEEE Xplore. Restrictions apply.
MC CABE: A COMPLEXITY MEASURE 319
G2: GG2':
TESTS:
acdfghik
acefgijabk
v=3
ac=2
ac-2
_ G2 @:(
v=2
(sg7 ac=2
Authorized licensed use limited to: University of London: Online Library. Downloaded on May 14,2023 at 10:53:09 UTC from IEEE Xplore. Restrictions apply.
320 IEEE TRANSACTIONS ON SOFTWARE ENGINEERING, DECEMBER 1976
Authorized licensed use limited to: University of London: Online Library. Downloaded on May 14,2023 at 10:53:09 UTC from IEEE Xplore. Restrictions apply.