FAULT PRONENESS
14.1 FAULT PRONENESS
What makes a software product prone to faults is its complexity; hence complexity metrics are adequate measures of fault proneness. Empirical studies routinely show
a significant correlation between complexity metrics and fault density in software pro- ducts. In this section, we present two widely known, routinely used metrics of struc- tural complexity of control structures.
14.1 FAULT PRONENESS 315
14.1.1 Cyclomatic Complexity
In its simplest expression, the cyclomatic complexity of a program reflects the com- plexity of its control structure and is computed by drawing the flowchart (say, F) of the program, then deriving the value of the expression as:
v F = e −n + 2,
where • e is the number of edges in F and
• n is the number of nodes in F. We know from graph theory that if the flowchart is a planar graph (i.e., a graph that
can be drawn on a planar surface without any two edges crossing each other) then v (F) represents the number of regions in F. Also, note that if the flowchart is merely a linear sequence of nodes, then its cyclomatic complexity is 1, regardless of the number of nodes; hence a single statement (corresponding to e = 0 and n = 1) has the same cyclo- matic complexity as a sequence of 100 statements (e = 99 and n = 100). This is con- sistent with our intuitive understanding of complexity as an orthogonal attribute to size: a sequence of 100 statements is longer than a single statement but is not more complex. As an illustration of this metric, we consider the following sample example:
int product (int a, int b) { int c; c=0; while (b!=0)
{if (b%2==0) {b=b/2; a=2*a;} else
{b=b-1; c=c+a;}}
return c; }
The flowchart of this program is given in Figure 14.1. From this flowchart, we compute the number of nodes and edges, and we find:
• e=7, • n=6
whence
vF=3
Exercises 1 and 2 in Section 14.8 explore how this figure is affected after we make the program more complex by additional tests.
316 METRICS FOR SOFTWARE TESTING
int c; c=0;
b!=0
int c; c=0;
b%2==0
b=b/2; a=2*a;
b=b–1; c=c+a;
Figure 14.1 Counting nodes and edges.
14.1.2 Volume
Whereas cyclomatic complexity measures the complexity of a program by consider- ing the density of its control structure, the metric of program volume measures complexity by the amount of intellectual effort that it took to develop the program. We can argue that unlike the cyclomatic complexity, which measures structure inde- pendently of size, program volume captures size along with structural complexity. Specifically, program volume is computed as follows:
• If N is the number of lexemes (operators, operands, symbols, constants, etc.) in a program and • n is the number of distinct lexemes (operators, operands, symbols, constants, etc.) in the program,
then the volume of the program is given by the following formula:
V = N × log 2 n
Interpretation: n is the size of the vocabulary in which the program is written; log 2 (n) measures the number of binary decisions one has to make to select one symbol in a vocabulary of size n; N × log 2 n measures the total development effort of the program, viewed as the selection of N symbols in a vocabulary of size n, and quanti- fied in terms of binary decisions. Alternatively, log 2 (n) can be interpreted as the entropy of the random variable that represents each symbol of the program and N × log 2 n is then the entropy of the whole program (quantity of information contained within its source text). If we consider again the product program given above, we find the following quantities:
14.2 FAULT DETECTABILITY 317
• Number of lexemes, N: 66. • Vocabulary: {int, product, (, a, ‘,’, b, ), {, c, ;, =, 0, while, !=, if, %, 2, ==, /, *, },
else, −, 1, +, return}. Hence n=26. The volume of this program is:
V = 66 × log 2 26 = 310 23 bits
Generating this program requires the equivalent of 310 binary (yes/no) decisions.