Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach

web.firat.edu.tr
from web.firat.edu.tr More from this publisher
21.08.2013 Views

7.2 Arguments against goto 89 If we visualize any one of the three constructs as they are used, then a third characteristic is evident: 3. the entry is at the start and the exit is at the end. Why is it that these characteristics are important? Why are other constructs that have the same characteristics (Figure 7.2) ruled out? We now go on to look at these questions. SELF-TEST QUESTION 7.1 Write a loop that repeats ten times, first using a while statement, then using goto. 7.2 ● Arguments against goto gotos are unnecessary Fortunately there is a mathematical theorem (thanks to Bohm and Jacopini) guaranteeing that any program written using goto statements can be transformed into an equivalent program that uses only the structured constructs (sequence, selection and iteration). The converted program will, in general, need additional data items that are used as flags to control the actions of the program. Indeed the new program may look rather contrived; nonetheless, it can be done. On the face of it, therefore, there is no need for programs with gotos in them. Note, as an interesting side issue, that the theorem does not tell us how to transform the unstructured program; only that it can be done. Experimental evidence Structured programming is well established and widely regarded as the best approach to programming. You might think, therefore, that there would be clear evidence from real software projects that it is beneficial, but this is not so; there are no convincing results from real projects, largely because a carefully controlled experiment would be difficult and expensive to mount. It would be necessary to develop a particular piece of software in two ways: once using structured programming and again using “unstructured” programming. All other variables, like the expertise of the programmers, would have to be held constant. The two versions of the software could be compared according to criteria like development time and number of errors. Regrettably, there are no results of this type. However, experimenters have carried out small-scale studies comparing how easily people can understand and debug small structured programs compared with unstructured ones. In a typical experiment, each of a group of subjects is presented with the

90 Chapter 7 ■ Structured programming > listing of a program that is written in a structured way and asked a series of questions that are designed to assess their comprehension of it. The accuracy of the replies and the time taken to reply are both measured. These are measures of the ease with which the program could be debugged or changed. A second group of subjects are given copies of the same program rewritten in an unstructured way. The accuracy and response times of the two groups are compared. The results of these experiments generally indicate that structured programs are superior to unstructured ones. The results of empirical studies are reviewed in the literature given at the end of the chapter. In a review published in 1984, long after the dust had settled on the structured programming debate, Vessey and Weber concluded that “the evidence supporting [structured programming] is weak”. This conclusion largely stems from the difficulty of carrying out experiments that give trustworthy results. Clarity and expressive power Compare the following two equivalent program fragments: ------ ------- ------ ------label: while a > 0 ------ ------- ------ ------if a > 0 goto label endwhile ------ ------- ------ ------- As we read down the first program fragment, we are not immediately sure what the roles of the label and goto are. It would take us some time to read and study the program in order to discover that they are being used to create the repetition of a piece of code. This is made immediately obvious by the while statement in the second program. Worse, there is a remaining doubt in the first program that there may be another goto aimed at this same label from some other point in the program. The facilities of a programming language should allow people to describe what they want to do in a meaningful way. If we examine a typical program written using gotos we see that the gotos are used for a variety of purposes, for example: ■ to avoid a piece of code (which is to be executed in different circumstances) ■ to perform repetition ■ to exit from the middle of a loop ■ to invoke a shared piece of code. When we see a goto, there are few clues that allow us to decide the purpose for which the goto is being used. The alternative is, of course, a unique language construct for use in each of these different circumstances. These are, respectively: >

7.2 Arguments against goto 89<br />

If we visualize any one of the three constructs as they are used, then a third characteristic<br />

is evident:<br />

3. the entry is at the start and the exit is at the end.<br />

Why is it that these characteristics are important? Why are other constructs that<br />

have the same characteristics (Figure 7.2) ruled out? We now go on to look at these<br />

questions.<br />

SELF-TEST QUESTION<br />

7.1 Write a loop that repeats ten times, first using a while statement, then<br />

using goto.<br />

7.2 ● Arguments against goto<br />

gotos are unnecessary<br />

Fortunately there is a mathematical theorem (thanks to Bohm and Jacopini) guaranteeing<br />

that any program written using goto statements can be trans<strong>for</strong>med into an<br />

equivalent program that uses only the structured constructs (sequence, selection and<br />

iteration). The converted program will, in general, need additional data items that are<br />

used as flags to control the actions of the program. Indeed the new program may look<br />

rather contrived; nonetheless, it can be done. On the face of it, there<strong>for</strong>e, there is no<br />

need <strong>for</strong> programs with gotos in them.<br />

Note, as an interesting side issue, that the theorem does not tell us how to trans<strong>for</strong>m<br />

the unstructured program; only that it can be done.<br />

Experimental evidence<br />

Structured programming is well established and widely regarded as the best approach<br />

to programming. You might think, there<strong>for</strong>e, that there would be clear evidence from<br />

real software projects that it is beneficial, but this is not so; there are no convincing<br />

results from real projects, largely because a carefully controlled experiment would be<br />

difficult and expensive to mount. It would be necessary to develop a particular piece of<br />

software in two ways: once using structured programming and again using “unstructured”<br />

programming. All other variables, like the expertise of the programmers, would<br />

have to be held constant. The two versions of the software could be compared according<br />

to criteria like development time and number of errors. Regrettably, there are no<br />

results of this type.<br />

However, experimenters have carried out small-scale studies comparing how easily<br />

people can understand and debug small structured programs compared with unstructured<br />

ones. In a typical experiment, each of a group of subjects is presented with the

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

Saved successfully!

Ooh no, something went wrong!