views:

612

answers:

13

My OCD makes me add "break" when writing case statements, even if they will not be executed. Consider the following code example:

switch(option) {
    case 1:
        a = 1;
        b = 7;
        break;
    case 2:
        a = 2;
        b = 4;
        return (-1);
        break;
    default:
        a = -1;
        break;
}

My two questions are:
For "case 2:", I don't really need the break, but is it a good idea to have it there anyway? For "default:". Is it purely OCD, or is there any real reason to have the break here?

A: 

I don't personally put the breaks in, but it could help when someone else decides to move the return (-1) to outside of the switch and forgets to add break.

Jimmy
+11  A: 

You don't need either break, but there's no harm in having them. In my opinion, keeping your code structured is worth having a couple of extraneous statements.

James Thompson
A good compiler will not generate any code following a return or a asm-goto. So no harm done...
0x6adb015
No harm done, but they shouldn't mess with a programmer's coding style in that way.
toto
Uh, the compiler doesn't automatically rewrite the author's source to remove the break or anything.
dss539
+4  A: 

The break after your default case is just a matter of personal preference.

Putting a break after return almost seems contradictory to me. I'd remove the break, just to make the return statement really stand out.

samoz
A: 

Neither break does anything for you, but neither does harm.

Personally, I usually leave them out if I have a return - but I also try to avoid having multiple return points in a function if possible.

However, I do think the break in the default: case is good - for one reason: If you were to leave it out, and somebody added a new case after default:, the behavior would be different if they "forget" to add in a break.

Reed Copsey
+11  A: 

I agree with having a break in a final default case, and don't agree with breaks after returns. (A colleague does those and it hurts my eyes.)

I also indent switches so as to reduce proliferation of indent levels. :) i.e.:

switch(option) {
case 1:
    a = 1;
    b = 7;
    break;
case 2:
    a = 2;
    b = 4;
    return -1;
default:
    a = -1;
    break;
}

(I also think that, since the return statement is not a function, it isn't appropriate to enforce a superfluous style that makes it look like one.)

chaos
I've seen the indenting as you have above recently and been on the fence for a while about your example vs. mine. I think I'm going to switch (no pun intended) to your way going forward. Thanks!
Alan H
Yea, good style there. I like my brackets on a single line tho.
toto
If you're nesting so many switches and ifs that the indentation becomes a problem, you're probably better off creating some new functions.
dss539
Probably. But seriously, is using two indent levels for a switch actually conveying any information? It really isn't. The switch is one control level, not two, and the cases are easily read as alternations of it.
chaos
Your indent level is clearly better, and anyone who disagrees is likely an Emacs user or something.
James Thompson
A: 

I would put the break in to show that you do not intend to fall through to next case.

Maggie
+1  A: 

I would consider the break after return to be bad form, you will get warnings about unreachable code on some compilers.

The break on your default case is completely appropriate, case fall through is a tool and should be especially marked when used.

Don Neufeld
A: 

Please excuse my limited knowledge, but what's OCD?
Apart from that, Brian Kernighan provides a good explanation on when you should (not) use break within a switch statement.

pugmarx
Obsessive compulsive disorder. As in it mentally bothers him to not see a break; after a return;
Will Eddins
Okay I kind of feel dumb now. Shall say no more.
pugmarx
A: 

As others have pointed out, placing a break after a return or in the default case is mostly a matter of personal style.

When I don't have to follow any specific style rules, I prefer something like this:

    switch(foo){
         case 0:
             baz = 1;
             break;
         case 1:
             bar %= 2;
             return -1;
             /* NOTREACHED */
         case 2:
             bar = -1;
             return -2;
             /* NOTREACHED */
             break;
         default:
             break;
    }

Between cases 1 and 2, I tend to prefer 2. Even though the comment says NOTREACHED, comments can lie ( unintentionally of course ) when the code changes. I like the NOTREACHED comment since it can satisfy lint that you know what you are doing and serves as a reminder that you exiting the function early. The reasoning that placing a break after the return will mitigate errors if the return is deleted seem flawed to me. You are still going to get bogus behavior regardless if you fall through to the next case or you exit the switch and continue on as before.

Of course, if I can avoid it I would not return from a function within the body of a switch.

Erik
A: 

I prefer always have a break in each case including the default and avoid doing return at all inside switch's. For short switches with just 2-3 cases(including default) return is ok but only if all cases do it the same way. 'pointless' break i see as pointless and only make's it more code to read. Same goes for empty defaults that just do break, totally pointless. The ease to read the code is in my opinion more important that what happens if someone happens to change this or that.

Joakim Elofsson
A: 

In this exact example, for both questions, it is personal preference. In general, the rule is this: anything without a break will fall through. That means (as Pod said) its a good idea to put breaks in default cases in case they are not last. This also means if your case contains a return, then a following break is indeed not necessary.

Toddeman
A: 

I'm told that in C, C++, java and C#, if you don't put those "breaks" the program code flow will fall into the other "cases" and will execute the instructions inside them, not matter if the variable doesn't have the values assignned to the "cases".

yelinna
Actually, in C#, you don't use breaks in a switch statement. If you want to fall through, you actually have to use a goto statement to go to the case you want to fall through to (e.g., goto case 2;). I was quite surprised when I learned this, given how similar the syntax is between C# and C/C++. I guess it makes sense, though, because it forces you to do something explicit in order to implement fall through. (And in fact, it makes "fall through" a lot more flexible, albeit with all of the caveats that go along with using goto's in other situations.)
RobH
A: 

Regarding the comment that others have made that they leave the break in the default case in case someone comes by later and adds a case after it: Where I work, the coding standard says to always put the default as the last case; so in our situation, a break on that case is just redundant. (This is one case where I agree wholeheartedly with the company's coding standard, because with the default case always being the last one, you always know where to find it, even in a long switch-statement.)

As for breaks after returns, I tend to omit the break unless there are any execution paths that do not return, as I find it redundant. (My exception to this is, on the rare occasions when there are several execution paths in a case and I can't tell with a quick scan of the code whether or not they all return, then I'll leave the break in just to be safe.)

RobH