Hacker News new | past | comments | ask | show | jobs | submit login

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.



One 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.


The C `switch` statement and `goto` were made for each other, too.


Used like this, it is not much different than a try-catch block


Yeah and it's make sense to use it this way since there is no try-catch in C.


Reminds me of BASIC best practices:

    ON ERROR GOTO handler
    [try code here] 
    GOTO finally

    handler:
    [catch code here]

    finally:
    [finally code here]


Or Knuth's take on it: 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.


Not 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.


Kernels 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.


Well, many BASICs had GOSUB, which sort of gave you function calls (albeit sans parameters)


Yes, 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...


Gotos 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.


I'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!


I’ll name Linux my go-to argument in this whole debate.


Other gotos include:

- if-statements

- switch-statements

- break

- continue

- exception handling

The whole "gotos are bad" really needs to die.


The 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.


gotos arent’t bad, they’re just “considered harmful”


Dijkstra is dead and we can move on from his peeves.


If 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: