News for hackers. Stuff that matters.
Re:The History, Controversy, and Evolution of the Goto State... by int_19h on Monday November 19, 2018 @04:27AM (#18485105)
The funny thing is, it became a cargo cult in many ways. I once worked with a guy who persistently wrote things like these in C++:

   do {
      ...
      if (...) break;
      ...
   } while (false);
It took me a while to realize what this was about. But, basically, he invented a way to goto (so long as it was forward) without actually spelling it out. When I asked him, he gave the usual creed about how "goto is bad", and wouldn't be convinced that break is goto, especially given how he used it.

Needless to say, this kind of code is actually less readable than if it were written with a goto in it, because it looks like a loop, but actually isn't one. With goto, at least it's very clear what it's for, even when there was a better way to do the same thing.

Re:The History, Controversy, and Evolution of the Goto State... by goto11 on Monday November 19, 2018 @07:05AM (#18485809)
To be fair, break is not goto, since it adheres to the block structure of the code, while goto can jump to arbitrary locations.

The code in question should of course be written with an if. A goto would not really be an improvement, both goto and break are really weird way to express something which seems to be simple conditional.

I agree goto's a fine if they are the simplest and most readable solution. But this does not seem to be such a case.

Re:The History, Controversy, and Evolution of the Goto State... by techno_modus on Monday November 19, 2018 @08:42AM (#18486306)
Jumps to arbitrary instruction addresses (goto) are unavoidable and the dispute is about how to structure such jumps by effectively replacing goto by some other mechanism:

- Introduce scopes and program structure instead of explicit labels (instruction addresses) and using control flow operators which rely on this structure

- Procedures and procedure calls

- Overriding and virtual functions where the system knows how to find the next instruction to jump to

- Concurrency and multi-threading where resume operation means essentially jumping to some (dynamic, not static) instruction address.

- Asynchronous and even-driven programming

Re:The History, Controversy, and Evolution of the Goto State... by goto11 on Monday November 19, 2018 @08:58AM (#18486403)
I think we should distinguish between gotos at language level and jumps in the underlying machine code. Djikstra was concerned about correctness at the language level. The whole point is that a structured high level language can provide correctness guarantees which is not present in the underlying machine code, even though all control structures compiles down to jumps.

Gotos are not unavoidable in high level languages.

Re:The History, Controversy, and Evolution of the Goto State... by techno_modus on Monday November 19, 2018 @09:09AM (#18486472)
> I think we should distinguish between gotos at language level and jumps in the underlying machine code.

Is there any non-syntactic difference between these two lines?

    JMP Ox1234
    GOTO label_1234
I think they are identical and the question is only where target labels can reside, how they are represented, and when the jumps are triggered (operation).
Re:The History, Controversy, and Evolution of the Goto State... by goto11 on Monday November 19, 2018 @11:16AM (#18487417)
You lost me here. Is this assembler? The question is if and when to use goto in higer-level code, not how to use jumps in machine code, which is a completely different question. What keyword you use for the instruction does not matter.
Re:The History, Controversy, and Evolution of the Goto State... by superflyguy on Monday November 19, 2018 @06:35AM (#18485666)
I've seen and use that. It's perfectly readable once you understand it's not a loop. It's handy on c, where you can't try...throw (or don't want to in c++) where you want to do a few things which you want to treat in a transaction-like manner. You would return success from the last line in the do loop and handle errors outside the loop. It's not just about mindlessly avoiding goto; it's about writing clean structured code.
Re:The History, Controversy, and Evolution of the Goto State... by BadThink6655321 on Monday November 19, 2018 @08:57AM (#18486396)
The problem is trying to understand that it’s not a loop. The first line of the block says “this is a loop”. The last line of the block says “this isnt’t a loop.”

The code is, if not outright lying, at least misdirecting.

Re:The History, Controversy, and Evolution of the Goto State... by superflyguy on Monday November 19, 2018 @11:59AM (#18487751)
You could always add a comment "this is not a loop" if you find it puzzling that the language allows you to loop zero times.
Re:The History, Controversy, and Evolution of the Goto State... by BadThink6655321 on Monday November 19, 2018 @05:42PM (#18490401)
This is wrong in so many ways. First, I know the language. I’ve been doing this for almost 50 years. This isn’t a question of what the language allows but what the coder is trying to do.

Second, experience has taught me that code and comments often don’t agree. Too, if you have to comment that a loop really isn’t a loop then this is an indication that something is wrong.

Third, it isn’t a loop: while(false) never loops. That’s the point. Having to use a forward reference to understand the beginning of something is bad practice.

Fourth, this construct is almost always a sloppy way to prevent if-else creep. The code is invariably improved by a) not lying about the loop and b) refactoring.

Re:The History, Controversy, and Evolution of the Goto State... by int_19h on Monday November 19, 2018 @08:04AM (#18486090)
In what sense is this any more clean or structured than a goto? It is literally a goto to an anonymous label, nothing else.
Re:The History, Controversy, and Evolution of the Goto State... by superflyguy on Monday November 19, 2018 @12:01PM (#18487771)
It's more clean in the way breaking or continuing from for loops and switch statements are. Or don't you approve of those either?
Re:The History, Controversy, and Evolution of the Goto State... by twtw on Monday November 19, 2018 @12:34PM (#18488040)
No, it's not more clean in the way those are. If you already have a loop, use break or continue. If you don't, it isn't "more clean" to introduce a pseudo-loop just so you can use break.
Re:The History, Controversy, and Evolution of the Goto State... by int_19h on Tuesday November 20, 2018 @02:44PM (#18496991)
I'll grant that it would be handy to have a syntactic feature that would be equivalent to "break", but would apply to the current enclosing block. Basically like "return", but block-scoped - which is basically what this is, except for the whole intent/readability issue. I also like languages that let you do labelled "break" and "continue" out of the loop, rather than forcing you to put the label after the loop - IMO labeling the loop itself is more logical.

But we should be cognizant that those features are still extremely close to a naked goto in terms of the ability to reason about the code - it's just a sprinkling of syntactic sugar on top. So if goto is unequivocally bad, then e.g. early returns are also bad for all the same reasons. Conversely, if those features are fine when used in moderation or in well-established patterns (like "if (error) return") - which I believe is the case - then so is a naked goto. It's the cognitive dissonance of people who use them heavily while insisting that goto is absolutely evil, no exceptions, that irks me.

Re:The History, Controversy, and Evolution of the Goto State... by twtw on Tuesday November 20, 2018 @04:06PM (#18497725)
> - which I believe is the case - then so is a naked goto. It's the cognitive dissonance of people who use them heavily while insisting that goto is absolutely evil, no exceptions, that irks me.

Sounds like we are in agreement then.

Re:The History, Controversy, and Evolution of the Goto State... by twtw on Monday November 19, 2018 @12:32PM (#18488022)
The solution to this is an Error label after returning success and a "goto Error" in case of failure. The pseudo- loop plus break is not cleaner in any sense - its only purpose is to avoid typing "goto."
Re:The History, Controversy, and Evolution of the Goto State... by ensiferum on Monday November 19, 2018 @04:46AM (#18485179)
In c++ however using goto is restricted depending on whether you'd jump over declaration of objects with non-trivial types or not.
Re:The History, Controversy, and Evolution of the Goto State... by int_19h on Monday November 19, 2018 @08:03AM (#18486089)
You can jump past declarations if you're exiting the block - it's only a problem if you're jumping past a declaration to a point where the declared object would still be visible. But the goto equivalent of this code does not require that - it would be a jump to a label after the closing brace.
Re:The History, Controversy, and Evolution of the Goto State... by enriquto on Monday November 19, 2018 @05:42AM (#18485446)
of course it has to be, what would you expect it to do otherwise?
Re:The History, Controversy, and Evolution of the Goto State... by n4r9 on Monday November 19, 2018 @06:40AM (#18485696)
Why not

  ...
  if (...) {
  ...
  }
?

Are there more complex examples where this becomes if-else hell?

Re:The History, Controversy, and Evolution of the Goto State... by shakna on Monday November 19, 2018 @07:17AM (#18485860)

    do {
    // Attempt to set a value here, but maybe not
    // succeed
    // Like asking a user for a specific set of values
    ...
    
    if(choice == sane) break;

    // User did bad. Try again.
    } while(false);
Sometimes, you need a way to repeatedly call the same unique piece of code until it becomes something valid.

However, goto serves it, and does it in a much clearer way, in my opinion:

    try_sane:
    // Attempt to set value
    ...
    if(choice == sane) goto sane;

    // Nope. Try again.
    goto: try_sane

    sane:
        printf("%s\n", "Well done.");
Re:The History, Controversy, and Evolution of the Goto State... by goto11 on Monday November 19, 2018 @07:35AM (#18485933)
I don't understand the example. While(false) means that the loop will not be executed again, which is the opposite of the example with goto.
Re:The History, Controversy, and Evolution of the Goto State... by n4r9 on Monday November 19, 2018 @09:01AM (#18486419)
Right, exactly. I assumed the parent comment was doing a single iteration of the loop. Maybe I interpreted wrongly.
Re:The History, Controversy, and Evolution of the Goto State... by int_19h on Monday November 19, 2018 @09:37AM (#18486648)
A do-loop will always execute the body before checking the condition. So yes, it always executes a single iteration, making it just a block, not really a loop. The whole point of having "do .. while" is so that you can use "break" inside to go to the point immediately after the loop.

Yes, with a single check, you might as well just do "if" instead. The actual code involved numerous such checks. Think of code that repeatedly calls various APIs that return error codes; if any call returns a non-success code, the code must immediately exit this block. You can just keep writing nested ifs, but then you start getting crazy nesting levels after a few calls, which makes code pretty hard to read. So, most people prefer to just jump out instead, resulting in a flat code on the success path, with all errors handled with jumps.

In C or C-like C++ (sans exceptions), this idiom is extremely common, and usually it's done with "if .. goto error" or something similar, often wrapped in a macro.

Re:The History, Controversy, and Evolution of the Goto State... by n4r9 on Monday November 19, 2018 @12:18PM (#18487905)
I think I get it. Writing mostly in C# these days, my instinct when facing nested ifs is to break things down into functions, as you can then return from a function call in order to break out of that code block. Admittedly the problem then becomes naming the functions and potentially having to pass a lot of local state around. I guess this is easier to swallow in a class-based language like C# where you can simply wrap the variables into an object or make them fields of the method's class.