5 ms·
I might be a bit too young, having mostly worked with Java-and-higher languages...what is a good usecase for goto? Most of the languages I use don't even have a
by tombert 4y ago
I might be a bit too young, having mostly worked with Java-and-higher languages...what is a good usecase for goto? Most of the languages I use don't even have an equivalent syntax.
- cephei 4y agoIt's fairly fundamental in languages, just hidden behind syntactic sugar like if/else and try/catch/finally. Outside of those it can be useful for de-duplicating cleanup and exit code from a function.
- garaetjjte 4y agoEscaping nested loops, cleanup during error handling.
- bombolo 4y agoyou put a cleanup label at the end and in case of any error set the return code and jump there, where all the buffers get freed. 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.
- Jtsummers 4y agoThe utility of a goto is: 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.
- tialaramex 4y ago> 2. There is no structured programming equivalent or no good structured programming equivalent within the language. 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.
- inconceivable 4y agoit's tricky because the kind of situations where goto is helpful are usually frowned upon, but sometimes they are unavoidable. like large nested state machines that you need to jump around in or skip parts of. this was more common with C. 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.