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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Figure 6.1 Two alternative software structures<br />

6.4 Component size and complexity 71<br />

If the components are small, there will be many components and there<strong>for</strong>e many<br />

connections between them in total. The structure is a network with many branches and<br />

many small leaves. The smaller the components, the easier an individual component<br />

should be to comprehend. But if the components are small, we run the risk of being<br />

overwhelmed by the proliferation of interconnections between them.<br />

The question is: Which of the two structures is the better? The alternatives are large<br />

components with few connections, or small components with many connections.<br />

However, as we shall see, the dilemma is not usually as simple as this.<br />

A common point of view is that a component should occupy no more than a page of<br />

coding (about 40–50 lines). This suggestion takes account of the difficulty of understanding<br />

logic that spills over from one page of listing (or one screen) to another.<br />

A more extreme view is that a component should normally take up about seven lines<br />

or less of code, and in no circumstances more than nine. Arguments <strong>for</strong> the “magic<br />

number” seven are based on experimental results from psychology. Research indicates<br />

that the human brain is capable of comprehending only about seven things (or concepts)<br />

at once. This does not mean that we can remember only seven things; clearly we<br />

can remember many more. But we can only retain in short-term memory and study as<br />

a complete, related set of objects, a few things. The number of objects ranges from about<br />

five to nine, depending on the individual and the objects under study. The implication<br />

is that if we wish to understand completely a piece of code, it should be no more than<br />

about seven statements in length. Relating lines of code to concepts may be oversimplifying<br />

the psychological basis <strong>for</strong> these ideas, but the analogy can be helpful. We shall<br />

pursue this further later in the chapter.<br />

Clearly a count of the number of lines is too crude a measure of the size of a component.<br />

A seven-line component containing several if statements is more complex than<br />

seven ordinary statements. The next section pursues this question.<br />

We have already met an objection to the idea of having only a few statements in a<br />

component. By having a few statements we are only increasing the number of components.<br />

So all we are doing is to decrease complexity in one way (the number of statements<br />

in a component) at the cost of increased complexity in another way (the number<br />

of components). So we gain nothing overall.<br />

Do we need a few, large components or many small components? The answer is that<br />

we need both. We pose the question of how a piece of software is examined. Studying

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

Saved successfully!

Ooh no, something went wrong!