2

My questions is basically all in the title. I have no real application in mind, I'm just trying to make sure I use generally accepted good coding practices, and I remember my CS professor saying that breaks are generally avoided when possible, but he never told us why.

I would ask him myself, but he's out of his office this summer.

Joshua
  • 4,270
  • 10
  • 42
  • 62

6 Answers6

4

This is a theological issue. People who are opposed to the use of break (or continue for that matter) say that it makes the control flow of the program harder to follow and thus makes the program less readable. But an easier way to make readable code is simply to make shorter functions, use meaningful variable names, comment appropriately, etc.

Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
2

Sometimes professors are wrong.

He may object to skipping out of a loop right in the middle of it. This is akin to using a return statement in the middle of a function...some consider that blasphemy.

What ever is the simplest, and produces the most easily maintainable code, is what should be used.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
1

Generally speaking, you will use 'break' to get out of cycles when a certain condition is met (for instance, when you've found what you're looking for, etc.); I don't consider it 'evil' such as the infamous GOTO statement, but reasonable people will differ.

Tomas Kohl
  • 1,388
  • 1
  • 19
  • 27
1

What Dijkstra said was harmful about goto statements - http://drdobbs.com/blogs/cpp/228700940

and https://softwareengineering.stackexchange.com/questions/58237/are-break-and-continue-bad-programming-practices

Community
  • 1
  • 1
darlinton
  • 2,131
  • 16
  • 21
  • 1
    Playing devil's advocate: The opponents of `break` argue it's a thinly-disgused `goto`. –  Jul 19 '11 at 14:09
  • This is exactly what @darlington is saying. That some believe break is harmful for the same reason that goto is harmful. – Scott C Wilson Jul 19 '11 at 18:13
1

Your professor was saying "breaks should be avoided" because they are similar to the now-taboo goto statement. However to say "breaks should be avoided whenever possible" is just wrong.

Break exists for readability. You can do anything without break that you can with it, but that doesn't mean you should. Sometimes it just makes sense to make a statement like "In this condition, break out of this loop." or "In this condition, return from this function."

It's a bad idea to use break for program flow in a nested for loops in a way that might give a casual reader some confusion.

Simple rule of thumb: Does using break here (instead of additional if/thens, another boolean, etc.) make this easier to understand or harder to understand? It's that simple.

Jeremy
  • 5,365
  • 14
  • 51
  • 80
0

Ask your professor to try writing a switch statement in any C-style language without break. It can be done I'm sure, but the resulting code is not going to be any more readable.

switch (someExpression) {
    case enumVar.CaseA:
        // do things here, but don't you dare break!
    case enumVar.CaseB:
        // do other things - possibly unrelated or contradictory to CaseA

    // ... and so on
}
Yuck
  • 49,664
  • 13
  • 105
  • 135