-1

Apropos of What kind of bugs do "goto" statements lead to? Are there any historically significant examples? I am not that learned in C, and to me the puzzle is that a single exit of a function is mandatory, but a goto is considered bad. Often one has to exit a function on a error condition, and then use a goto to the exit point. Also, I am fond of using protothreads. Is that not a form of goto?

  • 2
    Doesn't the highest voted answer's approaches answer your question? Also remember that it is legitimate to extract chunks of code into functions, even if those functions would have only one caller, because making the control flow more obvious and verifiable is a legitimate concern on its own. – rwong Oct 28 '16 at 4:18
  • Yes,rwong, it does answer most of the questions, and I am happy to use a goto the return statement to exit early. Then the question of entering a function at different points comes up, and protothreads does serve a very useful purpose, entering a function at different points, and exiting at different points. I don't see anybody arguing against it, so I accept that that is also not wrong. Thank you – user3824211 Oct 28 '16 at 5:16
  • 2
    Possible duplicate of Where did the notion of "one return only" come from? – gnat Oct 28 '16 at 5:36
  • goto cleanup is fine in C because it lacks other cleanup mechanisms (try...finally, RAII, etc.). – CodesInChaos Oct 28 '16 at 10:27
2

Languages are full of gotos. Only thing is, they've been abstracted away to make the use of them safer. A function with a return value is an abstracted goto, a while loop is an abstracted goto, every conditional statement is an abstracted goto.

Just take a look at the CPU stack and you will see lots of jmp instructions, which actually are gotos.

So in short: they're still there but abstracted to make them safer to use.

  • Dankie Pieter And nobody comments on the use of protothreads that goto different entry points? – user3824211 Oct 28 '16 at 14:05
0

If we take a look at the protothread we see that the macro is not creating a goto but using a switch-case statement. So protothreads are not goto.

The protothread does create multiple exit points. A single exit point of a function is not mandatory but it's a good rule of thumb that when the function requires multiple exit points or many indentations you can assume that the function you've written is too large. You have to consider both because you can avoid indentation by using many exit points or you can have one exit points by writing complex if-statements, loops and using gotos. Neither of those are good.

The reason why goto is bad is not because of exit points. If we take a look at the while or for loop we see that all the jumpers we're talking about is taking out of the programmer's hands. If wanted or not the programmer has no control over these hidden jumpers, it is placed and dynamically indexed for the programmer during compilation. If we take a look at an if-statement, which is a jump ahead instruction, we'll see the same thing. Nowhere in the code is any goto defined by the programmer but created by the language. This is the biggest reason why goto are considered harmful. Gotos are breaking the design of the programming language with the possibility breaking the program while knowing that the programming language is designed to take care of it flawlessly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.