Back in college I’ve been told that using break;
and continue;
outside switch
statements (e.g. to escape for
or while
loops) is wrong, bad practice and bad habits at the same time because it only means that you cannot write loops properly. Is it really?
A friend of mine also told me that using conditional ?:
is bad practice too, however I’ve started using this more often because it allows me to write stuff in less than 70 lines of code while without it i’d end up with something like 200 lines of code. It comes in very useful in almost every project I work on and I use it quite often. Should I stop?
4
On break
and continue
I don’t think there’s anything fundamentally wrong with a break
or continue
. I think the motivation to avoid them is really has to do more with readability than anything else. Consider the following loops (not strictly c/c++ code):
// loop 1
for(int i = 0; i < a.length; i++)
{
if (!found(a[i]))
continue;
result = a[i];
break;
}
// loop 2
for(int i = 0; i < a.length; i++)
{
if (found(a[i]))
{
result = a[i];
break;
}
}
// loop 3
{
bool resultFound = false;
for(int i = 0; !resultFound && i < a.length; i++)
{
if (found(a[i]))
{
result = a[i];
resultFound = true;
}
}
}
All three of these loops run essentially the same code, but are written in slightly different ways. The real question is which one most effective conveys the author’s intent?
-
loop 1 – very bad. The logic here seems to be inverted, and the
continue
is pretty much gratuitous. If translated to English this would read something like:Loop through the list while the item is not found, but if it is set the result then stop.
-
loop 2 – good. This is how such loops are generally written. If translated to English this would read something like:
Loop through the list and if the item is found, set the result then stop.
-
loop 3 – slightly bad. The break / continue logic is found in the condition of the for
loop, which is not as common. It means if someone reading your code wants to know what
will happen next, they have to go back to the top of the loop. Abreak
(as in loop 2)
specifies that logic in exactly one place. If translated to English this would read something like:Loop through the list while the item is not found and if an item is found, set the result.
On the ?:
operator
Again, there’s nothing fundamentally wrong with it. It’s really a matter of personal style and readability.
// Good uses of conditional operator
string result = (someBool) ? "YES" : "NO";
string oddness = (someInt % 2 == 0) ? "even" : "odd";
// Bad use of conditional operator
int result = ConditionA() ? (ConditionB() ? 0 : 1) : (ConditionC() ? 2 : 3);
Again the programmers intent is pretty clear in the first two examples, but is much more obscured in the last.
7
break
and continue
are perfectly fine, there are many programs that will be hard to express without them. The only time using them become a problem is if you want to formally prove correctness. In most real life code, correctness are ensured using unit test, not formal proof. Nevertheless, if a loop can be just as easily expressed without break
and continue
, don’t use them unnecessarily.
Using ?:
is actually a good practice. Good practice for ?:
though, is that all expressions (i.e. conditional part, true part, and false part) should have no side effect except for their return value. If the expression contains side effect, you might want to use conventional if-block statement instead. This is a case of command-query separation, command that have side effect should use if-block while queries that are side-effect-free should use :?
, this is a guideline though, not a hard and fast rule, sometimes complicated queries might be better expressed using if-block.
break
and continue
are invaluable, they often make the code shorter and better readable.
Well, except when they don’t. If you find yourself using them too often or multiple times in one block, it may be that you really want the loop body to be a function (i.e. “method” in the OO terminology). It’s sometimes better to have a function with early returns than a block with multiple breaks and continues.
Regarding ?:
, this is actually the functional cousin of the if statement. Functional programming is hip, so yes, use it. But remember, that functional code should be free of side effects, like others here said.
It is just a tool of programmers. No tool is bad or good by itself, it is on the user how efficiently he/she can use that tool and what he can produce with that.
It becomes necessary when you want to write optimized codes and when there is no sense to keep going further into the loops
Very common example to use break statement is into the Bubble sort algorithm. When make a check that if no swap is done into the loop stop further looping as the array is already sorted out.
Same time it is bad when it is used badly into nested loops and you embed into too much nested loops that you forgot the flow and highly chances are that you can get logical errors into your code.
? : —is a way to write code. Just a short hand, also is a tool and depends where you use it. Can reduce lines of code on some places and on the same time can reduce the code readability.