An algorithm for structuring programs (Extended Abstract)

Structured programming emphasizes programming language constructs such as <bold>while</bold> loops, <bold>until</bold> loops, and <bold>if then else</bold> statements. Properly used, these constructs make occurrences of loops and branching of control obvious. They are preferable to <bold>goto</bold> statements, which tend to obscure the flow of control [DDH,DIJ]. This paper describes an algorithm which transforms a flowgraph into a program written using <bold>repeat</bold> (<bold>do forever</bold>) and <bold>if then else</bold> statements. The goal of the algorithm is to produce readable programs, rather than to avoid the use of <bold>goto</bold> statements entirely. <bold>goto</bold> statements are generated when there is no better way to describe the flow of control. A number of techniques for eliminating <bold>goto</bold> statements from programs have been previously published [AM, BJ, BS, COO, KF, KOS, PKT]. However, these techniques do not necessarily produce clear flow of control [KN]. Misuse of control constructs may mislead the reader into expecting patterns of control flow which do not exist in the algorithm. For example, these techniques may use a <bold>repeat</bold> statement when the contained code cannot be executed more than once or add numerous control variables to avoid <bold>goto</bold> statements. They also may avoid <bold>goto</bold> statements by copying segments of code or creating subroutines. The former method results in longer programs and bugs may be introduced when all the identical segments must be modified. The latter method may result in subroutines which appear unnatural.