Why is the use of "break" considered bad?

Asked

Viewed 933 times

24

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

  • "Professional programmer does not use break"
  • "The code sucks when you see a break"

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

Personally I don’t see the breakas a bad instruction, in many cases I use it to make the program a little faster, as 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, where the goal is to count how many numbers there are at the beginning of a string, for example, I have the string "145StackOverflow", then $contNumber shall be equal to 3.

In such cases I use the break, because when the current character is no longer an integer I will no longer have numbers in the string, soon I won’t need to run the for.

Case forgot the question: Why use the break is so frowned upon by most developers?

  • I’m sorry if it was a duplicate, but I did not find the same question.

  • 8

    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".

  • 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.

  • @lvcs I’ll answer. There’s a quote for me to be more specific?

  • 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

  • 3

    There’s even a show that criticizes the use of break, called Breaking Bad *tudum tss*

  • 1

    @Piovezan Smoked what? Kkkk

  • 1

    But Michael Jackson was all for it, "Ba dum tsss tsss tsss" :)

  • http://answall.com/questions/128845/eval-%C3%A9-young man-or-bandit

  • Related http://answall.com/questions/88747/deve-se-usar-break-em-for

  • The "extra" condition that makes the execution flow arrive at the break; it may well be within the "for".

  • Only that it compromises the legibility of the code and even the understanding of it.

  • 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

Show 8 more comments

1 answer

31


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?

  • 4

    What would become of switch without a break :) hehe.

  • I thought the same thing, it would be better to do 10 if’s than a break switch ? It would make the code much worse to read

  • @Lucasqueirozribeiro there is no question of being worse to read, it would do something quite different. So some languages opted for the Switch fallthrough.

  • I mean, do if(a == "1"){}else if(a == "2"){}... just not to use the break

  • @I Ah, got it. There’s a point.

  • To complement the discussion: https://answall.com/questions/88747/deve-usar-break-em-for

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.