21.08.2013 Views

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

360 Chapter 29 ■ <strong>Software</strong> metrics and quality assurance<br />

><br />

x = y;<br />

if (a == b)<br />

c = d;<br />

else<br />

e = f;<br />

p = q<br />

has a complexity of 2, because there are two independent paths through the program.<br />

Similarly a while and a repeat each count one towards the complexity count.<br />

Compound conditions like:<br />

if a > b and c > d then<br />

count two because this if statement could be rewritten as two, nested if statements.<br />

Note that a program that consists only of a sequence of statements, has a cyclomatic<br />

complexity of 1, however long it is. Thus the smallest value of this metric is 1.<br />

There are two ways of using McCabe’s measure. First, if we had two algorithms that<br />

solve the same problem, we could use this measure to select the simpler. Second, McCabe<br />

suggests that if the cyclomatic complexity of a component is greater than 10, then it is<br />

too complex. In such a case, it should either be rewritten or else broken down into several<br />

smaller components.<br />

Cyclomatic complexity is a useful attempt to quantify complexity, and it is claimed<br />

that it has been successfully applied. It is, however, open to several criticisms as follows.<br />

First, why is the value of 10 adopted as the limit? This figure <strong>for</strong> the maximum<br />

allowed complexity is somewhat arbitrary and unscientific.<br />

Second, the measure makes no allowance <strong>for</strong> the sheer length of a module, so that a<br />

one-page module (with no decisions) is rated as equally complex as a thousand-page<br />

module (with no decisions).<br />

Third, the measure depends only on control flow, ignoring, <strong>for</strong> example, references<br />

to data. One program might only act upon a few items of data, while another might<br />

involve operations on a variety of complex objects. (Indirect references to data, say via<br />

pointers, are an extreme case.)<br />

Finally, there is no evidence to fully correlate McCabe’s measure with the complexity<br />

of a module as perceived by human beings.<br />

So McCabe’s measure is a crude attempt to quantify the complexity of a software<br />

component. But it suffers from obvious flaws and there are various suggestions <strong>for</strong><br />

devising an improved measure. However, McCabe’s complexity measure has become<br />

famous and influential as a starting point <strong>for</strong> work on metrics.<br />

SELF-TEST QUESTION<br />

29.1 Suggest a <strong>for</strong>mula <strong>for</strong> calculating the complexity of a piece of program.<br />

>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!