by augustk · 9mo ago · 0 comments
for (i = 1; i <= 100; i++) {
S;
if (P) {
break;
}
}
are just as bad since `break' (and `continue' and early `return') are a just gotos in disguise.Gotos get a bad rep because they become spaghetti when misused. But there are lots of cases where using gotos (or break/continue/early return/catch) makes your code cleaner and simpler.
Part of a programmer's job is to reason about code. By creating black and white rules like "avoid gotos", we attempt to outsource the thinking required of us out to some religious statement. We shouldn't do that.
Gotos can be useful and can lead to good code. They can also be dangerous and lead to bad code. But no "rule of thumb" or "programming principle" will save you from bad code.
(FWIW, this is essentially the argument that Knuth makes in his defense of goto paper)