7 ms·
Go to Statement Considered Harmful (1968) [pdf]
- rbinv 8y agoAhh yes, the origin of this phrase. I still cringe every time I see it used.
- wyldfire 8y agoInterestingly [1]: > The original title of the letter, as submitted to CACM, was "A Case Against the Goto Statement", but CACM editor Niklaus Wirth changed the title to "Go To Statement Considered Harmful" ... > Frank Rubin published a criticism of Dijkstra's letter in the March 1987 CACM where it appeared under the title "'GOTO Considered Harmful' Considered Harmful". The May 1987 CACM printed further replies, both for and against, under the title "'"GOTO Considered Harmful" Considered Harmful' Considered Harmful?". Dijkstra's own response to this controversy was titled "On a Somewhat Disappointing Correspondence." [1] https://en.wikipedia.org/wiki/Considered_harmful https://en.wikipedia.org/wiki/Considered_harmful
- kazinator 8y agoInterestingly, "A Case Against ..." is also a bit of hackneyed title pattern.
- tedunangst 8y agoWhy a case against is not my favorite pattern.
- kazinator 8y agoA Case Against "Why X is not my Favorite Y" Being Considered Harmful.
- stabbles 8y agoWell, sometimes they are useful when a break statement is not enough to get you out of a nested loop. while (...) { while (...) { // ... if (...) goto stop_it; // ... } // ... } stop_it:
- amitport 8y agothis use case is specifically addressed by "labeled statements" (e.g., https://docs.oracle.com/javase/specs/jls/se12/html/jls-14.html#jls-14.7 https://docs.oracle.com/javase/specs/jls/se12/html/jls-14.ht...)
- mFixman 8y agoDouble-breaking is a good sign that you need to refactor your code and put any of the while statements inside its own function.
- pjc50 8y agoThat generally makes it harder to follow and harder to optimise?
- Joky 8y agoQuite the opposite IMO: it gives a name to the computation and simplifies the control flow by making the computation more functional.
- kyllo 8y agoIn a high level language, yes, you should probably refactor code that looks like that. In C, no--just write the nested loop that iterates the pointers over the array, and use break or goto.
- burfog 8y agoWell then you'll need double-returning, which isn't generally available. One could use longjmp I suppose.
- 8y ago
- Isamu 8y agoThe classic. See also another viewpoint in that conversation: Donald Knuth: Structured programming with go to statements (1974) http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.6084 http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.... [edit] Of the 102 (!) entries in the bibliography of this paper, Knuth cites 9 of Dijkstra's papers (including the goto paper) and 3 personal communications.
- mcguire 8y agoSomebody needs to ask Tony Hoare about his axiomization of GOTO described in that...
- deleted 8y ago[deleted]
- kstenerud 8y agoTry as I might, I just don't see why it's important to maintain an independent index into the process progress of a running program. So you can know the exact point in the process progress where n equals the number of people in the room minus 1... What is the usefulness of it? What problem does it help solve to have this kind of process coordinate in time? I can understand that unbridled use of GOTO tends to lead to a mess, but Dijkstra's argument seems to be more about the purity of the process, for... some purpose I can't fathom. Still, I imagine he'd have a field day trashing dependency injection and dynamic dispatch!
- AnimalMuppet 8y agoIf you've ever had to deal with the previous mess in all its unbridled impurity, you'd know why Dijkstra cares about the purity of the process. The answer wasn't to use GOTO a little bit less, or a little more carefully. That wasn't going to work. The answer was to eliminate it, root and branch.
- mcguire 8y agoOk, I'm not sure I understand your question, but I'll try... Think of his "index" as a virtual program counter, while you are trying to write the code or understand what someone else has written. At some point, the process is going to be in a state where someone has entered the room (i.e. n = #people - 1), and at a later point you have incremented n (so that n = #people again). One of the key features of Dijkstra's thinking about how to understand and write programs is that you should be thinking about the text of the program, not its dynamic behavior. You should not be "pretending to be the computer"---when you mentally "execute" the program, you have to keep a finger pointed at the current statement (and procedures, recursion, loops, etc., make that pretty difficult). However, I don't know if he'd fully developed that idea in 1968; this article reads like he was still trying to understand programs by playing out their behavior. So, if you were counting people going into a room, at some point you will have seen someone go in, but not have incremented the counter while after the next statement you have seen someone and have incremented the counter. Without a lot of discipline, GOTO has the power to completely throw away whatever understanding of the program you have; you could GOTO that point where you are going to increment the count but without seeing anyone go into the room.
- ncmncm 8y agoEdsger, that is.
- gregfjohnson 8y agoDijkstra used the concept of program statements as "predicate transformers". The idea is that a program has a series of predicates (think "assert()" on steroids). A predicate is a formula (usually in first order predicate logic) that characterizes the allowable states of the program at the point where the predicate appears in the program text. I.e., "N == A * k + B". Unlike assert statements, a predicate may not be executable, but rather serves as an aid to reasoning about your code. A statement (i.e., "if", "assign", "case" etc.) modifies the allowable states of the program. Hence the phrase "predicate transformer". The initial state when the program starts has a predicate that characterizes any required assumptions about the inputs. The final state has a predicate that characterizes the required output of the program upon completion. Dijkstra, Gries, Hoare, and others created formal rules of inference describing exactly how a given statement modifies the predicates. This approach worked brilliantly for the normal "structured" statements ("if", "do", etc.) (To me, the most helpful idea from their system was the idea of invariants for iterative statements.) However, their approach was impossible to do for arbitrary "goto" statements. I believe Dijkstra was of the opinion that localized, static reasoning about program correctness was (is) the best way to get code correct and error-free. The fact that arbitrary "goto" statements destroyed the ability to reason about programs in this manner was why he forcefully advocated against them. (An alternative framework, denotational semantics, includes the idea of continuations, which among other things provides a mathematically rigorous characterization of arbitrary "goto" statements.) FWIW, as a programmer I have found over the years that the predicate/"invariant" approach of Hoare-Gries-Dijkstra gives me a useful handle on reasoning through my code and getting it right.
- threatofrain 8y agoWhat you’re describing sounds like TLA+, among other modeling languages.
- User23 8y agoLamport, Dijkstra, Scholten, Hoare, and Knuth, to name just a few, are giants in the field who routinely read each other’s work. I’m positive TLA+ is directly influenced by predicate transformer program semantics.
- nwhatt 8y ago"I don't know how many of you have ever met Dijkstra, but you probably know that arrogance in computer science is measured in nano-Dijkstras." - Alan Kay With Alan Kay putting it into context here: https://news.ycombinator.com/item?id=11796926 https://news.ycombinator.com/item?id=11796926
- SlipperySlope 8y agoThis article had great impact in commercial data processing at the time. I was just entering the programming workforce and my bank programming manager considered our shop very progressive due to strict guidelines on the use of statement labels in our COBOL programs. GOTOs were allowed only to the paragraph label - to effect a loop continue, or to an exit paragraph label - to effect a loop exit. Waves of consulting firms during that decade promoted structured programming in COBOL, which lacked block structure. The utility of Dijkstra's approach was drilled home time and time again by my maintenance of unstructured third party software whose GOTO statements followed some stale lost flowchart. My favorite COBOL label from the FORTE Burroughs database package, recalled vividly 46 years later... JIMS-HOPE.
- simonbyrne 8y agoIt's difficult to examine his comments without historical context: I haven't dealt with any code from that era, but there is still plenty of Fortran 77 code from ~20 years later where the use of gotos makes it difficult to understand control flow, e.g.: https://github.com/JuliaMath/openspecfun/blob/master/amos/zbesi.f https://github.com/JuliaMath/openspecfun/blob/master/amos/zb... That said, gotos are still occasionally very useful: jumping out of nested loops and implementing finite-state machines are both made much easier and clearer via gotos than trying to force them into more standard control flow statements. The linux kernel also makes judicious use of gotos for cleaning up error handling: https://koblents.com/Ches/Links/Month-Mar-2013/20-Using-Goto-in-Linux-Kernel-Code/ https://koblents.com/Ches/Links/Month-Mar-2013/20-Using-Goto...
- busfahrer 8y agoOne thing I've seen a lot in C code is a goto to a single exit point out of a function for common cleanup code
- btilly 8y agoThe use of goto to jump out of nested loops is available in many languages as the ability to break out of a labeled loop. Regular structured programming plus this single extension allows any algorithm that can be written with goto to be written without, with no loss of efficiency. I have seen multiple ways to implement finite state machines. Autogenerated code with goto is indeed a clean option. However I strongly prefer it if the state machine has a relatively clean definition that does not involve goto.
- gizmo686 8y ago>The linux kernel also makes judicious use of gotos for cleaning up error handling. Isn't that standard c style? I can't imagine programming c without goto end.
- bighi 8y agoA good (although often misundertood) paper.
- munificent 8y agoCoincidentally, I put up a new chapter in my in-progress book last night that contains a long aside with my thoughts on this paper: http://craftinginterpreters.com/jumping-back-and-forth.html#design-note http://craftinginterpreters.com/jumping-back-and-forth.html#... Overall, I find it pretty disappointing. It lacks the rigor that Dijkstra himself argued so strongly for and may have stifled actual useful language research.
- svat 8y agoThanks for sharing this. I had the same reaction. I have another point to add: many people think of “goto considered harmful” as saying something like: you can use all the other control structures that (say) C provides, but the “goto” statement should not appear in your program. At least that's what I thought, but in Knuth's encyclopedic response to the article, after giving some examples where eliminating goto (mechanically or otherwise) doesn't give great results, there's a section [1] where he discusses some proposed alternative control structures to goto — and among them is “break”! And on rereading Dijkstra's article and looking at the responses to it and some languages from the time, it dawned on me that what Dijkstra was advocating was that the only control structures be conditionals, loops, and subroutines — no “break” or “continue”, no early “return” in a function — and for example this (plus a discouraged goto) is in fact how things work in the original Pascal language (where you return from a function by assigning to a pseudo-variable and waiting for control flow to reach the end). Most programmers who echo “goto considered harmful” have not actually tried the discipline of giving up all of those and seeing how happy they are. Languages have realized that the barebones proposed by Dijkstra were inadequate and have added “break”, “continue” and “return” and even more control-flow structures, like exceptions. The weak version of the thesis is that too much ad-hoc control flow can be confusing and is best avoided. But most programmers agreed with this anyway (there wouldn't have been much of a debate if this was all it was), and in this version there's no reason the limited set of control-flow structures cannot involve some use of the “goto” keyword (like the “goto end” / “goto cleanup” pattern in C code). The strong version of the thesis, as mentioned in the previous paragraph, is something no one really wants to live with (and I haven't seen the “coordinates” in practice either way). So what people seem to have settled on is an in-between version that abandons the actual content of Dijkstra's article, while retaining the word “goto” as a marker of sin. [1]: http://www.kohala.com/start/papers.others/knuth.dec74.html#comparison http://www.kohala.com/start/papers.others/knuth.dec74.html#c...
- bigato 8y agoSoon in hacker news: "Go 2 considered harmful"