tags:

views:

424

answers:

9
+5  Q: 

Is 'break' evil?

I have heard several times in my career that the break operator is evil because it may cause internal exceptions (at least in older languages) or something else. I never thought so. But I would like to ask you if I am mistaken. And if I am, why? It happens in all languages?

Duplicate: Break Statements in the Real World

+13  A: 

"Break" is not evil. It will not cause exceptions.

Some people say "break" is too simliar to "goto," another banned substance. However, when you go out of you way to avoid "break" you often add more complexity, more chance for an error.

Use break, then also use unit tests to make sure your code works like you expect.

Jason Cohen
+2  A: 

I'm pretty sure break; is totally safe.

zodeus
+9  A: 

I have a hard time using case statements without them. So, no, not evil.

Rob Lachlan
This implies that case statements themselves are not evil. A dubious assumption, in my opinion.
Wedge
agreed wedge, that would be another debate
eglasius
Oh, snap. Hadn't considered that. I must go and get my exorcism kit...
Rob Lachlan
All conditionals are evil. Damn useful though...
Shog9
A: 

it exists for a reason. so use it when you have to - much like goto. although i can't imagine how break can be misused.

+1  A: 

Nop, break isn't evil and has some clear scenarios. Say you need to find something in a list, so you do a for/foreach on it, why would you want to keep searching after you already found it?

SomeClass matchingItem = null;
for(int i = 0; i < count; i++)
{
   SomeClass item= someList[i];
   if( item.SomeCheck(i))
   {
      matchingItem = item;
      break;
   } 
}

Ps we can use some methods that do that, but you expect their implementation to do that ;)

eglasius
+1  A: 

Break is not evil, neither is its close-cousin, "continue";

Both are extremely helpful in controlling loop behavior properly, and both connect very closely to what the actual assembly instructions are. So if you try to ignore them, you end up ignoring how your code actually compiles as well.

abelenky
+2  A: 

There's quite a few programmers who think that the mandatory break in switch/case is 'evil' since when missed could cause unforeseen fall through's of the code:

switch(var)
{
     case "something": 
           dosomething();
     case "somethingelse":
           dosomethingelse();
}

In the above case this may or may not do exactly what you want without the break's in place.

I for one don't share this opinion and quite often depend on this fall through behaviour of a switch/select statement.

Martijn Laarman
well, you are banned from doing that in c# :P
eglasius
A: 

It does not cause exceptions, but depending on where you use it, it can worsen the level of overview you have. For example in a switch-case-block you must use them, but in a loop it will move the condition for termination to the code-block, instead of having it in the loop-header.

Patrick Daryll Glandien
A: 

I think the helpful thing to do with break, continue and return is have your editor colour them in a different colour to everything else That way you can more easily see early returns and thelike.

Zarkonnen