Live data from Hacker News

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

github.com

21–30 of 48 posts

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

#21

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…

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

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

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

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

#23

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

I assumed the parent poster meant back before functionality was expressed as functions; i.e. it's sinful to jump from functionality A to functionality B if it's not expressed in terms of packaged functions, and impossible to do so if it is.

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

#24

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

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

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

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

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

#27

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

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

#28

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…

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.

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

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

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

#30

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…

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

It does have longjmp, which can do basically the same, just without safety (surprise!)
Post reply on HN