Edgar Dijkstra:Go To Statement Considered Harmful Go To Statement Considered Harmful dynamic progress is only characterized when we also give to which Key Words and Phrases:go to statement,jump instruction, call of the procedure we refer.With the inclusion of procedures branch instruction,conditional clause,alternative clause,repet- we can characterize the progress of the process via a sequence of itive clause,program intelligibility,program sequencing textual indices,the length of this sequence being equal to the dynamic depth of procedure calling CR Categories:4.22,5.23,5.24 Let us now consider repetition clauses (like,while B repeat A EDITOR: or repeat A until B).Logically speaking,such clauses are now For a number of years I have been familiar with the observation superfuous,because we can express repetition with the aid of that the quality of programmers is a decreasing function of the recursive procedures.For reasons of realism I don't wish to ex- density of go to statements in the programs they produce.More clude them:on the one hand,repetition clauses can be imple- recently I discovered why the use of the go to statement has such mented quite comfortably with present day finite equipment;on disastrous effects,and I became convinced that the go to state- the other hand,the reasoning pattern known as "induetion" ment should.be abolished from all "higher level"programming makes us well equipped to retain our intellectual grasp on the languages (i.e.everything exeept,perhaps,plain machine code). processes generated by repetition clauses.With the inclusion of At'that time I did not attach too much importance to this dis- the repetition clauses textual indices are no longer sufficient to covery;I now submit my considerations for publication because describe the dynamic progress of the process.With each entry into in very recent discussions in which the subject turned up,I have a repetition clause,however,we can associate a so-called "dy- been urged to do so. namic index,"inexorably counting the ordinal number of the My first remark is that,although the programmer's activity corresponding current repetition.As repetition clauses (just as ends when he has constructed a correct program,the process procedure calls)may be applied nestedly,we find that now the taking place under control of his program is the true subject progress of the process can always be uniquely characterized by a matter of his activity,for it is this process that has to accomplish (mixed)sequence of textual and/or dynamic indices. the desired effect;it is this process that in its dynamic behavior The main point is that the values of these indices are outside has to satisfy the desired specifications.Yet,once the program has programmer's control;they are generated (either by the write-up been made,the "making"'of the corresponding process is dele- of his program or by the dynamic evolution of the process)whether gated to the machine. he wishes or not.They provide independent coordinates in which My second remark is that our intellectual powers are rather to describe the progress of the process. geared to master statie relations and that our powers to visualize Why do we need such independent coordinates?The reason processes evolving in time are relatively poorly developed.For is-and this seems to be inherent to sequential processes-that that reason we should do (as wise programmers aware of our we can interpret the value of a variable only with respect to the limitations)our utmost to shorten the conceptual gap between progress of the process.If we wish to count the number,n say,of the static program and the dynamic process,to make the cor people in an initially empty room,we can achieve this by increas- respondence between the program (spread out in text space)and ing n by one whenever we see someone entering the room.In the the process (spread out in time)as trivial as possible. in-between moment that we have observed someone entering the Let us now consider how we can characterize the progress of a room but have not yet performed the subsequent increase of n, process.(You may think about this question in a very concrete its value equals the number of people in the room minus one! manner:suppose that a process,considered as a time succession The unbridled use of the go to statement has an immediate of actions,is stopped after an arbitrary action,what data do we consequence that it becomes terribly hard to find a meaningful set have to fix in order that we can redo the process until the very of coordinates in which to describe the process progress.Usually, Bame point?)If the program text is a pure concatenation of,say people take into account as well the values of some well chosen assignment statements(for the purpose of this discussion regarded variables,but this is out of the question because it is relative to as the descriptions of single actions)it is sufficient to point in the the progress that the meaning of these values is to be understood program text to a point between two successive action deserip- With the go to statement one ean,of course,still describe the tions.(In the absence of go to statements I can permit myself the progress uniquely by a counter counting the number of actions syntactic ambiguity in the last three words of the previous sen- performed since program start (viz.a kind of normalized clock). tence:if we parse them as"successive (action descriptions)"we The difficulty is that such a coordinate,although unique,is utterly mean successive in text space;if we parse as"(successive action) unhelpful.In such a coordinate system it becomes an extremely descriptions"we mean successive in time.)Let us call such a complicated affair to define all those points of progress where pointer to a suitable place in the text a "textual index." say,n equals the number of persons in the room minus one! When we include conditional clauses (if B then A),alternative The go to statement as it stands is just too primitive;it is too clauses(if B then Al else A2),choice clauses as introduced by much an invitation to make a mess of one's program.One can C.A.R.Hoare (caseli]of(A1,A2,...,An)),or conditional expres regard and appreciate the clauses considered as bridling its use.I sions as introduced by J.MeCarthy(B1→E1,B2→E2,·, do not claim that the clauses mentioned are exhaustive in the sense Bn-En),the fact remains that the progress of the process re- that they will satisfy all needs,but whatever clauses are suggested mains characterized by a single textual index. (e.g.abortion clauses)they should satisfy the requirement that a As soon as we include in our language procedures we must admit programmer independent coordinate system can be maintained to that a single textual index is no longer sufficient.In the case that describe the process in a helpful and manageable way. a textual index points to the interior of a procedure body the It is hard to end this with a fair acknowledgment.Am I to Volume 11 Number 3/March,1968 Communications of the ACM 147 Edgar Dijkstra:Go To Statement Considered Harmful 1
Edgar Dijkstra: Go To Statement Considered Harmful 1 Edgar Dijkstra: Go To Statement Considered Harmful
judge by whom my thinking has been influenced?It is fairly obvious that I am not uninfluenced by Peter Landin and Chris- topher Strachey.Finally I should like to record(as I remember it quite distinctly)how Heinz Zemanek at the pre-ALGoL meeting in early 1959 in Copenhagen quite explicitly expressed his doubts whether the go to statement should be treated on equal syntactic footing with the assignment statement.To a modest extent I blame myself for not having then drawn the consequences of his remark. The remark about the undesirability of the go to statement is far from new.I remember having read the explieit recommenda- tion to restrict the use of the go to statement to alarm exits,but I have not been able to trace it;presumably,it has been made by C.A.R.Hoare.In [1,See.3.2.1.]Wirth and Hoare together make a remark in the same direction in motivating the ease construction:"Like the conditional,it mirrors the dynamic structure of a program more elearly than go to statements and switches,and it eliminates the need for introducing a large number of labels in the program." In [2]Guiseppe Jacopini seems to have proved the (logical) superfluousness of the go to statement.The exercise to translate an arbitrary flow diagram more or less mechanically into a jump- less one,however,is not to be recommended.Then the resulting How diagram cannot be expected to be more transparent than the original one. REFERENCES: 1. WIRTH,NIKLAUS,AND HOARE,C.A.R.A contribution to the development of ALGOL.Comm.ACM 9 (June 1966),413-432. 2.BOHM,CoRRADO,AND JACOPINI,GUISEPPE.Flow diagrams. Turing machines and languages with only two formation rules.Comm.ACM 9 (May 1966),366-371. EDSGER W.DIJKSTRA Technological Universily Eindhoven,The Netherlands Aus:Communications of the ACM11,3(March 1968).147-148. Edgar Dijkstra:Go To Statement Considered Harmful 2
Edgar Dijkstra: Go To Statement Considered Harmful 2 Aus: Communications of the ACM 11, 3 (March 1968). 147-148