Live data from Hacker News

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

github.com

41–48 of 48 posts

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

#41

Earlier quoted context omitted.

> It is sinful to jump into a different function I've heard of that. It's called "exceptions", right? I find it kind of amusing when people are afraid of "goto" but not "throw", "break" or "return".

You skipped the "and pass information around as global state" part. It obviously depends on your language, but ideally an Exception object should be a specific class you can choose to catch or re-raise, and it should include a message. None of the calling state should be visible to you.

I give you that I skipped it. I skipped it intentionally to make a bit of hyperbole.

An exception is still essentially a goto that crosses stack frames. There is a whole class of common bugs, you will see a lot of talk of them in the C and C++ communities, where an early return or a throw at an unexpected time leads to resources not being cleaned up. A lot of good coding styles say avoid this stuff, it's dangerous, and they are not without cause. Whereas a goto cleanup block is often a lot more explicit and therefore can lead to more reliable code than a scheme that encourages not sweating details and not expecting failure at every step.

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

#42

Earlier quoted context omitted.

>It is sinful to jump into a different function So sinful you can't in C

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

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.

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

#43

Earlier quoted context omitted.

Growing up in the 80s, that's how a lot of books taught us BASIC, because they catered to a lowest-common-denominator implementation. Fortunately, I was using BBC BASIC, with functions, procedures and REPEAT-UNTIL (and later WHILE and CASE), and the BBC-specific books taught structured programming.

On error GOTO next

The empty catch block of BASIC.

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

#44

Earlier quoted context omitted.

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

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

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 than 10k of them?

#45

Earlier quoted context omitted.

On error GOTO next

The empty catch block of BASIC.

For every line of code (just to be pedantic).

I have customers still churning out wads Classic ASP who think On Error Resume Next magically fixes their shabby code. Makes me weep.

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

#46
post #40

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.

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

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

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

#47

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.

C has loops and functions. But you're right that modern use of goto is typically used in error handling to replace specific use cases of exceptions in a primitive way (If error, goto end of function where cleanup is done before the return statement.)

>C has loops and functions

Which leaves...

> But you're right that modern use of goto is typically used in error handling to replace specific use cases of exceptions in a primitive way

Exactly. I pointed out the context of "goto considered harmful". Obviously the one that applies to C is the one that is the reason for all the gotos in C code.

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

#48
post #31
post #19

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.

Gotos are nowhere near as hard to debug as exceptions.

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.
Post reply on HN