I do not think that GoTo is bad. It can lead to hellish code but if you don't misuse it - it can be extremely useful.

  • 1
    Interesting, do you have an example of not misusing it?
  • 1
    @kvsm if you takve care of your memory yourself in C i.e. Microsoft coding standards require goto cleanup whenever you allocate memory
  • 2
    @kvsm I second @slinavipuz. Think of a function that mallocs memory several times. If the second malloc fails you should free the first but not the others.
    If this third fails you should free the first two but not the third.

    That's how it's done in the Linux kernel.

    a = alloc
    if a failed goto a_fail

    b = alloc
    if b failed goto b_fail

    c = alloc
    if c failed goto c_fail


    free c
    free b
    free a
  • 0
    @Gauthier it is kinda natural - unconditional jump in ASM. There is a valid use case. But we've all been told scary stories 'bout it.
  • 0
    @slinavipuz Which is a good thing. You have to scare students with gotos, because it's always the last solution. Coding the above if if/else would be an abomination if you have more than 2 allocs.
    Maybe there should be a license to goto. Only allowed with 5 years pro experience, or something...
  • 1
    @Gauthier well - I agree. Don't use it unless you're fucking absolute, 100%, sure as hell, p=1, without any doubt, sure that you know what you are fucking doing.

    But goto is not evil. People are. Stupid and evil.
Add Comment