5 ms·
I've always stuck to my guns with goto, and will continue to do so. Any sufficiently large C codebase will have some areas where goto makes the code cleaner, le
by kstenerud 12y ago
I've always stuck to my guns with goto, and will continue to do so. Any sufficiently large C codebase will have some areas where goto makes the code cleaner, less error prone, and easier to refactor (most commonly in error handling code).
- Liru 12y agogoto is merely a tool that got a bad rap simply because it has its roots in basic assembler, back before structured programming was a thing. I agree with you regarding its use, but I'm still surprised by just how many seemingly educated programmers jump onto the "never use goto" cargo cult because of a paper title that they misattribute to Djikstra. Ironically, from the code I've seen, I don't think I've ever seen anyone actually misuse goto, probably because the people who use it actually know what they're doing. That's just anecdotal on my part, but still.
- rbanffy 12y ago> I don't think I've ever seen anyone actually misuse goto I've lived through the 80's, when 8-bit Microsoft BASIC was a popular programming language. Misuse of GOTO's was quite common, albeit unavoidable.
- jrochkind1 12y agoIf it was unavoidable, what made it "misuse"?
- spc476 12y agoWorse case, imagine a GOTO at the end of each line of code. There was no lexical scoping in BASIC [1] (and all variables were global but that's another topic) so one could literally GOTO any point in the program. [1] I'm talking about the BASIC one found on most 80s home computers at the time.
- dottrap 12y agoIronically, from the code I've seen, I don't think I've ever seen anyone actually misuse goto, probably because the people who use it actually know what they're doing. That's just anecdotal on my part, but still. This no longer is just anecdotal. An empirical study on this just got linked to on Slashdot the other day. http://developers.slashdot.org/story/15/02/12/1744207/empirical-study-on-how-c-devs-use-goto-in-practice-says-not-harmful http://developers.slashdot.org/story/15/02/12/1744207/empiri...
- MaulingMonkey 12y ago> Ironically, from the code I've seen, I don't think I've ever seen anyone actually misuse goto, probably because the people who use it actually know what they're doing. That's just anecdotal on my part, but still. I've misused goto at least once in the brave new world(tm) of structured programming. That said, once the cleaner way to do things occurred to me, I refactored it away immediately.
- api 12y agoGoto is the only way to cleanly and concisely implement state-teardown in exceptional conditions in C. The alternative is endless code duplication or to bloat your code with huge numbers of tiny functions with complex cascading call semantics.
- keenerd 12y agoHow large is "sufficiently large"? Usually I write error handling code that could use goto as a state machine. This makes the code much more readable because the whole activity should have been a state machine in the first place. (Teardown and clean up is merely another state.) Needing to use goto for foo typically means foo was an afterthought. Occasionally when I feel I need something goto-like in very small code I find it is more cleanly expressed as a `for(;;){}` loop with either break or continue as the jump points. I'm not saying goto is never a good idea. I just want to see a single good example.