Structured Programming with go to Statements DONALD E. KNUTH Stanford University, Stanford, California 9~S05 A consideration of several different examples sheds new light on the problem of ereat- ing reliable, well-structured programs that behave efficiently. This study focuses largely on two issues: (a) improved syntax for iterations and error exits, making it possible to write a larger class of programs clearly and efficiently without go to state- ments; (b) a methodology of program design, beginning with readable and correct, but possibly inefficient programs that are systematically transformed if necessary into efficient and correct, but possibly less readable code. The discussion brings out op- posing points of view about whether or not go to statements should be abolished; some merit is found on both sides of this question. Fina!ly, an attempt is made to define the true nature of structured programming, and to recommend fruitful direc- tions for further study. Keywords and phrases: structured programming, go to statements, language design, event indicators, recursion, Boolean variables, iteration, optimization of programs, program transformations, program manipulation systems searching, Quieksort, efficiency CR categories: 4.0, 4.10, 4.20, 5.20, 5.5, 6.1 (5.23, 5.24, 5.25, 5.27) You may go when you will go, And I will stay behind. --Edna St. Vincent Millay [66] Most likely you go your way and I'll go mine. --Song title by Bob Dylan [33] Do you suffer from painful elimination? --Advertisement, J. B. Williams Co. INTRODUCTION change your life. The reasons for this revolu- tion and its future prospects have been aptly A revolution is taking place in the way we described by E. W. Dijkstra in his 1972 Tur- write programs and teach programming, be- ing Award Lecture, "The Humble Program- cause we are beginning to understand the mer" [27l. associated mental processes more deeply. It As we experience this revolution, each of is impossible to read the recent book Struc- us naturally is developing strong feelings one tured programming [17; 55] without having it way or the other, as we agree or disagree This research was supported in part by the Na- with the revolutionary leaders. I must admit tional Science Foundation under grant number GJ 36473X, and by IBM Corporation. to being a nomhumble programmer, egotisti- Copyright (~) 1974, Association for Computing Machinery, Inc. General permission to republish, but not for profit, all or part of this material is granted, provided that ACM's copyright notice is given and that reference is made to this publication, to its date of issue, and to the fact that reprint- ing privileges were granted by permission of the Association for Computing Machinery. Computing Surveys, V?L 6, No. 4, Dee,ember 1974 262 , Donald E. Knuth CONTENTS eal enough to believe that my own opinions of the current treads are not a waste of the reader's time. Therefore I want to express in this article several iof the things that struck me most forcefully as I have been thinking about structured programming during the last year; several of my blind spots were re- INTRODUCTION moved as I ivas learning these things, and I 1. ELIMINATION OF so to STATEMENTS hope I can convey some of my excitement to Historical Background A Searching Example the reader. Hardly any of the ideas I will Efficiency discuss are my own; they are nearly all the Error Exits work of others, but perhaps I may be pre- Subscript Checking Hash Coding senting them in a new light. I write this Text Scanning article in the first person to emphasize the A Confession fact that what I'm saying is just one man's Tree Searching Systematic Elimination opinion; I don't expect to persuade everyone Event Indicators that my present views are correct. Comparison of Features Before beginning a more technical discus- Simple Iterations 2. INTRODUCTION OF so to STATEMENTS sion. I should confess that the title of this Recursion Elimination article was chosen primarily to generate Program Manipulation Systems attention. There are doubtless some readers Reeursion vs. Iteration Boolean Variable Elimination who are convinced that abolition of go to Coroutines statements is merely a fad. and they may see Quicksort : A Digression Axiomatics of Jumps this title and think, "Aha! Knuth is rehabili- Reduction of Complication tating the go to statement, and we can go 3. CONCLUSIONS back to our old ways of programming Structured Programming With go to Statements again." Another class of readers will see the Efficiency heretical title and think, "When are die- The Future hards like Knuth going to get with it?" I ACKNOWLEDGMENTS APPENDIX hope that both classes of people will read on BIBLIOGRAPHY and discover that what I am really doing is striving for a reasonably well balanced view- point about the proper role of go to state- ments. I argue for the elimination of go to's in certain cases, and for their introduction in others. I believe that by presenting such a view I am not in fact disagreeing sharply with Dijkstra's ideas, since he recently wrote the following: "Please don't fall into the trap of believing that I am terribly dogmatical about [the go to statement]. I have the uncomfortable feeling that others are making a religion out of it, as if the conceptual problems of programming could be solved by a single trick, by a simple form of coding discipline!" [29]. In other words, it, seems that fanatical advocates of the New Pro- gramming are going overboard in their strict enforcement of morality and purity in programs. Sooner or later people are going to find that their beautifully-structured Computing Surveys, Vol. 6, No. 4, December 1974 Structured Programming with go to Sta~ment~ * 263 ! programs are running at only half the speed we also hear complaints about floating-point --or worse--of the dirty old programs they calculations, global variables, semaphores, used to write, and they will mistakenly blame pointer variables, and even assignment the structure instead of recognizing what is statements. Soon we might be restricted to probably the real culprit--the system over- only a dozen or so programs that are suffi- head caused by typical compiler implementa- ciently simple to be allowable; then we will tion of Boolean variables and procedure calls. be almost certain that these programs Then we'll have an unfortunate counter- cannot lead us into any trouble, but of revolution, something like the current rejec- course we won't be able to solve many tion of the "New Mathematics" in reaction problems. to its over-zealous reforms. In the mathematical ease, we know what It may be helpful to consider a further happened: The intuitionists taught the other analogy with mathematics. In 1904, Bert- mathematicians a great deal about deductive rand Russell published his famous paradox methods, while the other mathematicians about the set of all sets which aren't mem- cleaned up the classical methods and even- bers of themselves. This antinomy shook the tually "won" the battle. And a revolution foundations of classical mathematical rea- did, in fact, take place. In the computer soning, since it apparently brought very science case, I imagine that a similar thing simple and ordinary deductive methods into will eventually happen: purists will point the question. The ensuing crisis led to the rise way to clean constructions, and others will of "intuitionist logic", a school of thought find ways to purify their use of floating-point championed especially by the Dutch mathe- arithmetic, pointer variables, assignments, matician, L. E. J. Brouwer; intuitionism etc., so that these classical tools can be used abandoned all deductions that were based on with comparative safety. questionable nonconstructive ideas. For a Of course all analogies break down, includ- while it appeared that intuitionist logic ing this one, especially since I'm not yet would cause a revolution in mathematics. conceited enough to compare myself to But the new approach angered David Hil- David Hilbert. But I think it's an amusing bert, who was perhaps the leading mathema- coincidence that the present programming tician of the time; Hilbert said that "For- revolution is being led by another Dutchman bidding a mathematician to make use of the (although he doesn't have extremist views principle of the excluded middle is like corresponding to Brouwer's); and I do forbidding an astronomer his telescope or a consider assignment statements and pointer boxer the use of his fists." He characterized variables to be among computer science's the intuitionist approach as seeking "to "most valuable treasures!'. save mathematics by throwing overboard At the present time I think we are on the all that is troublesome .... They would chop verge of discovering at last what program- up and mangle the science. If we would ming languages should really be like. I look follow such a reform as they suggest, we forward to seeing many responsible experi- could run the risk of losing a great part of our ments with language design during the next most valuable treasures" [80, pp. 98-99, few years; and my dream is that by 1984 we 148-150, 154-157, 184-185, 268-270]. will see a consensus developing for a really Something a little like this is happening good programming language (or, more likely, in computer science. In the late 1960's we a coherent family of languages). Further- witnessed a "software crisis", which many more, I'm guessing that people will become people thought was paradoxical because so disenchanted with the languages they are programming was supposed to be so easy. now using--even COBOL and FORTrAN-- As a result of the crisis, people are now be- that this new language, UTOPXA84, will have ginning to renounce every feature of pro- a chance to take over.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages41 Page
-
File Size-