It is sinful to jump into a different function and pass around information as global state. GOTO as functionality is tangential in this matter.
If goto statements are bad why does linux src have more than 10k of them?
11–20 of 48 posts
Re: If goto statements are bad why does linux src have more than 10k of them?
#12Contrary 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..
Re: If goto statements are bad why does linux src have more than 10k of them?
#13Contrary 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…
Re: If goto statements are bad why does linux src have more than 10k of them?
#14The 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…
Re: If goto statements are bad why does linux src have more than 10k of them?
#15They'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…
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?
#16Re: If goto statements are bad why does linux src have more than 10k of them?
#17The 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…
So sinful you can't in C
Re: If goto statements are bad why does linux src have more than 10k of them?
#18This is because Linux Kernel source code is not representative of what usual software should look like. And also because C is terrible ;)
Re: If goto statements are bad why does linux src have more than 10k of them?
#19So 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.