5 ms·
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 (
by int_19h 8y ago
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.
- ensiferum 8y agoIn c++ however using goto is restricted depending on whether you'd jump over declaration of objects with non-trivial types or not.
- enriquto 8y agoof course it has to be, what would you expect it to do otherwise?
- int_19h 8y agoYou 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.
- superflyguy 8y agoI'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.
- int_19h 8y agoIn what sense is this any more clean or structured than a goto? It is literally a goto to an anonymous label, nothing else.
- superflyguy 8y agoIt's more clean in the way breaking or continuing from for loops and switch statements are. Or don't you approve of those either?
- twtw 8y agoNo, 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.
- int_19h 8y agoI'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.
- twtw 8y ago> - 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.
- n4r9 8y agoWhy not ... if (...) { ... } ? Are there more complex examples where this becomes if-else hell?
- shakna 8y agodo { // 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.");
- goto11 8y agoI 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.
- n4r9 8y agoRight, exactly. I assumed the parent comment was doing a single iteration of the loop. Maybe I interpreted wrongly.
- int_19h 8y agoA 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.
- goto11 8y agoTo 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.
- techno_modus 8y agoJumps 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
- goto11 8y agoI 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.
- techno_modus 8y ago> 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).