I think the underlying reason is that it is annoying to write tree walking interpreters and implement goto:s. (How do you even do that?). So the prominent CS folks at the time just dismissed goto:s instead by bullying user to submission.
I only use it in Lua, to compensate for a lack of `continue` in loops.
cybrpnk:~/src/linux$ grep -rniE 'goto\W+.*;' . | grep -E '\.[c|h]:' | wc -l
182044
Sure.. I mean.. 'almost'1. One need for goto in C is for what is done with labelled loops in other languages. So one might expect uses there
2. Another use is for cleanup after errors which would be automatically handled by the compiler (via destructors) in most more modern non-gc languages. It looks like:
int do_thing(args…){
foo *x; bar *y; baz *z; int err;
if((err = make_foo(&x, …)) < 0) goto exit3;
if((err = make_bar(&y, …)) < 0) goto exit2;
if((err = make_baz(&z, …)) < 0) goto exit1;
frob(foo, bar, baz);
err = 0; /* maybe use x,y,z and return instead of cleanup */
free_baz(z);
exit1: free_bar(y);
exit2: free_foo(x);
exit3: return err;
}
So really all you’re proving is that C does not provide sufficiently useful structured programming tools to avoid goto. Java handles this fine for memory but not so nicely for objects that have more involved cleanup that shouldn’t be delayed (e.g. open files, removing from some parent object, etc). Indeed Java also doesn’t provide great tools for protecting cleanup code from stack unwindings, or at least they aren’t sufficiently ergonomic that you see them used everywhere.It certainly isn’t the case that Linux is full of old-school do-whatever-you-like uses of goto.
But the contempt goes further than "goto vs subroutine calls" or "goto vs the if-else construct".
With "underlying reason" I meant more like "underlying reason for the contempt of all use of goto". Even when goto makes the code less spaghetti and more readable.
Knuth did a good write up on the matter: https://pic.plover.com/knuth-GOTO.pdf
> I think the underlying reason is that it is annoying to write tree walking interpreters and implement goto:s
I don't believe that there is any historical justification for this view. And your updated statement is even less historically sound. Everything written about go to statements (as a negative, something to partially or fully eliminate) seems to have been predicated on reasonability and comprehensibility of the code by people, not the ability to write tree walking interpreters. Which is a minor activity when reviewing the totality of all computer programming activities of the past 70 or so years.