News for hackers. Stuff that matters.
Re:If goto statements are bad why does linux src have more t... by jandrewrogers on Tuesday December 16, 2014 @09:12PM (#8760733)
Contrary 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 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 t... by wcummings on Tuesday December 16, 2014 @09:55PM (#8760855)
And C doesn't have exceptions, which otherwise would cover a lot of where goto's are used.
Re:If goto statements are bad why does linux src have more t... by shortsightedsid on Tuesday December 16, 2014 @10:27PM (#8760930)
There is a difference though. Exceptions can result in Stack Unwinding which is costly. Gotos are a jump which costs literally nothing.
Re:If goto statements are bad why does linux src have more t... by asveikau on Tuesday December 16, 2014 @11:36PM (#8761095)
In practice you will do the same thing as unwinding the stack. You'll jump to the end of a function where you'll likely free some heap buffers on your way out and you'll return error status to the caller. What will likely happen next is the caller will bubble up the error: free buffers and report error status to their caller. It works out to pretty much the same thing as a modern C++ code base using RAII, just more manual.
Re:If goto statements are bad why does linux src have more t... by tormeh on Tuesday December 16, 2014 @10:34PM (#8760946)
It does have longjmp, which can do basically the same, just without safety (surprise!)
Re:If goto statements are bad why does linux src have more t... by ilovecookies on Tuesday December 16, 2014 @10:39PM (#8760963)
Seems like they use it for most of the error handling, that's true. Almost as many of these as there are switch statements (and 1/3 of if statements). It would be cool if someone could analyze keywords from different projects. Maybe one could give that as use cases in universites instead, that would seem to be alot more practical.

EDIT: Upon closer inspection it seems like even if 'gotos' make everything nastier the extra added performance is worth it.

Re:If goto statements are bad why does linux src have more t... by agumonkey on Tuesday December 16, 2014 @09:30PM (#8760788)
As long as they're local I guess, like a handmade `finally` clause. It's like mutation in functional programming languages, they're allowed as long as they don't leak. IIRC Zed Shaw's Learn C the Hard Way has some nice `goto` examples..
Re:If goto statements are bad why does linux src have more t... by zzzcpan on Tuesday December 16, 2014 @10:36PM (#8760953)
Breaking from nested loops with goto is also cleaner and simpler. Which is actually one of the reasons some higher level languages even have a goto statement.
Re:If goto statements are bad why does linux src have more t... by _delirium on Tuesday December 16, 2014 @10:57PM (#8761007)
For that use-case in particular, a "labeled break" is an alternative that some languages use (like java). I somewhat semantically prefer it if given the choice (name the loop being broken, vs. naming the end-point to jump to), though that preference isn't strong.
Re:If goto statements are bad why does linux src have more t... by dmux on Tuesday December 16, 2014 @11:11PM (#8761046)
Ah yes, the "good" programmer that is well versed in when goto statements are warranted.