5 ms·
> Last I checked they're almost as big no-nos as using goto. Huh? Why is goto a no-no? It is there for good reason. I think we all agree with Dijkstra that, in
by randomdata 3y ago
> Last I checked they're almost as big no-nos as using goto.
Huh? Why is goto a no-no? It is there for good reason. I think we all agree with Dijkstra that, in his words, unbridled gotos are harmful, but C's goto is most definitely bridled. I doubt any language created in the last 50+ years has unbridled gotos. That's an ancient programming technique that went out of fashion long ago (in large part because of Dijkstra).
- School-Cotton 3y agogoto used in certain idiomatic ways (e.g. to jump to cleanup code after an error, or to go to a `retry:` label, or to continue or break out of a multiply nested loop) is fine. What's annoying is bypassing control flow with random goto spaghetti.
- bluGill 3y agoLanguages other than C give you options for flow control so that you don't need goto for that. It is a spectrum, if you only use goto to jump to the end of a small function on error it is okay, though I prefer something better in my language. I've seen 30,000 line functions with gotos used for flow control (loops and if branches) - something you can do in C if you are really that stupid and I think we will all agree is bad. This 30,000+ line function with gotos as flow control was a lot more common in Dijkstra's day.
- lelanthran 3y ago> Languages other than C give you options for flow control so that you don't need goto for that. The idiom `if (error) goto cleanup` is about the only thing I see goto used for. What flow control replaces that other than exceptions?
- randomdata 3y ago> What flow control replaces that other than exceptions? defer has gained in popularity for that situation.
- sirwhinesalot 3y agoJumping out of nested loops. Implementing higher level constructs like yield or defer. State machines. Compiler output that uses C as a "cross-platform" assembly language. All of them are better served with more specialized language constructs but as a widely applicable hammer goto is pretty nice. I don't expect C to have good error handling or generators any time soon but with goto I can deal with it.
- lelanthran 3y agoI'm actually familiar with this, having used libprotothreads in production for about 4 years. Something like libprotothreads can't actually be implemented in a language that doesn't have gotos, so yeah, I see the need for it.
- nickpsecurity 3y agoCompiling HLL constructs in some of those scenarios ultimately produces a jump statement. So, it makes sense that a higher-level version of a jump would be helpful in the same situations.
- cozzyd 3y agoRAII + destructors Though gcc supports cleanup functions, just not very ergonomically.
- randomdata 3y agoWe all agree that you shouldn't write bad code. Not using goto, not using any language construct. But when unbridled gotos were the only tool in the toolbox, bad code was an inevitability in a codebase of any meaningful size. Not even the best programmer was immune. This is what the "Go to statement considered harmful" paper was about. It was written in 1968. We listened. We created languages that addressed the concerns raised and moved forward. It is no longer relevant. Why does it keep getting repeated in a misappropriated way?
- bluGill 3y agoIn 1968 they had better languages and programmers were still using goto for control in them despite better options.
- deleted 3y ago[deleted]
- randomdata 3y agoOf course. The ideas presented in said paper went back at least a decade prior, but languages were still showing up with unbridled gotos despite that. But that has changed in the meantime. What language are you or anyone you know using today that still has an unbridled goto statement?
- jjav 3y ago> 30,000 line functions with gotos The problem there is the 30K line function, not the goto!
- bluGill 3y ago30k functions are a problem but they are manageable if goto isn't used in them. I prefer not to but a have figured them out.
- jjav 3y agoWow! Longest single function I can think of having written is ~200 lines. I always feel bad when editing it but there's no useful way to break it down so I let it be. But a single 30,000 line function? Wow.
- tazu 3y agoI'll take a 30k line function that does one thing over 30 1k line functions that are used once...
- jjav 3y agoAgreed! Breaking into multiple functions for no reason other than style isn't smart either.