3 ms·
The goto of goto considered harmful is not the goto of C. Somehow this subtlety seems to be broadly missed. The goto of Dijkstra jumps to somewhere else in the
by JonChesterfield 3y ago
The goto of goto considered harmful is not the goto of C. Somehow this subtlety seems to be broadly missed.
The goto of Dijkstra jumps to somewhere else in the program. You're somewhere in the call stack and decide to scribble on some registers and jump to some arbitrary location. Like longjmp with the restrictions taken off. As in across function boundaries. When you're writing assembly that works exactly as it it sounds like it would.
The goto of C jumps to somewhere else in the same function with a bunch of compiler warnings if you manage to bypass an initialisation. You can make control flow more complicated within a single function, sure.
Goto in C is not a significant problem. Jumping between different functions in assembly is however legitimately confusing.
This is something of a pet annoyance because various languages or coding standards decide to outlaw the friendly easy goto as it has such a scary reputation. This makes some algorithms much more complicated to express and others inherently slower.
- samatman 3y agoI don't believe I came up with this one, but I haven't been able to source it: for a modern audience, "Go To Statements Considered Harmful" should probably be titled "Structured Programming Considered Beneficial". Structured programming was (comparatively) new, and advocating for it made sense, given that FORTRAN and COBOL were the major languages in use, and (at the time) didn't exactly encourage the style.
- kazinator 3y agoSomeone made a similar claim in HN recently. I reviewed Dijkstra's paper and it doesn't bear out the claim. The C goto falls squarely under the scope of the paper. Dijkstra's paper is not strictly focused on a global, machine-language goto that can branch anywhere in the address space. In fact, Dikjstra mention that he would also like to banish statement like while B repeat A and repeat B until A, because we have recursion, which makes them unnecessary. He lets them slide only for reasons of "realism" and because such loops are analyzable with inductive techniques.
- JonChesterfield 3y agoThere is imprecision in my wording. The paper was about removing goto from high level programming languages. The context in which people were starting to view goto with suspicion was that of ~1968, at which point I believe the baseline assumption was that goto was a branch to anywhere as that's what assembly gives you. However it was before my time and I'm inferring from partial information. I think the distinction between goto as rendered in C and goto as rendered in machine code is significant. It's the same argument in favour of structured programming in both cases. The gap between structured programming and local goto within a function is smaller than in the assembly case.
- kazinator 3y agoAlgol 60 already had a go to statement, restricted to jumping to local labels, similar to the one in C. It is only my speculation, but it seems very likely that Dijkstra had the Algol 60 and Algol 68 go to statements in mind. As in, an example high level language from which to banish the go to statement would have been whatever successor is planned for Algol 68.