Live data from Hacker News

Goto (2007)

beej.us

41–50 of 207 posts

Re: Goto (2007)

#41
I think the issue is only relevant when considering the years when programming languages began to have more nuanced instructions. For Assembly, goto is an essential construct and you'll learn how to use it reliably as jump conditions. For C, it's a great way to get out of nested code that you couldn't avoid for the given problem. For more high level languages, continue and break are the 'goto' statements and they're used all over but they have built in restrictions which make them easy to use. Goto is just hard to master in use for some of us and it's okay to restrict ourselves to limited uses of it. I just don't know why it's become a mantra to say, "do not ever use goto or labels or jump conditions what so ever." It just seems odd to me.

Re: Goto (2007)

#42

At StackOverflow you're downvoted heavily when you just typ got (don't event have time for the last "o"). Then Apple comes with the glorious "goto fail" and shows that real companies use goto, and the problem wasn't "goto" itself.

It seemed odd to me that their fail context didn't set 'err', but assumed it would be set by the origin of the jump. At least, I would have initialized 'err' to a failure value, so that any mistake that led it not to be set would result in a valid error state.

Re: Goto (2007)

#43
post #4

Coming from assembly language, I always found the anti-goto sentiment rather cute. A beautiful restriction, but ultimately arbitrary. Like writing poetry. Or those novels that do not ever use the letter "e". Why would an otherwise sane person write code with "rep" and without ever using "jmp"?

Even in assembly there's position-dependent and position-independent code.

Re: Goto (2007)

#45
post #38

Earlier quoted context omitted.

goto is the idiomatic way to emulate RAII in C. It's everywhere.

Please explain a little more with an example. Certainly not in my C code, though I a huge fan of RAII in C++.

Here is an example with RAII and goto: https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consi...

Btw that is a Cert C recommendation to use goto for RAII!

Take a look at the Linux kernel sometime, goto is everywhere there too https://www.kernel.org/doc/html/v4.10/process/coding-style.h...

Re: Goto (2007)

#46
post #32

My favorite use of goto is with C#'s "yield return". The compiler converts a function with a yield command into a label. To resume a function from where it left off, the compiler adds code to test an added resumption-point variable and to goto the label for each resumption point.

All code that ends up being written as assembly will become a command (an opcode or a set of opcodes) and a label (the opcode's address).

Re: Goto (2007)

#47
post #38

Earlier quoted context omitted.

goto is the idiomatic way to emulate RAII in C. It's everywhere.

Please explain a little more with an example. Certainly not in my C code, though I a huge fan of RAII in C++.

A function that allocates resources in several steps jumps, on error, to the teardown part at the end to deallocate (in reverse order of allocation) only the resources that were allocated. The others are skipped (jumped over). It's very popular in the Linux kernel.

Re: Goto (2007)

#48

I think the issue is only relevant when considering the years when programming languages began to have more nuanced instructions. For Assembly, goto is an essential construct and you'll learn how to use it reliably as jump conditions. For C, it's a great way to get out of nested code that you couldn't avoid for the given problem. For more high level languages, continue and break are the 'goto' statements and they're…

For more high level languages, continue and break are the 'goto' statements...

I have never liked continue or break. It always felt like I messed something up if I found myself needing them.

Re: Goto (2007)

#49
I like the presentation of the three nexted loops.

Maybe "break;" should become a shortform for a generalized "break(1);" (= break one level) if we want to avoid using goto.

Perhaps this can be implemented by combining a macro with setjmmp() and longjmp() from the standard C library, without requiring a compiler change.

Re: Goto (2007)

#50
post #4

Coming from assembly language, I always found the anti-goto sentiment rather cute. A beautiful restriction, but ultimately arbitrary. Like writing poetry. Or those novels that do not ever use the letter "e". Why would an otherwise sane person write code with "rep" and without ever using "jmp"?

Even in assembly there's position-dependent and position-independent code.

And jumps relative to cs or ip, which serve both. PIC idea is not related to goto.
Post reply on HN