5 ms·
I spied a goto. Ctrl/Command+F spies 60 gotos. CS students: point to the Linux source code if your professor ever gives you grief about gotos.
by lapetitejort 5y ago
I spied a goto. Ctrl/Command+F spies 60 gotos. CS students: point to the Linux source code if your professor ever gives you grief about gotos.
- umvi 5y agoGotos can be used to effectively accomplish try/catch in C. As long as you stick to a safe goto idiom like this then they can be useful. In general, an inexperienced programmer taught that goto is okay is probably not going to stick to safe idioms and will instead create spaghetti code, hence why they are steered away.
- SAI_Peregrinus 5y agoI'd be more likely to use setjmp/longjmp to accomplish try/catch in C than goto. Goto is fine for error handling, but exceptions are a bit different, since they have the try/catch/finally structure. Personally I prefer to avoid exceptions, but if I were forced to implement them I'd not use goto!
- dblohm7 5y agoOne of the first codebases I ever worked on professionally allowed gotos as part of its style guide, but only under two conditions: 1. You're using the goto for error handling and cleanup in a function; and 2. You only jump in a forward direction.
- TheSockStealer 5y agoUsed like this, it is not much different than a try-catch block
- SXX 5y agoYeah and it's make sense to use it this way since there is no try-catch in C.
- jimmygrapes 5y agoReminds me of BASIC best practices: ON ERROR GOTO handler [try code here] GOTO finally handler: [catch code here] finally: [finally code here]
- thechao 5y agoThe C `switch` statement and `goto` were made for each other, too.
- anyfoo 5y agoKernels written in C are often full of gotos. There's nothing wrong with gotos per se, it's only harmful if that's your (or one of your) primary means of control flow, e.g. because you don't have other good options. BASIC for example had that problem back in the day.
- ArnoVW 5y agoWell, many BASICs had GOSUB, which sort of gave you function calls (albeit sans parameters)
- anyfoo 5y agoYes, but that was pretty much it: GOTO, GOSUB, and NEXT. And GOSUB is pretty much useless for building control flow equivalent to while/try/catch/... and even just if/else, or only "if" with more than one statement. NEXT works for only special cases of "for". So, most of the time, GOTO it is...
- Jtsummers 5y agoOr Knuth's take on it: https://pic.plover.com/knuth-GOTO.pdf https://pic.plover.com/knuth-GOTO.pdf [pdf] Structured Programming with go to Statements. Which helps to clarify why go to statements are both useful and worth minimizing, from a person who makes extensive use of goto statements in a lot of their code.
- alkonaut 5y agoNot all gotos are made equal. A hundred gotos jumping forward to the same cleanup/error tail is very readable and actually reduces complexity over alternatives - which it’s obviously why it’s used. In many languages this type of construct has the form of try/catch/finally but those are glorified forward gotos. Seeing even one goto jumping backward would be much more surprising, and seeing what made gotos get their bad rep, overlapping goto regions, would be completely shocking in any quality code base.
- sshine 5y agoI’ll name Linux my go-to argument in this whole debate.
- CodeGlitch 5y agoOther gotos include: - if-statements - switch-statements - break - continue - exception handling The whole "gotos are bad" really needs to die.
- klyrs 5y agoDijkstra is dead and we can move on from his peeves.
- Jtsummers 5y agoIf you think it was a "peeve" or that he considered go to statements universally bad, maybe you should actually read the letter he wrote (NB: The title was not his, but Wirth's) and the discussion around it.
- throwaddzuzxd 5y agoThe whole point of "gotos are bad" is that they don't carry semantics. All your list is composed of semantic gotos, and are not considered bad.
- sgerenser 5y agogotos arent’t bad, they’re just “considered harmful”