19.5. A Programming Foundation: Structured Programming

 < Free Open Study > 

The term "structured programming" originated in a landmark paper, "Structured Programming," presented by Edsger Dijkstra at the 1969 NATO conference on software engineering (Dijkstra 1969). By the time structured programming came and went, the term "structured" had been applied to every software-development activity, including structured analysis, structured design, and structured goofing off. The various structured methodologies weren't joined by any common thread except that they were all created at a time when the word "structured" gave them extra cachet.

The core of structured programming is the simple idea that a program should use only one-in, one-out control constructs (also called single-entry, single-exit control constructs). A one-in, one-out control construct is a block of code that has only one place it can start and only one place it can end. It has no other entries or exits. Structured programming isn't the same as structured, top-down design. It applies only at the detailed coding level.

A structured program progresses in an orderly, disciplined way, rather than jumping around unpredictably. You can read it from top to bottom, and it executes in much the same way. Less disciplined approaches result in source code that provides a less meaningful, less readable picture of how a program executes in the machine. Less readability means less understanding and, ultimately, lower program quality.

The central concepts of structured programming are still useful today and apply to considerations in using break, continue, throw, catch, return, and other topics.

The Three Components of Structured Programming

The next few sections describe the three constructs that constitute the core of structured programming.

Sequence

A sequence is a set of statements executed in order. Typical sequential statements include assignments and calls to routines. Here are two examples:

Cross-Reference

For details on using sequences, see Chapter 14, "Organizing Straight-Line Code."


Java Examples of Sequential Code
// a sequence of assignment statements a = "1"; b = "2"; c = "3"; // a sequence of calls to routines System.out.println( a ); System.out.println( b ); System.out.println( c );

Selection

A selection is a control structure that causes statements to be executed selectively. The if-then-else statement is a common example. Either the if-then clause or the else clause is executed, but not both. One of the clauses is "selected" for execution.

Cross-Reference

For details on using selections, see Chapter 15, "Using Conditionals."


A case statement is another example of selection control. The switch statement in C++ and Java and the select statement in Visual Basic are all examples of case. In each instance, one of several cases is selected for execution. Conceptually, if statements and case statements are similar. If your language doesn't support case statements, you can emulate them with if statements. Here are two examples of selection:

Java Examples of Selection
// selection in an if statement if ( totalAmount > 0.0 ) {    // do something    ... } else {    // do something else    ... } // selection in a case statement switch ( commandShortcutLetter ) {    case 'a':       PrintAnnualReport();       break;    case 'q':       PrintQuarterlyReport();       break;    case 's':       PrintSummaryReport();       break;    default:       DisplayInternalError( "Internal Error 905: Call customer support." ); }

Iteration

An iteration is a control structure that causes a group of statements to be executed multiple times. An iteration is commonly referred to as a "loop." Kinds of iterations include For-Next in Visual Basic and while and for in C++ and Java. This code fragment shows examples of iteration in Visual Basic:

Cross-Reference

For details on using iterations, see Chapter 16, "Controlling Loops."


Visual Basic Examples of Iteration
' example of iteration using a For loop For index = first To last    DoSomething( index ) Next ' example of iteration using a while loop index = first While ( index <= last )    DoSomething ( index )    index = index + 1 Wend  ' example of iteration using a loop-with-exit loop  index = first  Do     If ( index > last ) Then Exit Do     DoSomething ( index )     index = index + 1  Loop

The core thesis of structured programming is that any control flow whatsoever can be created from these three constructs of sequence, selection, and iteration (Böhm Jacopini 1966). Programmers sometimes favor language structures that increase convenience, but programming seems to have advanced largely by restricting what we are allowed to do with our programming languages. Prior to structured programming, use of gotos provided the ultimate in control-flow convenience, but code written that way turned out to be incomprehensible and unmaintainable. My belief is that use of any control structure other than the three standard structured programming constructs that is, the use of break, continue, return, throw-catch, and so on should be viewed with a critical eye.

 < Free Open Study > 


Code Complete
Code Complete: A Practical Handbook of Software Construction, Second Edition
ISBN: 0735619670
EAN: 2147483647
Year: 2003
Pages: 334

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net