
A Case for the GOT O Martin E . Hopkins, I13 M In recent years there has been much controversy ove r programming style and while style is not everythin g the use of the goto statement . This paper, whil e it does have a great deal of importance . Second , acknowledging that goto has been used too often , the popularity of the no goto rule is, in larg e presents the case for its retention in current an d part, due to the fact that it is a simple rule whic h future programming languages . does improve the code produced by most programmers . As we shall see, this is not sufficient grounds KEY WORDS AND PHRASES : goto, block structur e for banishing the construct from our languages , languages, programming styl e although it may well justify teaching alternativ e CR CATEGORIES : 4 .2 2 methods of programming to beginners or restrictin g its use on a project . Perhaps the most beneficia l INTRODUCTION aspect of the controversy will be to encourage th e use of block structure languages and to discourag e It is with some trepidation that I undertake t o use of our most popular languages, COBOL and FORTRA N defend the goto statement, a construct, which whil e as they are not well suited to programming withou t ancient and much used has been shown to be theoret- the goto . ically unnecessary (2) and in recent years has com e The principal motivation behind eliminatin g under so much attack (3) . In my opinion, ther e the goto statement is the hope that the resultin g have been far too many goto statements in mos t programs will not look like a bowl of spaghetti . programs, but to say this is not to say that got o Instead they will be simple, elegant and easy t o should be eliminated from our programming languages . read, both for the programmer who is engaged i n This paper contains a plea for the retention o f composition and checkout as well as the poor sou l goto in both current and future languages . Le t attempting future modification . By avoiding got o us first examine the context in which the contro- one guarantees that a program will consist entirel y versy occurs . of one-in-one-out control structures . It is eas y A wise philosopher once pointed out to a to read a properly indented program without t o lazy king that there is no royal road to geometry . statements, which is written in a block structur e After discovering, in the late fifties, that pro- language . The possible predecessors of every state- gramming was the computer problem, a search wa s ment are obvious and, with the exception of loops , made during the sixties for the royal road t o all predecessors are higher on the page . (I assum e programming . Various paths were tried includin g nobody writes inner procedures longer than a pag e comprehensive operating systems, higher leve l anymore?) Why then should we retain the goto state- languages, project management techniques, tim e ment in our current and future programming languages ? sharing, virtual memory, programmer education , and applications packages . While each of thes e THEORETICAL CONSIDERATION S is useful, they have not solved the programmin g problem . Confronted with this unresolve d It has been demonstrated that there are flo w problem and with few good ideas on the horizon , charts which cannot be converted to a procedura l some people are now hoping that the royal roa d notation without .goto statements [1,4] . It turns will be found through style, and that banishmen t out though that this result is not really an argu- of the to statement will solve all . Th e ment for the retention of goto as there are mean s existence of this controversy and the seriousnes s by which a procedure can be rewritten in a system- assigned to it by otherwise very sensible peopl e atic manner to eliminate all instances of goto . A n are symptoms of a malaise in the computing com- almost trivial method is to introduce a new variabl e munity . We have few promising new ideas at hand . which can be thought of as the instruction counte r I also suspect that the controversy reflects some - along with tests and sets of this counter . The thing rather deep in human nature, the notion tha t method is fully described by Bohm and Jacopini [2] . language is magic and the mere utterance of certai n The results of this procedure when applied to a words is dangerous or defiling . Is it an acciden t large program with many instances of goto woul d that " goto " has four letters ? usually be a program which is less readable tha n Having indicated my belief that this contro- the original program with goto statements . However , versy is not quite as momentous as some have mad e nobody seems to be advocating using such unconsidere d out, it is appropriate to point out some beneficia l methods . aspects . First, interest has been focused on The real issue is that theoretical work has 59 suggested a number of techniques that can be use d warning to the reader of the routine that this i s to rewrite programs, eliminating the instances o f a procedure with more than the usual complexity . goto . These include replication of code (nod e Note also that the label point catches the eye . I t splitting), the introduction of new variables an d is immediately apparent when looking at this state- tests as well as the introduction of procedures . ment that it has an unusual predecessor . The care- Any of these techniques, when used with discretion , ful reader will want to consult a cross referenc e can increase the readability of code . The questio n listing to determine the potential flow of control . is whether there are any instances when the appli- Note that the BLISS leave construct is somewha t cation of such methods decreases clarity or produce s less than ideal here . In BLISS when one examine s some other undesirable effect . Whether or not t o the code which follows a bracket terminating a leve l retain the goto does not seem to be a theoretica l of control, its potential predecessors are not im- issue . It is rather a matter of taste, style an d mediately apparent . One must look upward on th e the nractical considerations of day to day compute r page for its associated label, which indicates a use . potential unusual predecessor and then find th e leave . It is my feeling that unusual exits fro m ALTERNATIVES TO GOT O levels of control should be avoided . The multipl e level case is especially ugly . Where such construct s With respect to current languages which are i n are necessary, it should be made com p letely obvious wide use such as COBOL and FORTRAN, there is th e to all . Statements such as the Bliss leave encour- practical consideration that the goto statement i s age unusual exits from multiple levels of control . necessary . Even where a language is reasonably wel l One should not cover up the fact that there is a n suited to programming without the goto, the elimi- awkward bit of logic by the introduction of a new nation of this construct may be at once too loos e control construct . and too restrictive . PL/I provides some interestin g Another interesting PL/I control construct i s examples here . One exits from an Algol procedur e the ON unit . This is a named block which is auto- when the flow of control reaches the end bracket . matically invoked on certain events such as overflow , PL/I provides an additional mechanism, an explici t but it can also be invoked explicitly by a statemen t RETURN statement . Consider the table look up i n of the form : Fig . 1 which is similar to an example of Floyd an d SIGNAL CONDITION (name) ; Knuth (4) . The problem is to find an instance o f The name is established dynamically and need not b e X in the vector A and if there is no instance o f declared in the scope of the SIGNAL . This facilit y X in A, then make a new entry in A of the argu- often eliminates the need to pass special erro r ment X . In either case the index of the entry i s return parameters or test a return code which indi- returned . A count of the number of matches associate d cates abnormal termination of a lower level proce- with each entry in A is also maintained in an asso- dure . , After completion of an ON unit activated b y ciated vector, B . In this example there are n o SIGNAL, control is passed back to the statemen t goto statements but the two RETURN statements caus e following the SIGNAL . This is usually not useful . an exit from both the procedure and the iterativ e One wants to terminate the signaling block and th e DO . Thus the procedure has control structures which only way to do this in PL/I is with a goto out o f have more than one exit and one-in-one-out contro l the ON unit . Is SIGNAL permissible under the n o structures were a principal reason for avoidin g goto criteria? Elimination of goto seems too goto . Should the PL/I programmer add a rule for - restrictive here as SIGNAL is a useful facilit y bidding RETURN? The procedure could then be re - which can eliminate much messy programming detail .
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages4 Page
-
File Size-