7 ms·
This is an excellent article that sets context for, and summarizes, the “goto debate”. I know modern programmers who have simply absorbed the received wisdom t
by svat 8y ago
This is an excellent article that sets context for, and summarizes, the “goto debate”.
I know modern programmers who have simply absorbed the received wisdom that “goto is terrible” and wonder why there even is/was a debate; this short and concise article should show what goto was used for.
At the time Dijkstra wrote (the letter that the editor titled) "Go To Statement Considered Harmful", his proposal would have meant programming in a C-language (of course I know C didn't exist then...) without any `break` or `continue` in loops, and without `return` except at the end of a function. There are very few languages today that don't have `break` and `continue`, and in them (Lua for instance) `goto` is still used.
As the article mentions, it takes much from Knuth's wonderful summarization paper “Structured Programming with Go To Statements”, and if you find this interesting you should definitely read it! https://pic.plover.com/knuth-GOTO.pdf https://pic.plover.com/knuth-GOTO.pdf (Also reprinted in his book/collection titled Literate Programming.)
- zzo38computer 8y agoGoto is good to have. With enough other flow controls you will not use goto so much, although sometimes it is best way to do, by goto. Just because it is uncommon that it will be helpful does not mean that it should be never used; sometimes it is much more clearly and cleanly than the other way which might be messy in some case. Unfortunately, JavaScript does not have a goto command (although I wrote a way in which a preprocessor could convert programs using goto, to the one without, with the restriction that you cannot jump into a block) (what else I don't like about JavaScript is automatic semicolon insertion, although that is a different problem). Of course there is the consideration of implementation, since you will have to keep track of the labels, which may be forward or backward, rather than only the nesting level stack like implementing break/continue and if/else.
- quietbritishjim 8y agoAny time I have felt the need for something like goto, I have found that need served well by a function with multiple return statements. You're right that it can't simulate jumping into a block though, but that seems like a feature to me. Also, refactoring the necessary portion of code into a function is usually an improvement in its own right. It's funny that the GP comment mentioned this but the other comments have all focused on break/continue. I suppose the backward jumps you mention are usually only served by something like break/continue. But I find that when beginners are starting to learn to program, they very often ask for something that sounds a lot like a goto, but their need is also always satisfied by a function with multiple returns (which usually involves explaining what a function is).
- int_19h 8y agoThe funny thing is, it became a cargo cult in many ways. I once worked with a guy who persistently wrote things like these in C++: do { ... if (...) break; ... } while (false); It took me a while to realize what this was about. But, basically, he invented a way to goto (so long as it was forward) without actually spelling it out. When I asked him, he gave the usual creed about how "goto is bad", and wouldn't be convinced that break is goto, especially given how he used it. Needless to say, this kind of code is actually less readable than if it were written with a goto in it, because it looks like a loop, but actually isn't one. With goto, at least it's very clear what it's for, even when there was a better way to do the same thing.
- ensiferum 8y agoIn c++ however using goto is restricted depending on whether you'd jump over declaration of objects with non-trivial types or not.
- enriquto 8y agoof course it has to be, what would you expect it to do otherwise?
- int_19h 8y agoYou can jump past declarations if you're exiting the block - it's only a problem if you're jumping past a declaration to a point where the declared object would still be visible. But the goto equivalent of this code does not require that - it would be a jump to a label after the closing brace.
- superflyguy 8y agoI've seen and use that. It's perfectly readable once you understand it's not a loop. It's handy on c, where you can't try...throw (or don't want to in c++) where you want to do a few things which you want to treat in a transaction-like manner. You would return success from the last line in the do loop and handle errors outside the loop. It's not just about mindlessly avoiding goto; it's about writing clean structured code.
- codeulike 8y agoGOTO https://news.ycombinator.com/item?id=18485597 https://news.ycombinator.com/item?id=18485597
- augustk 8y ago> There are very few languages today that don't have `break` and `continue`, and in them (Lua for instance) `goto` is still used. Oberon is one of those few "pure" structured languages where a statement sequence is never interrupted. The latest version of Oberon doesn't even have a return statement (which is also also a form of goto unless defined as the last statement of a subroutine).
- SilasX 8y agoWait, what? From my memory, the Dijkstra comment was about arbitrary jumps from Goto statements. Like, where you could jump into another function with a completely different stack state. I saved this reddit comment that said it perfectly: >>C goto is not the type of goto that Dijkstra was complaining about. It's a tamed sort of goto. The sort of goto that Dijkstra was complaining about would allow you to do things, in modern terminology, like leap from one function directly into the middle of another function, in the middle of two loops, an if clause, and three additional nested exception contexts the goto-using function doesn't have. >>That is to say, we've built so many abstractions on top of structured programming that the modern mind can barely even comprehend the sort of goto that Dijkstra had in mind. He won his point so thoroughly that structured programming is simply the water that modern-day programmer fish swim through, taking for granted. The only gotos that are permitted are ones that honor all of the structured programming criteria. And then people get all excited saying that Dijkstra was wrong, because look at how safe these goto statements are! They can't possibly catch on fire, what with all this water we're swimming in! https://www.reddit.com/r/programming/comments/8lm8p7/30_years_from_now_the_goto_will_still_be_alive/dzijde4/ https://www.reddit.com/r/programming/comments/8lm8p7/30_year...
- svat 8y agoYou can understand what Dijkstra's comment was about, by reading both the original source (https://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf https://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.p...) and the responses to it (the most comprehensive being Knuth's https://homepages.cwi.nl/~storm/teaching/reader/Knuth74.pdf https://homepages.cwi.nl/~storm/teaching/reader/Knuth74.pdf). See for instance page 277 in Knuth's paper: > Comparison of Features > Of course, event indicators are not the only decent alternatives to go to statements that have been proposed. Many authors have suggested language features which provide roughly equivalent facilities, but which are expressed in terms of exit, jump-out, break or leave statements. It goes on to discuss more alternatives (at least one of which I find in fact better than the "break" and "continue" we happen to have got with C). It also mentions: > what I believe is really the most common situation in which go to statements are needed by an ALGOL or PL/I programmer, namely [a loop which is performed "n and a half times"] This is also what the linked article discusses in section 4.1, the first one under "Common uses" (http://web.sonoma.edu/users/l/luvisi/goto/goto.pdf#page=5 http://web.sonoma.edu/users/l/luvisi/goto/goto.pdf#page=5). If you restrict Dijkstra's comment to only apply to arbitrary jumps such as those into another function, then it does it a great disservice: there's no force in the argument anymore (it's a banal claim that nearly everyone always agreed with... in fact some of the languages in which goto was being debated at the time didn't even support such non-local gotos), and there would have been no controversy. It's worthwhile to consider thoughtfully the stronger claim that Dijkstra actually made (“the go to statement should be abolished from all 'higher level' programming languages (i.e., everything except, perhaps, plain machine code)”).
- MichaelMoser123 8y agojust a nitpick: it doesn't mention setjmp/longjmp in C - that is even less restricted and allows you to jump between functions. Well it is used to do something like exception handling in C; but don't do that if C and C++ got mixed as your automatic objects in C++ will not get destructed.