Switch statement formatting

coding-style, switch-statement

Solution

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.

Problem

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?

Original source