
If goto statements are bad why does linux src have more than 10k of them? - ilovecookies
https://github.com/torvalds/linux/search?p=100&q=goto&utf8=%E2%9C%93
======
jandrewrogers
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.

~~~
wcummings
And C doesn't have exceptions, which otherwise would cover a lot of where
goto's are used.

~~~
shortsightedsid
There is a difference though. Exceptions can result in Stack Unwinding which
is costly. Gotos are a jump which costs literally nothing.

~~~
asveikau
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.

------
estebank
The original context of the "GOTO considered harmful" is lost on most of us,
as at the time there were large swaths of developers that were trained
_before_ structured programming took off, so people were using global
variables and GOTOs instead of procedures and functions. GOTO has its place,
specially in C.

It is sinful to jump into a different function and pass around information as
global state. GOTO as functionality is tangential in this matter.

~~~
wcummings
>It is sinful to jump into a different function

So sinful you can't in C

~~~
astrange
You can do it in GNU C! It's used to write direct-threading interpreters.

[https://gcc.gnu.org/onlinedocs/gcc-3.1/gcc/Labels-as-
Values....](https://gcc.gnu.org/onlinedocs/gcc-3.1/gcc/Labels-as-Values.html)

~~~
knome
They don't intend for label addresses to be gone to outside of the function
they are located in. The stack wouldn't be setup to handle them at all.

Sure, you can store them wherever, and do something like " goto
*g_greenthreads[ threadno ].lastpos ", but you would call that within the
function defining the label, not outside it.

From your own link: > You may not use this mechanism to jump to code in a
different function. If you do that, totally unpredictable things will happen.
The best way to avoid this is to store the label address only in automatic
variables and never pass it as an argument.

------
kristopolous
Dogmatically following so called rules in a cargo cult programming style
without any understanding of the rationale behind them is many orders of
magnitude more bad.

This form of blind programming is terribly dangerous. It takes focus away from
the task and placed on the process.

Engage with the problem, engage with the computer. Do not engage with
autocratic decrees and programming equivalencies of heretic and kosher.

Goto does something specific. If you need that specific thing then that's why
it's there. Ceremonial rituals be damned

------
thomaslee
At a glance, looks like most of 'em are used for error handling. Sort of a
`try ... finally` for C. This is actually a fairly common pattern -- check out
Python's source code for more examples.

The 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)

------
NelsonMinar
Linus himself has publicly said he doesn't mind appropriate gotos. "I think
goto's are fine, and they are often more readable than large amounts of
indentation." Mind you this was 11 years ago, and it's weird to quote him as
some sort of oracle. But it seems relevant.

[https://web.archive.org/web/20120331202351/http://kerneltrap...](https://web.archive.org/web/20120331202351/http://kerneltrap.org/node/553/2131)

------
AdmiralAsshat
They're usually used for an error clean-up section at the bottom.

Zed Shaw introduces some debug macros here which make use of a goto:

[http://c.learncodethehardway.org/book/ex20.html](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.

~~~
kabdib
Yeah, I've been on teams that have used this kind of error handling
extensively, at several different companies. It's lightweight, very little is
being done behind your back (do _you_ know when your destructors are being
called? really?) and it's easily inspected.

Also, never pass up the opportunity to troll a god-fearing "goto puritan" :-)

------
steakejjs
"Goto is bad" is something that professors tell first year computer science
students because as computer scientists, the students will find novel ways of
abusing them.

Using goto for error cleanup is pretty standard, easily readable, and
understandable.

It avoids ugly braces and depth.

So, are gotos bad? It depends.

------
xantronix
One of these days, "gotos: The Good Parts" will overshadow EWD's oft-abused
essay, and the world will be spared highly nested, pointy-arrow C resource
cleanup code.

------
10098
[http://onlinehut.org/2011/10/goto-is-not-evil-
okay/](http://onlinehut.org/2011/10/goto-is-not-evil-okay/)

------
peterkelly
Because they're not bad when you use them for actions which would go in a
"finally" block in a language that supports exception handling.

------
zzzcpan
I think what people are trying to say is that you shouldn't be taking too
seriously all these advises about good and bad features in programming
languages. Instead, try to understand the reasons behind such advises. All the
bad things, that goto is blamed for, can be just as easily made with a bunch
of methods in some object.

------
CGamesPlay
Even C# has the goto statement. [http://weblogs.asp.net/stevewellens/why-goto-
still-exists-in...](http://weblogs.asp.net/stevewellens/why-goto-still-exists-
in-c)

------
beachstartup
without a goto, you have to rely on functions/return values/pointers, which
implies a function call (sometimes billions of times), which can have
performance and design implications.

------
imanaccount247
Because it is written in C. "Goto statements are bad" meant: lets start using
languages that offer higher level constructs to replace the current usage of
goto. Things like "loops" and "functions" and "exceptions". C did not get the
memo.

~~~
10098
> Because it is written in C.

Writing something in C does not automatically imply having to use goto.

> "Goto statements are bad"

Goto statements are not bad.

> Things like "loops" and "functions" and "exceptions". C did not get the
> memo.

C has loops and functions. Bro do you even K&R?

It might be a good idea to know what you're going to be talking about before
opening your mouth.

~~~
imanaccount247
>Writing something in C does not automatically imply having to use goto.

I didn't say it did. But any large enough C project will obviously use them.

>Goto statements are not bad.

Do you know what quotation marks are?

>C has loops and functions

It does not have exceptions. Which is what all the goto uses in question are
being used for.

>It might be a good idea to know what you're going to be talking about before
opening your mouth.

How ironic.

------
jarfil
GOTO statements are not bad, they make your code run 10% faster while taking
just 10 times as long to debug.

So if your code gets executed millions of times per second, like in some OS
kernel, it may be wise to use GOTOs.

Otherwise, stay away.

~~~
vardump
Gotos are nowhere near as hard to debug as exceptions.

~~~
pontifexa
How's that? To debug GOTOs you have to place breakpoints on each possible
detection of error (or at a shared trampoline). To debug exceptions, you just
enable first-chance exception handling for the kind of exception you're
interested in and you're done.

------
Joky
This is because Linux Kernel source code is not representative of what usual
software should look like. And also because C is terrible ;)

~~~
10098
Please get some experience writing kernel and driver code before making inane
statements.

