Why is the use of "break" bad?

21

In many places I've heard several comments about this, such as:

  • "Professional programmer does not use break"
  • "Code is not good when you see a break"

Why is the use of break so frowned upon by most developers?

Personally I do not see break as a bad instruction, in many cases I use it to make the program a little faster, like in a for :

for ($letter = 0; $letter < count($keyArray); $letter ++)
{
    if(filter_var($keyArray[$letter], FILTER_VALIDATE_INT))
        $contNumber ++;
    else
        break;
}

I have this simple example, in which the goal is to count how many numbers you have at the beginning of a string , for example, I have string "145StackOverflow" , then $contNumber will be equal to 3.

In these cases I use break , because when the current character is no longer an integer I will not have any numbers in string , so I will not have to be running for .

If you've forgotten the question: Why is break so poorly seen by most developers?

    
asked by anonymous 29.05.2016 / 03:14

1 answer

29

People have prejudices. Even with language commands. Basically what they say is bad even is the use of goto , whose myth I already give a detonated another question . They say that break is goto disguised, especially if it has a label and can deviate to a specific point in the code.

Some people specifically criticize break because it does not let them follow the natural course of the algorithm (aaah, go?). They consider that the correct thing is to develop a logic that the term is decided exclusively by the condition of the bond. It is said that it is always possible to rewrite the code so that it does not need break . And it's true, this is the ideal . They even use some example where the code gets good, sometimes even better without the use of this command. But do not usually show cases that gets worse. They impair readability or even simplicity to meet a meaningless rule.

Of course, using where you do not need, where it gets worse, using it as an "easy" solution, is really bad, no one argues, even because whoever makes these bad constructions (the layman) can not and is not interested in discussing the subject.

In the code sample the question seems to make sense. The break occurs for a situation independent of the condition that controls the end of the loop. Nor would it be absurd to eliminate it, but I think it is less readable. It's opinion:

for ($letter = 0; $letter < count($keyArray) && filter_var($keyArray[$letter], FILTER_VALIDATE_INT); $letter++) {
    $contNumber++;
}

It certainly gets smaller, but it is harder to read and especially to understand why you are making this condition. It is not normal to use for like this. I can not imagine the complication if it has a maintenance and the condition becomes more complex. But I agree that it is not the end of the world to do so. I will not fight anyone I like:

Maybe someone preaches to use while to be more appropriate for a more complex condition. I think it gets worse, at least in that case. Really in good ties while break tends to be less useful, but there is still room for it.

Someone creates a flag variable to "facilitate". Congratulations! You will be much better </sarcasmo> .

In general, it is most useful at the beginning of the loop. When you go in the middle, it tends to be less useful. If you are at the end, you have a good chance of having something wrong there. Note that the above example is good because break is at the beginning, middle, and end: P

In fact a lot of people would have used a foreach :

foreach ($keyArray as $letter) {
    if (filter_var($letter, FILTER_VALIDATE_INT))
        $contNumber++;
    else
        break;
}

Solve without break !

Another way that some would prefer:

foreach ($keyArray as $letter) {
    if (!filter_var($letter, FILTER_VALIDATE_INT))
        break;
    $contNumber++;
}

Perhaps, once again, come from an idea of the past. It was common to have great ties, it was easy to make a queer logic, to get lost in the middle, and a break only aggravated the problem. But today every good programmer makes short loops. The bad ones will do it all wrong even, it will not be the% of% that will cause the headaches in your code. Of course, you can not abuse it, you have to do it right.

Today it is rare to see someone criticizing break , only some "academics" who only accept a solution as certain. It has study showing that it is more common to make mistakes by avoiding these facilitator controls . p>

Interestingly one of the criticisms is that it is unpredictable. There you will see and the person defends the use of exception, the queen of unpredictability: D

Criticize break too, which I think is even bigger myth. It is obvious that there are cases that improving continue can eliminate if or continue , if it is possible to delete without harming the code, I think it should do the same.

Bad programmer is considered bad : P or even better "Harmful programmer is considered harmful".

Related: Should I use break in for?

    
29.05.2016 / 04:26