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.4 Selecting control structures 95 The simplest interface comprises one entry point, at the start, and one exit point at the end. This has the strength of being consistent with the essence of sequential programming. It also conforms to the important idea of calling a method. We are used to the idea of calling a method as a sequential step and returning from it to the next instruction in sequence. (We do not, for example, expect to supply a label as a parameter to which control is returned.) Abstraction This is probably the most important idea in structured programming. The human mind cannot devise or understand the whole of a complex system. Instead we can only understand one part of the system at a time. Nonetheless it is vital to understand the whole system. The only way to resolve these contradictory statements is to be able to perceive the overall structure in a digestible way. The solution is to use abstraction, that is, the system must be described in a notation that allows subsystems to be seen as black boxes whose task is readily understood but whose detail is invisible. In programming, the method has long been a mechanism that fulfills this role. Other constructs that possess the same single-entry at the start, single-exit at the end property, are if-then-else and while-do. Expressive power In discussing the arguments against the goto statement, we saw that the goto is too primitive. It has more to do with describing what a machine will do than what a programmer intends. Instead we look at the range of structures on offer, tempted on the one hand to seize every mechanism available, while at the same time conscious of the need for minimality. Certainly we need some mechanism for repetition, and either a while statement or recursion is sufficient to provide this facility. Many languages provide both a while statement and a repeat-until statement. Most languages also support recursion. Arguably we also require a statement to carry out a choice of actions following a test. The if-then-else fulfills this requirement, but others are equally valid, including the case statement. Again, we are torn between expressive power and minimality. Orthogonality When designing a set of facilities, a good design principle is to create features that are each as different from each other as possible. If this is so, we can more easily satisfy the goal of a minimum number of functions, while at the same time ensuring that the facilities are sufficiently powerful for all our needs. Minimality The principle of minimality curbs our tendency to include too many facilities. A consequence of Bohm and Jacopini’s theorem is that we know that the three control structures

96 Chapter 7 ■ Structured programming > (sequence, selection and iteration) are sufficient. (Strictly, a construct for iteration, such as a while, is also unnecessary, because any loop that can be written iteratively can also, in theory, be achieved using only recursion.) Consider the flowcharts of various control structures. Sequence has one box, while has two, and if has three boxes. There are other control structures that involve only three or less boxes; but from amongst them all, these are the minimal feasible set. 7.5 ● What is structured programming? It is easy to become engrossed in the arguments about the goto statement, but is this the central issue of structured programming? Can a program that is written using only the three structures claim to be well structured? The answer is no; it is possible to create a bad program structure using the three structures, just as it is possible (with greater difficulty) to create a good structure that uses goto statements. To illustrate why this is so, consider a badly structured program that has been written using many gotos. If we now transform this into a program that uses the three structures, we still have a badly structured program, since we have done nothing significant to improve it. As a second example, consider a program to search a table for a required item. Two alternative solutions, one structured, the other not, were compared earlier. However, arguably, neither of these is the best solution. Here is another, in which the item being sought is first placed at the end of the table: a[tableSize + 1] = x i = 1 while a[i] not = x i = i + 1 endwhile if i = tableSize + 1 then ---else---endif This is arguably the best of the solutions because it is less complex (the condition in the while statement is simpler) and would execute more quickly on a conventional computer (for the same reason that there is only one condition to test). This example illustrates that the use of the approved structures does not necessarily guarantee the best design. A structured program is essentially one that can be understood easily, and the most useful tool in understanding is abstraction. Abstraction is concerned with identifying the major elements of what is being studied, and ignoring detail. Such is the complexity of software that we have to do this in order to stand a chance of understanding it. >

96 Chapter 7 ■ Structured programming<br />

><br />

(sequence, selection and iteration) are sufficient. (Strictly, a construct <strong>for</strong> iteration, such<br />

as a while, is also unnecessary, because any loop that can be written iteratively can also,<br />

in theory, be achieved using only recursion.) Consider the flowcharts of various control<br />

structures. Sequence has one box, while has two, and if has three boxes. There are<br />

other control structures that involve only three or less boxes; but from amongst them all,<br />

these are the minimal feasible set.<br />

7.5 ● What is structured programming?<br />

It is easy to become engrossed in the arguments about the goto statement, but is this<br />

the central issue of structured programming?<br />

Can a program that is written using only the three structures claim to be well structured?<br />

The answer is no; it is possible to create a bad program structure using the three<br />

structures, just as it is possible (with greater difficulty) to create a good structure that<br />

uses goto statements. To illustrate why this is so, consider a badly structured program<br />

that has been written using many gotos. If we now trans<strong>for</strong>m this into a program that<br />

uses the three structures, we still have a badly structured program, since we have done<br />

nothing significant to improve it.<br />

As a second example, consider a program to search a table <strong>for</strong> a required item. Two<br />

alternative solutions, one structured, the other not, were compared earlier. However,<br />

arguably, neither of these is the best solution. Here is another, in which the item being<br />

sought is first placed at the end of the table:<br />

a[tableSize + 1] = x<br />

i = 1<br />

while a[i] not = x<br />

i = i + 1<br />

endwhile<br />

if i = tableSize + 1<br />

then<br />

---else---endif<br />

This is arguably the best of the solutions because it is less complex (the condition in the<br />

while statement is simpler) and would execute more quickly on a conventional computer<br />

(<strong>for</strong> the same reason that there is only one condition to test). This example illustrates that<br />

the use of the approved structures does not necessarily guarantee the best design.<br />

A structured program is essentially one that can be understood easily, and the most<br />

useful tool in understanding is abstraction. Abstraction is concerned with identifying<br />

the major elements of what is being studied, and ignoring detail. Such is the complexity<br />

of software that we have to do this in order to stand a chance of understanding it.<br />

>

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

Saved successfully!

Ooh no, something went wrong!