4 ms·
I found it enlightening to learn about the context in which the GOTO was first "considered harmful" by Dijkstra in 1968[1]. This is a period of time when the c
by jacques_chester 14y ago
I found it enlightening to learn about the context in which the GOTO was first "considered harmful" by Dijkstra in 1968[1].
This is a period of time when the concept of higher level languages with specialist if-then, loops and return statements were still relatively new.
Most programmers in the world had cut their teeth on assembly language. In that model the natural way to think of programs is as a sequential series of addresses in memory that contain instructions.
Since all control structures are merely jumps to a new address, so why were all those grumpy ivory tower idiots so keen on abolishing goto?
Probably the biggest blow to the pro-goto camp in those days was the discovery of the "Structured Program Theorem" in 1966. It was proved that any computable algorithm can expressed in any higher level language that has sequencing of instructions, ability to select an execution path and iteration. The ability to jump is not required. For the times it was an critical discovery because it meant higher level language designers could proceed with the confidence that any HLL they designed with those three properties would be usable for any algorithm in theory.
Finally, Dijkstra (and the other European greats like Hoare and Wirth) had in mind a particular vision of what programming ought to be. He didn't see it so much as an engineering activity. He wanted to treat programs as mathematical objects. Controlling the scope of languages can make this more or less tractable. So for example the "one return from a function" rule goes back to this vision, but has since gone on to live a care-free life of its own.
[1] http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html http://www.u.arizona.edu/~rubinson/copyright_violations/Go_T...
- ygra 14y agoI think Hoare said it in a talk I once heard, that control structures essentially give a higher-level meaning to the ordinary GOTO. In that sense I always understood the “considered harmful” paper as advice to replace GOTO by appropriate control structures where applicable.
- jacques_chester 14y agoRight. That was part of the confusion, because as I said above, lots of programmers of the day didn't see the point in inventing a bunch of special cases when a simple general case existed.
- bcoates 14y agoIt's also important to realize that the "go to" Dijkstra is complaining about isn't seen in modern languages. It's the goto of unstructured Basic and early Fortran where you're doing an unconstrained jump to some other part of the program source by line number. It has no real counterpart in C. Unless your entire program is one long main() function, forward and even backwards C goto within a function is a tamed HLL version that can be reasoned about.
- unwind 14y agoIt is possible to reason about the effects of a goto into the middle of a for loop (or switch case), of course, but doing so is likely to make you angry. :) I believe those are reason enough to avoid doing those particular things, even if the global jumping of assembly of course is even worse.
- nitrogen 14y agoOf course, this is the part where someone mentions Duff's Device, which uses a do..while spanning multiple switch labels. This is possible because switch works a lot like goto. http://en.wikipedia.org/wiki/Duffs_device http://en.wikipedia.org/wiki/Duffs_device
- pi18n 14y agoI agree in the general case but there are many cases where a goto statement is actually more readable than the alternatives in C. Especially if you choose descriptive labels.
- jacques_chester 14y agoLabelled goto statements were around as early as ALGOL-60.
- heja2009 14y agoHowever, an unconstrained goto in C can be realized via the setjmp() longjump() library functions.
- tragomaskhalos 14y agoInteresting insight, thanks. The way that "goto considered harmful" has been taken out of context and then gone viral reminds me of how Simonyi's original Hungarian prefix scheme was similarly misunderstood and then misapplied, to the detriment of many C and C++ codebases.
- sophacles 14y agoA good discussion of what tragomaskhalos is talking about here: http://www.joelonsoftware.com/articles/Wrong.html http://www.joelonsoftware.com/articles/Wrong.html
- mattmanser 14y agoI found it enlightening at why GOTO is considered harmful by programming in BASIC as a kid. Oh-oh spaghetti-oh! Not that you should waste your time programming in a GOTO dependant language, but it becomes pretty obvious after a couple of days why GOTO sucks. The opening article is pretty vacuous imo. That's not really a legitimate use, it's just that C++ doesn't support finally.
- defrost 14y agoThe language is C, not C++, and it is in no way GOTO dependant. The examples given are ones of considered and deliberate use of GOTO by choice in a systems programming context. John Regehr knows his stuff and generally expects the same of his readers. Given his background as a harsh but fair critic of the ragged edges of the C Language standard the part of interest here is that he lets these examples go under the umbrella of "fine by me" That you found the article vacuous in your opinion is no surprise, at a guess I'd ascribe that to general ignorance on your part as born out by the comment you made. Never mind, you can rectify that with a bit of applied effort.
- mattmanser 14y agoI'm sorry, perhaps you can share what profound take away you gained from this article which boils down to 'my language doesn't have feature x. So shitty feature y is really meaningful'? Perhaps you could write an article about the usefulness of Option Explicit in vbscript to really underline the take away here. I'd love to see you defend the claim that [goto] has a number of legitimate uses in well-structured code including an idiom seen in systems code Defend that odd statement and you might have a claim on the superiority you're fatuously exuding. Personally I haven't seen a goto in live code apart from C for about 8 years. But perhaps I'm too durr hurr stupid to notice in the hundreds of different programs I've read.
- defrost 14y ago> this article which boils down to 'my language doesn't have feature x. So shitty feature y is really meaningful'? Actually that's not what this article is about at all, it's a small part of a much larger arc about C, C semantics, and LLVM. As I pointed out, you're ignorant of this. > Perhaps you could write an article about the usefulness of Option Explicit in vbscript to really underline the take away here. Irrelevant and unconnected. > I'd love to see you defend the claim that [goto] has a number of legitimate uses in well-structured code including an idiom seen in systems code No need, it's an argument that's been covered already, probably before you were born. As others have pointed out even Dijkstra conceded it's (appropriate) usefulness, he simply wished to promote structured code and the (at the time) endemic bad use of GOTO. > a claim on the superiority you're fatuously exuding. Just experience, and seemingly more than yours. > But perhaps I'm too durr hurr stupid to notice in the hundreds of different programs I've read. While I originally inferred ignorance and not stupidity on your part I concede that you are making a strong case for the latter.