Live data from Hacker News

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

github.com

31–40 of 48 posts

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

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

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

#32

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 pri…

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

#34

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 functionali…

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

With longjmp you can. Your stack will be trashed, though, so any state better be global!

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

#35

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 pri…

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

#36

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 functionali…

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

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

#37
post #32

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 pri…

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.

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

#38

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 functionali…

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

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

#39

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 pri…

Ah yes, the "good" programmer that is well versed in when goto statements are warranted.

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

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

Post reply on HN