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.
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 earlierNote 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;
}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 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.n... )
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.
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 :)
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.
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.
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.
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.
when used properly it should be really obvious what it's doing, where it's going, and shouldn't produce any weird side effects. jumping into another function or module or file isn't good.
Structured programming won.