Cereal
Cereal

Reputation: 3849

Switch statement formatting

I've been conflicted over how to format switch statements for awhile now. I see three viable options, and while I've been using the first more often than not (As it's the form I see most often), I find the second and third to be more intuitive.

First:

switch(x) {
    case 1:
        DoSomething();
        break;
    case 2:
        DoSomething();
        break;
}

Second:

switch(x) {
    case 1: DoSomething();
        break;
    case 2: DoSomething();
        break;
}

Third:

switch(x) {
    case 1: DoSomething(); break;
    case 2: DoSomething(); break;
}

I understand a lot of code style is preferential, so I'll set my official question as:

Is there anything fundamentally wrong with using the second or third options, so long as it's consistent throughout the code?

Upvotes: 6

Views: 17348

Answers (6)

Reed Copsey
Reed Copsey

Reputation: 564701

Is there anything fundamentally wrong with using the second or third options, so long as it's consistent throughout the code?

No - provided your language allows this format, there's nothing "fundamentally" wrong with it. As with all code formatting, it's purely a personal or team preference.

There are good reasons for the first format, such as:

  • A visual break between the case and the statement start.
  • Seeing the break; on its own line to differentiate between fall through cases, etc.
  • Consistency when there are multiple statements vs. a single statement.

That being said, there is nothing wrong with any of the three options.

Upvotes: 4

JNL
JNL

Reputation: 4713

As per Oracle Docs;

A switch statement should have the following form:

switch (condition) {
case ABC:
    statements;
    /* falls through */
case DEF:
    statements;
    break;
case XYZ:
    statements;
    break;
default:
    statements;
    break;
}

The important thing here is to be consistent when you follow a format.

Upvotes: 7

AlainD
AlainD

Reputation: 6615

Several important references gives examples with indented case statements:

This matches your 1st and 2nd styles, as opposed to the 3rd style which has case statements at the same indentation as switch.

One argument for indenting case statements is where variables are declared with local scope, which require braces. Here's an example adapting your 1st style:

switch (x) {
    case 1:
        DoSomething(0);
        break;
    case 2:
    {
        int superNum = GetSuperNumber();
        DoSomething(superNum);
        break;
    }
}

Without indentation, the closing brace of the final case or default statement has the same indentation and "falls into" the closing brace of the parent switch as follows:

switch (x) {
case 1:
    DoSomething(0);
    break;
case 2:
{
    int superNum = GetSuperNumber();
    DoSomething(superNum);
    break;
}
}

This disguises the hierarchy and, in my opinion, is harder to read. Unless you're forced for reasons of consistency into using the existing code style or the style is mandated for you, the 1st style is a good choice (and my favourite).

Upvotes: 5

brandito
brandito

Reputation: 706

Personally I use this in languages/text editors/IDEs that don't auto format it to the first option you provided.

switch(x) {
    case 1:
        DoSomething();
    break;
    //Line separator here depends on how big the line-heights are with the IDE I'm working with
    case 2:
        DoSomething();
    break;
}

Like most have said, it's purely choice, but consistency is key.

Upvotes: 2

MasterPlanMan
MasterPlanMan

Reputation: 1032

So long as you follow standards of the code base you're working in, it's fine. If you want to define a better way to do things, it all depends on your own preferences.. Some are easier to read for some and some others prefer other standards. If that is the case (defining new standards for a new project), you should try using the most common formats.

With all that said, when there's only 1 line of code in the switch, I often go for the 3rd one. I find readability more important than consistent usage of standards (probably because consistent use of standards is meant to help read).

Upvotes: 1

boen_robot
boen_robot

Reputation: 1546

IMHO, the problem is that you don't always have just once statement per case, and that's the true problem - you're creating an inconsistency. Sometimes you have multiple statements below, sometimes not.

When you glance over a large code base, the second style may lead you to think the switch breaks without doing anything, while the third may lead you to think it's one of those cases where the break is intentionally omitted.

Of course, both of these disappear once you look more closely at the code and figure it out and/or get used to the inconsistency (and thus it turns into a consistency in your mind), but the whole point of coding styles/standards is so that you don't have to do that.

It's a similar deal with "if (condition) statement;" vs. "if (condition) {statement;}" - the latter is more generic, and thus requires less effort to read on large code bases, which is why most style guides insist on it.

Upvotes: 2

Related Questions