46

I use curly braces with all of my switch case statements in C/Objective-C/C++

I had not, until a few moments ago, considered whether including the break; statement inside the braces was good or bad practice. I suspect that it doesn't matter, but I figure it is still worth asking.

    switch (foo) {
        case 1: {
            // stuff
            break;
        }

        default: {
            break;
        }
    }

vs

    switch (foo) {
        case 1: {
            // stuff
        } break;

        default: {
            // stuff
        } break;
    }
griotspeak
  • 13,022
  • 13
  • 43
  • 54

5 Answers5

40

Short answer: it doesn't matter.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • I suppose I can just accept this (in a minute) but is there anything interesting in the long answer? – griotspeak Sep 10 '11 at 20:42
  • 1
    @gritospeak: I'll leave that for you to decide! (IMO, the long answer is just expressing a personal preference.) – Oliver Charlesworth Sep 10 '11 at 20:50
  • 3
    My preference is to put the break inside, because I think of the curly braces as defining the contents of the case statement, and the break is part of those contents. I don't like the curly braces to be a subset of the case statement's contents. – Jayen Jun 29 '12 at 23:03
15

Just a give a slightly more detailed answer...

The official C99 specification says the following about the break statement:

A break statement terminates execution of the smallest enclosing switch or iteration statement.

So it really doesn't matter. As for me, I put the break inside the curly braces. Since you can also have breaks in other places inside your curly braces, it's more logical to also have the ending break inside the braces. Kind of like the return statement.

gregschlom
  • 4,825
  • 4
  • 28
  • 23
6

There's tons of different coding styles of how to combine curly braces and switches. I'll use the one I prefer in the examples. The break statement breaks out of the innermost loop or switch statement, regardless of location. You could for example have multiple breaks for a single case:

switch (foo) {
case 1:
    {
        if (bar)
            break;
        bar = 1;
        ...
    }
    break;
}

Note that you can also put the cases anywhere, though that is somewhat considered bad practice. The case label is very much like a goto label. It has happened that I've written something like this:

switch (foo) {
case 1:
    bar = 1;
    if (0) {
case 2:
        bar = 2;
    }
    ...
    break;
}

but use it with care.

Per Johansson
  • 6,697
  • 27
  • 34
1

You probably don't want the curlies in the first place unless you need them for lexical scope. The first example looks better to me, but I suppose the real answer is that it's a matter of taste.

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
  • 1
    I use curly braces to provide scope 'just in case' because of this issue: http://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement – griotspeak Sep 10 '11 at 20:43
0

As clearly stated, this is just a matter of personal style, but I always put the break statement outside the braces: putting the break before the closing brace seems to me to jump out a compound statement thus slightly increasing the spaghetti code feeling.

Zac
  • 4,510
  • 3
  • 36
  • 44