TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

If goto statements are bad why does linux src have more than 10k of them?

31 pointsby ilovecookiesover 10 years ago

16 comments

jandrewrogersover 10 years ago
Contrary to popular reputation, there are a handful of use cases where &quot;goto&quot; makes the code simpler, cleaner, more readable, and (in rarer cases) faster. In these cases, you <i>should</i> 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.<p>The use cases for goto I see are primarily complex&#x2F;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.
评论 #8760855 未加载
评论 #8760963 未加载
评论 #8760788 未加载
评论 #8760953 未加载
评论 #8760783 未加载
评论 #8761046 未加载
estebankover 10 years ago
The original context of the &quot;GOTO considered harmful&quot; is lost on most of us, as at the time there were large swaths of developers that were trained <i>before</i> structured programming took off, so people were using global variables and GOTOs instead of procedures and functions. GOTO has its place, specially in C.<p>It is sinful to jump into a different function and pass around information as global state. GOTO as functionality is tangential in this matter.
评论 #8760856 未加载
评论 #8760987 未加载
评论 #8760862 未加载
kristopolousover 10 years ago
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.<p>This form of blind programming is terribly dangerous. It takes focus away from the task and placed on the process.<p>Engage with the problem, engage with the computer. Do not engage with autocratic decrees and programming equivalencies of heretic and kosher.<p>Goto does something specific. If you need that specific thing then that&#x27;s why it&#x27;s there. Ceremonial rituals be damned
thomasleeover 10 years ago
At a glance, looks like most of &#x27;em are used for error handling. Sort of a `try ... finally` for C. This is actually a fairly common pattern -- check out Python&#x27;s source code for more examples.<p>The other way I&#x27;ve seen it used is to break out of multiple levels of nested loops, though that&#x27;s far less common.<p>`goto` is bad when it&#x27;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&#x27;t be comprehended is where goto is &quot;bad&quot;.<p>goto isn&#x27;t inherently bad, it&#x27;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&#x27;s major beef with it)
NelsonMinarover 10 years ago
Linus himself has publicly said he doesn&#x27;t mind appropriate gotos. &quot;I think goto&#x27;s are fine, and they are often more readable than large amounts of indentation.&quot; Mind you this was 11 years ago, and it&#x27;s weird to quote him as some sort of oracle. But it seems relevant.<p><a href="https://web.archive.org/web/20120331202351/http://kerneltrap.org/node/553/2131" rel="nofollow">https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20120331202351&#x2F;http:&#x2F;&#x2F;kerneltrap...</a>
AdmiralAsshatover 10 years ago
They&#x27;re usually used for an error clean-up section at the bottom.<p>Zed Shaw introduces some debug macros here which make use of a goto:<p><a href="http://c.learncodethehardway.org/book/ex20.html" rel="nofollow">http:&#x2F;&#x2F;c.learncodethehardway.org&#x2F;book&#x2F;ex20.html</a><p>From that point on in the book, pretty much every function written has an &quot;error:&quot; label at the bottom where stuff gets cleaned up and memory freed. You can see his example code for it there.<p>Pretty useful macros, really. Context for the &quot;goto considered harmful&quot; always seems lost on young coders these days, unaware of the kind of spaghetti code that overuse of goto had caused.
评论 #8760860 未加载
steakejjsover 10 years ago
&quot;Goto is bad&quot; is something that professors tell first year computer science students because as computer scientists, the students will find novel ways of abusing them.<p>Using goto for error cleanup is pretty standard, easily readable, and understandable.<p>It avoids ugly braces and depth.<p>So, are gotos bad? It depends.
xantronixover 10 years ago
One of these days, &quot;gotos: The Good Parts&quot; will overshadow EWD&#x27;s oft-abused essay, and the world will be spared highly nested, pointy-arrow C resource cleanup code.
10098over 10 years ago
<a href="http://onlinehut.org/2011/10/goto-is-not-evil-okay/" rel="nofollow">http:&#x2F;&#x2F;onlinehut.org&#x2F;2011&#x2F;10&#x2F;goto-is-not-evil-okay&#x2F;</a>
peterkellyover 10 years ago
Because they&#x27;re not bad when you use them for actions which would go in a &quot;finally&quot; block in a language that supports exception handling.
zzzcpanover 10 years ago
I think what people are trying to say is that you shouldn&#x27;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.
CGamesPlayover 10 years ago
Even C# has the goto statement. <a href="http://weblogs.asp.net/stevewellens/why-goto-still-exists-in-c" rel="nofollow">http:&#x2F;&#x2F;weblogs.asp.net&#x2F;stevewellens&#x2F;why-goto-still-exists-in...</a>
beachstartupover 10 years ago
without a goto, you have to rely on functions&#x2F;return values&#x2F;pointers, which implies a function call (sometimes billions of times), which can have performance and design implications.
imanaccount247over 10 years ago
Because it is written in C. &quot;Goto statements are bad&quot; meant: lets start using languages that offer higher level constructs to replace the current usage of goto. Things like &quot;loops&quot; and &quot;functions&quot; and &quot;exceptions&quot;. C did not get the memo.
评论 #8761050 未加载
评论 #8760928 未加载
jarfilover 10 years ago
GOTO statements are not bad, they make your code run 10% faster while taking just 10 times as long to debug.<p>So if your code gets executed millions of times per second, like in some OS kernel, it may be wise to use GOTOs.<p>Otherwise, stay away.
评论 #8760950 未加载
Jokyover 10 years ago
This is because Linux Kernel source code is not representative of what usual software should look like. And also because C is terrible ;)
评论 #8760866 未加载