My boss keeps mentioning nonchalantly that bad programmers use break
and continue
in loops.
I use them all the time because they make sense; let me show you the inspiration:
function verify(object) {
if (object->value < 0) return false;
if (object->value > object->max_value) return false;
if (object->name == "") return false;
...
}
The point here is that first the function checks that the conditions are correct, then executes the actual functionality. IMO same applies with loops:
while (primary_condition) {
if (loop_count > 1000) break;
if (time_exect > 3600) break;
if (this->data == "undefined") continue;
if (this->skip == true) continue;
...
}
I think this makes it easier to read & debug; but I also don't see a downside.
goto
) are useful in some cases. – sakisk Mar 26 '11 at 09:39break
may/maynot be bad practice compared withreturn
. Thosereturn
statements are alternative exit-points for the loop too - and also alternative exit-points for the whole function. There is a time and a place, of course, but the thing to watch out for is having non-obvious exit points (a weaker but pragmatic version of the single-exit-point rule). Personally, I'd be nervous about mixingcontinue
in with a mess ofbreak
exit points, though - the fact that one or two of the exit-points behave differently isn't necessarily obvious at a glance. – Oct 06 '11 at 08:10continue
being a sensible keyword forcontinue after the end of the loop
until I tried to write a comment calling you an idiot. Damn! That said, I don't see howbreak
can be misinterpreted asgo back to the top of the loop
. – Oct 06 '11 at 08:20continue
(there's no such thing in Pascal) - until I found it in someone elses code that I was maintaining. I remember it because the confusion was momentary, and because it was an "I'm such an idiot - why haven't I been doing this myself?" moment. – Oct 07 '11 at 06:00if(condition){}
is 100% the same as havingif(!condition) continue;
at the beginning. You're still running the code "where you just evaluated the proper conditions". Forgetting an edge case will happen just the same way for both ways. However, the latter saves you up to 2 lines and 1 indent. – R. Schmitz Oct 20 '17 at 13:48if
orelse
statement gives me a visual reminder that this code is executed conditionally, while in your code you have to keep that fact in mind, but if you don't and you forget, then you're entering the Danger Zone. – klaar Oct 21 '17 at 14:39continue
is the worst of all; it acts like a miniaturegoto
within the loop. – Martin Nov 02 '22 at 16:59value < 0
is an error condition, which shouldn't happen unless someone messed up at some point. Ifvalue < 0
is an acceptable case which you just want to ignore, using exceptions is wrong. In that case, I'd just restructure the function to perform all the checks in a singleif
(or better yet, by calling a function). – Martin Nov 10 '22 at 13:36success = (value >= 0 && value <= max_value && ...); if (success) { success = ... }
, with a singlereturn success
at the end, outside theif
. Sorry for the bad indentation, looks like this comment box doesn't allow for blank lines, etc. – Martin Nov 10 '22 at 13:39goto
,longjmp
, and other such constructs are. I don't see how thinking in negatives all the time is clearer than following a simple if-else logic, but I guess it's the same as with conventional current: sometimes the wrong way of thinking is so ingrained in the industry than trying to fix it is a hopeless endeavor. – Martin Nov 18 '22 at 09:41break
andcontinue
are perfectly legitimate and concise way of exiting from a loop. Makes the code clearer than compoundingif's
. – user7195 Jan 02 '23 at 22:26