5 ms·
goto is common in C and C++ code as a clean way to consolidate cleanup code without using exceptions. I imagine that's the main use case they wanted to preserv
by taybin 13y ago
goto is common in C and C++ code as a clean way to consolidate cleanup code without using exceptions. I imagine that's the main use case they wanted to preserve.
- heydenberk 13y agoStill, though, goto is considered so harmful that it's inspiration for "considered harmful" phrasing. Apple's goto-related SSL vulnerability this week is a blatant, high-profile reminder of how harmful it is.
- deleted 13y ago[deleted]
- dmunoz 13y ago> Still, though, goto is considered so harmful that it's inspiration for "considered harmful" phrasing. It gets tiring constantly hearing this. We discussed it in depth 18 days ago [0]. I'm going to copy and paste something I said then, as it's relevant to your post: > Also, a minor rant here: Dijkstra's "Go To Statement Considered Harmful" is often mentioned, sometime wordlessly, when goto is brought up, but it seems many of those people misunderstood what was actually being argued. Although he mentions being "convinced that the go to statement should be abolished from all 'higher level' programming languages," his main gripe was that the "unbridled use of the go to statement has an immediate consequence that it becomes terribly hard to find a meaningful set of coordinates in which to describe the process progress." He felt it was "as it stands is just too primitive; it is too much an invitation to make a mess of one's program." So, as others have pointed out already, his main issue was with goto being used in a way that resulted in unstructured, hard to understand programs. [0] https://news.ycombinator.com/item?id=7191201 https://news.ycombinator.com/item?id=7191201 Edit: The original paper [1] is so short, it might as well be required reading before people bring up goto near any mention of "considered harmful". [1] http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html http://www.u.arizona.edu/~rubinson/copyright_violations/Go_T...
- mrbrowning 13y agoApple's goto-related SSL vulnerability this week is a blatant, high-profile reminder of how harmful unbraced conditionals are. The same bug could have happened with two return statements in a row.
- bjz_ 13y agoI'm really thankful Rust does away with them. Static analysis can catch these things, but it should be prevented in the grammar from the start.
- dragontamer 13y agoAmen. Replace the "goto" with "Throw", "break", or "return", or any other construct, and you'd come up with the same problem.
- jfb 13y agogoto cleanup; /* ... */ cleanup: foo(); ... is perfectly idiomatic C. It was the brace style that was the real stinker there.
- pkroll 13y agoSteve McConnell, in Code Complete, said in 1993: "These articles contain the whole goto debate. It erupts from time to time in most workplaces, textbooks, and magazines, but you won't hear anything that wasn't fully explored 20 years ago." Thankfully he's got the whole section on his site. http://www.stevemcconnell.com/ccgoto.htm http://www.stevemcconnell.com/ccgoto.htm
- duaneb 13y ago> goto is common in C and C++ code as a clean way to consolidate cleanup code without using exceptions. Most of the reason for this is the lack of higher level language constructs (e.g. `finally`). Keeping goto around for cleanup reminds of me of performing demolition with a nuclear bomb.
- deleted 13y ago[deleted]
- dmunoz 13y agoC++ has strong opinions on why it is lacking finally [0], and D has both try...catch...finally, and scope statements e.g. scope(exit). Personally, I'm not sold that this is enough to argue for the removal of goto. I don't think goto-cleanup is the only sane use case of goto. [0] http://www.stroustrup.com/bs_faq2.html#finally http://www.stroustrup.com/bs_faq2.html#finally
- oofabz 13y agoGoto is also very useful for machine-generated state machines, like Ragel or yacc parsers. I'd guess this is the only reason Go has goto, because it has "defer" for cleanup, which is functionally equivalent to "finally".
- dragontamer 13y agoCommon in C. C++ should focus on RAII destructors, such as std::lock_guard. I can't think of a legitimate time to use "goto" in C++, unless you were forced to emulate C for some reason.
- ternaryoperator 13y agoIt can be useful in deeply nested loops. One can certainly argue about the wisdom of deeply nested loops whose complexity requires a goto statement, but sometimes you just need to pop out of a loop to somewhere in a higher loop and goto solves that problem effectively.
- dragontamer 13y agoFair point. IIRC, Java and C# allow "break" statements to have a label associated with them specifically for this case. C++ "break" statements are all implicit. So a goto is the only thing that can solve this problem.