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.
- 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
Gotos are not unavoidable in high level languages.
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).The code is, if not outright lying, at least misdirecting.
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.
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.
Sounds like we are in agreement then.
...
if (...) {
...
}
?Are there more complex examples where this becomes if-else hell?
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.");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.
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.