4 ms·
Goto is a perfectly good command in moderation. So, the first instance of “Considered Harmful” was harmful; the pithy title combined with the famous author has
by bee_rider 4y ago
Goto is a perfectly good command in moderation. So, the first instance of “Considered Harmful” was harmful; the pithy title combined with the famous author has resulted in a disproportionate response (the total loss of this perfectly decent bit of syntax).
- Jtsummers 4y agoIt was only harmful to the people that read the title and not the actual letter. Dijkstra's take was much more moderate than most people seem to think it was, and the title wasn't even his.
- CoolGuySteve 4y agoYeah, the angst about goto is misplaced. Dijkstra was mostly talking about control flow jumping all over the program, ie spaghetti code. So it's funny when people rigidly adhere to the goto ban while simultaneously littering a bunch of tiny single use functions and classes all over their supposedly well-designed code.
- phantom784 4y ago"break" and "continue" are both effectively a "goto", and they're pretty commonly used.
- Jtsummers 4y agoThey are only equivalent to a very limited goto, not the unlimited sort that Dijkstra's letter was primarily about. A continue or break can be replaced with this: while (some_cond) { if (whatever) continue; if (whocares) break; } => while (some_cond) { next: if (whatever) goto next; if (whocares) goto after; } after: ... EDIT: added break into the above, forgot to earlier Note that it stays within the same scope or, in the case of a break, would just escape that scope. The sort of goto Dijsktra wrote about would let you do things like this: void foo() { while(some_cond) { next: // how did we get here again? ... } } void bar() { goto next; }
- shagie 4y agothrow, raise, catch, switch... even if and else. They're all wrappers around goto with various amounts of safety in their handling. Some implementations are more safe than others ( https://www.php.net/manual/en/control-structures.break.php https://www.php.net/manual/en/control-structures.break.php still gives me the heebie jeebies thought it was worse in older versions where the number could be a variable rather than an integer literal https://web.archive.org/web/20140312123653/https://www.php.net/manual/en/control-structures.break.php https://web.archive.org/web/20140312123653/https://www.php.n... )
- pyrale 4y agoMany control flow instructions can be expressed as other instructions. The idea of the article was to promote higher level, less ambiguous and less ubiquitous instructions. Sure, you can express a for-loop in terms of goto. But when you read a for-loop, there are many things you're guaranteed won't happen that could with a goto. In that sense, it takes less attention to make a good for-loop. And a for-loop iterating over a collection also takes less attention than one with a manually-crafted iteration check. That's the whole point of the article: lower-level instructions should be wrapped in higher-level instruction, more specialized and with less footguns.
- hn8305823 4y agoIn 1968 nobody was using 'goto' in any way resembling moderation.
- Gibbon1 4y agoWhile Dijkstra was arguing against jmp instructions other people were trying to get a FORTRAN compiler to execute out of 32k of core memory.
- tombert 4y agoI might be a bit too young, having mostly worked with Java-and-higher languages...what is a good usecase for goto? Most of the languages I use don't even have an equivalent syntax.
- cephei 4y agoIt's fairly fundamental in languages, just hidden behind syntactic sugar like if/else and try/catch/finally. Outside of those it can be useful for de-duplicating cleanup and exit code from a function.
- garaetjjte 4y agoEscaping nested loops, cleanup during error handling.
- bombolo 4y agoyou put a cleanup label at the end and in case of any error set the return code and jump there, where all the buffers get freed. Otherwise you have to free all the buffers in every place where an error might happen, and you will most likely forget to free some or double-free.
- Jtsummers 4y agoThe utility of a goto is: 1. Sometimes more efficient code (state machines can make really good use of this pattern) 2. There is no structured programming equivalent or no good structured programming equivalent within the language. For (2), you see it a lot in the Linux kernel (and similar) code where it's utilized for early exits. Where a language like Go might use a defer statement or Java and similar might use a try/catch/finally and exceptions, C would put a bunch of clean up statements after one or more labels at the end of the function before the single return. Which label you go to (if you use more than one) would depend on how far into the function you've gotten.
- tialaramex 4y ago> 2. There is no structured programming equivalent or no good structured programming equivalent within the language. My emphasis. C is barely younger than Dijkstra's letter, and by comparison to a modern language it is impoverished when it comes to structural flow control. Still, you won't find modern C programmers arguing that they don't see any use for while loops, or that switch is pointless because they could just use longjmp.
- moron4hire 4y agoI think it's important to remember that Dijkstra was a bit of a troll. Well actually, not just a bit. Like, really, really a troll.
- spacechild1 4y agoI recently had to work with a small FORTRAN IV code base from the early 1960s where all control flow is done with gotos. That's what Dijkstra was arguing against, not the moderate modern use.
- albert_e 4y agoWhen I got my very first introduction to computers and programming as a middle schoolkid in the early 90s India ... I got home the set of textbooks for BASIC programming language from the institute -- the classes had not yet started. So I knew next to zero about programming and barely touched a computer. I sat down and read through the books. They were all well-written and most of it made sense (whatever I could make sense of -- having never written a program before) All commands seemed to have some logical names : LET, IF ... THEN ..ELSE, FOR, PRINT, INPUT, etc. But one command stumped me. GOTO. I was not a native speaker of English and wondered if it is an obscure english word. (My mind defaulted to sounding it as a single word, "lotto" with a G) I read the explanation and that sounded okayish -- make the program execution jump to a line number specified after the GOTO command. But what the hell was "GOTO" -- why did they name it that -- I had no clue. It took embarassingly long for me to realize that it was a rare command (the only one perhaps?) in BASIC language that is not a single English word but simply a combination of the simple words 'go' and 'to' :) So I have a special place in my heart for this command :)
- d0mine 4y agoThe argument is for structured programming against equivalent goto code. Structured programming won.
- pyrale 4y ago> Goto is a perfectly good command in moderation. So, the first instance of “Considered Harmful” was harmful You're fighting yesterday's battle, and you're siding with the losing side. The ideas from this article have been the trend for new programming language innovations for several decades now, and it has yet to cause any harm.