Live data from Hacker News

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

github.com

11–20 of 48 posts

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

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

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

#12

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…

As long as they're local I guess, like a handmade `finally` clause. It's like mutation in functional programming languages, they're allowed as long as they don't leak. IIRC Zed Shaw's Learn C the Hard Way has some nice `goto` examples..

[deleted]

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

#13

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.

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

#14

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.

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

#15

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

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" :-)

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

#17

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

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

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