Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
I must admit...I've never quite understood the jokes around GOTO because I've never seen it or used it.
If it was used in early languages that didn't really have functions I don't intuitively understand why it was so hated.
Isn't calling a function quite similar to a GOTO ?
(Except the line number is referenced with a function name which avoids what I imagine must be a horrid hassle of having to update the line number every time more code was added) -
@jiraTicket Unlike a function call, goto doesn't return.
Goto does have its place, especially in stacked error handling, but the goto we have now is nothing like the old school goto. Modern goto jumps only within functions, the old one could jump anywhere. -
@jiraTicket
https://nakedsecurity.sophos.com/20...
A security bug in TLS handling that is based on go-to - "Now the C code proceeds as follows:" if you just want to see the code and skip the rest of the lengthy explanations.
Goto jumps to a predefined label "somewhere" as FastNop explained.
If you look at the mentioned code, think about the flow of code, then you should be able to answer your question.
To point it out: There is no flow of code.
If you e.g. define a lot of intermediary variables and try to define a common goto fail routine like in the TLS example, you have the joy of carefully making sure that either before every jump the jump itself is safe (freed memory, zeroed out sensitive data, set _all_ possible intermediary variables to a correct value etc) or that the fail function does take care of it....
And yeah. Quite often such goto jump points look exactly like it sounds: A ubiquitous mass of if / else and dubious checks to cater for every possible "state" that could have happened before. -
@IntrusionCM The reason goto is used in that one is because the alternative isn't nice, either: massively indented source code, especially with stacked error handling, or even code repetition. That's why goto is used in the Linux kernel.
For that use case, goto is perfectly fine and appropriate. Just keep it to error handling and don't use it in the actual algorithmic flow, and only jump forward, never backward.
The one thing where I need a backward goto is easy enough to solve with only minimal code restructuring: using a state machine, an enclosing while loop, and achieving the backward jump by going into a previous state. Means, bouncing off of the end of the while loop back to the beginning, then dropping into the switch case for the previous state. -
@IntrusionCM That said, the code is structured badly in this specific case because the goto target is always the same, i.e. not stacked.
Together with the boolean shortcut evaluation in C, that should have been a bunch of checks OR'ed together with exactly one goto in the overall "if true" branch instead of repeating it over and over. -
@IntrusionCM & @Fast-Nop
Thanks. "no returns" is enough.
That actually jog's my memory. When I took C in college my professor thought GOTO was Satan incarnate so rarely saw it in use and since then kinda just forgot why. Now I get it. And as you mention @Fast-Nop it seems like it could work if used sparingly and consciously, but might be a disaster if used by cowboy coders. -
@jiraTicket Yeah, that anti-goto at uni was already dubious because the goto that Dijkstra addressed in his "goto considered harmful" rant was totally different from what "goto" means in C. The main common factor is just the name.
That said, you do have setjmp / longjmp in C which is the C style goto on steroids, and I bet your prof didn't even mention that. :)
TIL the term "Kangaroo Code" was also a popular sort-of synonym for "Spaghetti Code".
It more referred to languages that heavily used "goto"s (because it would be "code with a lot of jumps")
rant