views:

434

answers:

9

Possible Duplicate:
Break statements In the real world

Some days ago I started a quick open source project and, when some mates looked at the code on svn, one of them told me that using break statement inside a for loop is considered harmful and shouldn't be done.

He added, though, that I would find several cases of break statements inside for loops on Linux kernel source code, but that was just because only Linus Torvalds and Chuck Norris were allowed to use it and no one else.

What do you think? I see no problem in using break inside a for loop. In my opinion, emulating the behaviour of break using boolean variables or something alike adds a lot of innecesary overhead and makes the code less straightforward.

Also, there's no room for comparison with goto, because break cannot arbitrarily change program's flow from one point to the other lie goto does.

+13  A: 

I see no problem with using breaks. There will always be circumstances where you want to stop processing a loop, and using a break; makes much more sense (and makes it more readable!) than setting your loop counter up to a value that would make your loop stop at the next iteration.

fire.eagle
I'll have to admit, I'm kind of disappointed that this got upvoted, simply because this brought me off my nice, round number of 256. Ah well, time to go for 512. :P
fire.eagle
I know the feeling... I was so happy when I got SCHAR_MAX friends on facebook -_-
Cogwheel - Matthew Orlando
I see no proble with using goto. There will always be circumstances where you want to stop processing a very nested loop, and using goto; makes more sense (and makes it more readable!) than having boolean values to tell you when to get out of the loops.
Carlos Muñoz
+9  A: 

Obligatory:

XKCD Goto

The point is that you should not avoid it purely on grounds of bad practice (or velociraptors), but consider on a case by case basis.

It's all about clarity. As you said, you never have to use it, but in some cases it promotes readability. It's useful when the loop usually terminates normally, but in rare cases you have to bail out. Loops that usually (or always) break are more of a code smell (but could still be appropriate).

Matthew Flaschen
+1 for providing a good answer with this comic. I recall seeing one heavily upvoted answer that was this comic and nothing else, ugh.
Maulrus
+1  A: 

I think it depends on context. While it may be 'bad' coding style in some situations, I can't think of a case where it would be harmful.

In fact, in some cases I would recommend it. For example, if you were using a linear search, any case (except the very worst case), a break would improve your speed. I think breaking out of the loop when you've found your needle would be perfectly acceptable, and it would be more readable than messing around with the loop variable (which might not solely be used for the loop, depending on your program) or wrapping the loop body in an if block. (And the other option, an if which contains continue, combines the worst of two worlds: wrapping loop logic in an if block, and the poor coding style railed against by people like your friends who don't like break.)

Brian S
You should not recommend break on performance grounds. Compilers can usually optimise status variables.
Artelius
Fair point. The point I was trying to make was if, for example, I'm performing a linear search on a large number of elements and I find what I'm looking for at index 2, I would much prefer to stop looking than continue looping over the remaining elements which I know I don't want.On the other hand, if I know I'll have a large number of elements, I probably wouldn't go for linear search, anyway.
Brian S
+10  A: 

Not only is there no problem to using break, I'd say anyone saying it is "considered harmful" is downright wrong.

break is a language feature used to abort a loop - you could use goto, but then you incur the (appropriate) wrath of the XKCD comic below. You could use a flag in the condition, but that hampers readability. break is not only the easiest, but also the clearest way many times to bust out of a loop. Use it as it was meant to be used.


Edit: To get at a larger picture here: When you're writing code, the guiding principle to "should I use language feature X or Y" should be "which way will result in the more elegant code"? Elegance, in code, is pretty much an art, but I'd lay it down as a fine balance between readability and algorithmic (read: not micro-optimizations) efficiency. Readability is going to be determined by length, complexity of the code, etc. A one-line boost::bind may very well be harder to read & understand than a 3 line loop.

If a language feature can help you write code that is easier to understand while getting the job done, then use it. This applies to break, goto, C++ exceptions, etc. Don't follow a "X is (evil|considered harmful)" blindly - apply common sense and logic each time.

Thanatos
+2  A: 

There is a paradigm that any loop should only have one point of exit (same as a function should only have one return). This has to do with readability. Too many exit points can make the code very difficult to understand. Also, it is important if you want to do code verification (i.e. mathematically proof if your code is correct).

However, guidelines are often there to help, but are not strict. There are possibly situations where a break is better than not using it. Hence, one should be pragmatic about this, but understand the reason for such principles in order to create good code.

txwikinger
+1  A: 

Incrementing your loop counter instead of using break also means you finish executing the current iteration of the loop, which may or may not be desirable.
Of course, you could wrap the rest of it in an if clause, and then do it again when you realize you need to check whether or not to stop looping multiple times, and you will quickly realize why people use break;

Larry Wang
+9  A: 

Not only is there no problem with break, it's also OK to use goto when break is insufficient. Don't be afraid of multiple returns, either.

All of the above only applies if it makes the code easier to understand*.

*And if your PHB allows it...

Cogwheel - Matthew Orlando
A: 

I suggest this algorithm if you're considering using a given technique.

  • If it is considered good practice:
    • use it.
  • If it is not considered good practice:
    • use it only if you judge it to be the best long-term solution.
  • If it is considered evil:
    • use it only if you judge it to be the best long-term solution and you are extremely confident in your judgement. Beware: things which are considered evil tend to be deceptively appealing.

I would classify break; as "not good practice". Use it when it makes the code more readable, reduces the chance of bugs, does not complicate debugging, etc.

Artelius
I use break all the time...
Andreas Rejbrand
+3  A: 

I think your mate is insane. break is perfectly acceptable, perfectly readable, and perfectly maintainable. Period.

Scott Stafford