If goto statements are bad why does linux src have more than 10k of them?
1–10 of 48 posts
Re: If goto statements are bad why does linux src have more than 10k of them?
#2The other way I've seen it used is to break out of multiple levels of nested loops, though that's far less common.
`goto` is bad when it's used in an unstructured way: e.g. A does some stuff, then `goto B`. Then B does some stuff, then `goto C`. C sets a flag, does more stuff, then `goto B`. B does something different this time around based upon the flag set by C. Jumping around in a completely uncontrolled way such that the code can't be comprehended is where goto is "bad".
goto isn't inherently bad, it's just easy to use it in bad ways (and once upon a time, people did so more so than they generally do today -- which iirc was Dijkstra's major beef with it)
Re: If goto statements are bad why does linux src have more than 10k of them?
#3The use cases for goto I see are primarily complex/thorough error handling (e.g. unwinding some concurrency control mechanisms) and certain low-level state machine patterns. I would expect to see these kinds of use cases in the Linux kernel.
Re: If goto statements are bad why does linux src have more than 10k of them?
#4Re: If goto statements are bad why does linux src have more than 10k of them?
#5Contrary to popular reputation, there are a handful of use cases where "goto" makes the code simpler, cleaner, more readable, and (in rarer cases) faster. In these cases, you should use goto statements. A good programmer can recognize these cases and use goto appropriately. Goto is never necessary in a strict sense but occasionally it can eliminate some pretty ugly spaghetti code. The use cases for goto I see are pri…
Re: If goto statements are bad why does linux src have more than 10k of them?
#6Contrary to popular reputation, there are a handful of use cases where "goto" makes the code simpler, cleaner, more readable, and (in rarer cases) faster. In these cases, you should use goto statements. A good programmer can recognize these cases and use goto appropriately. Goto is never necessary in a strict sense but occasionally it can eliminate some pretty ugly spaghetti code. The use cases for goto I see are pri…
Re: If goto statements are bad why does linux src have more than 10k of them?
#7Zed Shaw introduces some debug macros here which make use of a goto:
http://c.learncodethehardway.org/book/ex20.html
From that point on in the book, pretty much every function written has an "error:" label at the bottom where stuff gets cleaned up and memory freed. You can see his example code for it there.
Pretty useful macros, really. Context for the "goto considered harmful" always seems lost on young coders these days, unaware of the kind of spaghetti code that overuse of goto had caused.
Re: If goto statements are bad why does linux src have more than 10k of them?
#8Re: If goto statements are bad why does linux src have more than 10k of them?
#9Using goto for error cleanup is pretty standard, easily readable, and understandable.
It avoids ugly braces and depth.
So, are gotos bad? It depends.