People have prejudices. Even with language commands. Deep down what they say is really bad is the use of goto
, whose myth I already detonate in another question. They say that the break
is a goto
disguised, especially if he possesses a label and may deviate to a specific point in the code.
Some specifically criticise the break
Because it doesn’t let you follow the natural course of the algorithm (Aaah, go?). They consider that it is right to develop a logic that the ending is decided exclusively by the condition of the loop. It is said that it is always possible to rewrite the code so as not to need the break
. And it’s true, that’s the ideal. They even use some example where the code looks good, sometimes even better without using this command. But they don’t usually show the cases that it gets worse. Impair readability or even simplicity to meet a meaningless rule.
It is clear that use where you do not need, where it gets worse, use as an "easy" solution, really is bad, nobody discusses, even because who usually makes these bad constructions (the layman) can not and is not interested in discussing the subject.
In the question code example it seems to make sense. The break occurs by a situation independent of the condition that controls the ending of the loop. Nor would it be absurd to eliminate it, but I think it becomes less readable. It is opinion:
for ($letter = 0; $letter < count($keyArray) && filter_var($keyArray[$letter], FILTER_VALIDATE_INT); $letter++) {
$contNumber++;
}
It certainly gets smaller, but it is more difficult to read and especially to understand why you are making this condition. It is not normal to use a for
like this. I can’t imagine the complication if I have a maintenance and the condition becomes more complex. But I agree that it’s not the end of the world to do so. I won’t fight with anyone I like :)
Maybe someone will wear one while
to become more suitable for a more complex condition. I think it gets worse, at least in this case. Really in good ties while
the break
tends to be less useful, but there is still room for it.
Some people create a variable of flag to "facilitate". Congratulations! It will be much better </sarcasmo>
.
In general it is more useful at the beginning of the loop. When it goes to the middle, it tends to become less useful. If it is at the end, there is a good chance that there is something wrong there. Note that the cited example is good because the break
is at the beginning, middle and end :P
In fact many people would have used one foreach
:
foreach ($keyArray as $letter) {
if (filter_var($letter, FILTER_VALIDATE_INT))
$contNumber++;
else
break;
}
Solve without break
!
Another way some would prefer:
foreach ($keyArray as $letter) {
if (!filter_var($letter, FILTER_VALIDATE_INT))
break;
$contNumber++;
}
I put in the Github for future reference.
Perhaps, once again, it comes from an idea of the past. It was common to have great ties, it was easy to make a strange logic, get lost there in the middle and a break only aggravated the problem. But today every good programmer makes short loops. The bad ones will do everything wrong even, will not be the break
that will cause headaches in your code. Of course, you can’t abuse, you have to do it right.
Today it is rare to see someone criticizing the break
, only some "academics" who only accept a solution as certain. Have study showing that it is more common to make mistakes avoiding these facilitator controls.
Interestingly one of the criticisms is that it is unpredictable. Then you will see and one defends the use of exception, the queen of unpredictability :D
Criticize the continue
also, that I find an even bigger myth. It is obvious that there are cases that improve the if
can eliminate the continue
or the break
, if it is possible to delete without harming the code, I think should do even.
Bad programmer is considered bad :P or better yet "Harmful Programmer is considered Harmful".
Related: One should break into a?
I’m sorry if it was a duplicate, but I did not find the same question.
– Leonardo
It’s not the same question, but I think it’s worth a read here as a comparison, the problem is very similar: Why should I only use a "Return" in each function?. The problem lies in who makes this kind of statement, and not on break. Similar situation here: Why the use of GOTO is considered bad? - It is suggested that, upon hearing such a statement given with such certainty, put who spoke on the list of "unreliable sources".
– Bacco
I think it’s taste, I prefer not to use, I prefer to put all the conditions to stop within the while/for, I think it gets easier to give maintenance and more readable by concentrating the conditions in one place.
– Guilherme Guini
@lvcs I’ll answer. There’s a quote for me to be more specific?
– Maniero
No, no quote, it’s more in a global context anyway. I was contributing a framework when I used a break and I remembered these many people who have told me this kk
– Leonardo
There’s even a show that criticizes the use of break, called Breaking Bad *tudum tss*
– Piovezan
@Piovezan Smoked what? Kkkk
– Leonardo
But Michael Jackson was all for it, "Ba dum tsss tsss tsss" :)
– Maniero
http://answall.com/questions/128845/eval-%C3%A9-young man-or-bandit
– Wallace Maxters
Related http://answall.com/questions/88747/deve-se-usar-break-em-for
– Paulo Gustavo
The "extra" condition that makes the execution flow arrive at the break; it may well be within the "for".
– mau humor
Only that it compromises the legibility of the code and even the understanding of it.
– Leonardo
It should be noted that at the 1998 Standards Conference (PLOP) patterns of ties were presented with broad use of
break
: https://users.cs.duke.edu/~ola/Patterns/plopd/loops.html– Piovezan