4 ms·
The contempt is justified. `goto`s break the flow and make the code harder to read and reason about, and thus make it easier to introduce bugs. It's an instruct
by scambier 5y ago
The contempt is justified. `goto`s break the flow and make the code harder to read and reason about, and thus make it easier to introduce bugs. It's an instruction that's almost never _needed_ today, because there is always a better structure to use instead.
I only use it in Lua, to compensate for a lack of `continue` in loops.
- cyberpunk 5y agocybrpnk:~/src/linux$ grep -rniE 'goto\W+.*;' . | grep -E '\.[c|h]:' | wc -l 182044 Sure.. I mean.. 'almost'
- dan-robertson 5y agoIt’s worth noting: 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.
- _0ffh 5y agoMeh, that's just the kind of dealing in absolutes that gets on some peoples' nerves. I agree that in almost all situations gotos are probably not the best choice, but there are still situations where gotos are a reasonable option, arguably even the best that's available.