C Switch-case curly braces after every case

CSwitch Statement

C Problem Overview


In a C switch-case flow control, it's required to put curly braces { } after a case if variables are being defined in that block.

Is it bad practice to put curly braces after every case, regardless of variable declaration?

For example:

switch(i) {
  case 1: {
    int j = 4;
    ...code...
  } break;

  case 2: {  //No variable being declared! Brace OK?
    ...code...
  } break;
}

C Solutions


Solution 1 - C

It's certainly not invalid to use braces in every case block, and it's not necessarily bad style either. If you have some case blocks with braces due to variable declarations, adding braces to the others can make the coding style more consistent.

That being said, it's probably not a good idea to declare variables inside case blocks in straight C. While that might be allowed by your compiler, there's probably a cleaner solution. Mutually-exclusive case blocks may be able to share several common temporary variables, or you may find that your case blocks would work better as helper functions.

Solution 2 - C

Braces may be used in every case statement without any speed penalty, due to the way compilers optimize code. So it's just the style and the preference of the coder.

  • The most preferred usage is not using braces, though the usage of them in every case during an active development may be found easier to make some additions on the code every now and then.

  • It's just the easthetics; because a 'case' statement doesn't need only a single command, but will walk through the code as it works as a label. So blocks are not needed, and are not invalid.

  • In 'case's with variables; braces are used just-in-case, to create contexts for variables, and it makes big sense to use them. Some compilers on different platforms show different behaviours if they are not included.

Solution 3 - C

Generally it is bad practice jump over the initialization of a variable, be it with goto or switch. This is what happens when you don't have the the blocks per case.

There is even a case in C99 where jumping over the initialization is illegal, namely variable length arrays. They must be "constructed" similarly as non-PODs in C++, their initialization is necessary for the access of the variable later. So in this case you must use the block statement.

Solution 4 - C

I consider it bad style to use braces in each case. Cases are labels in C, akin to goto labels. And in the current C language, you're free to declare variables in each case (or anywhere you like) without introducing new blocks, though some people (myself included) also consider that bad style.

Solution 5 - C

Just to add a minor point many editors & IDEs allow blocks to be collapsed and/or auto indented and several allow you to jump to the matching brace - I personally don't know of any that allow you to jump from a break to the matching case statement.

When debugging, or re-factoring, other peoples, (or even your own after a few months), code that contains complex case statements the ability to both collapse sections of the code and to jump to matching cases is invaluable, especially if the code contains indentation variations.

That said it is almost always good advice to avoid complex case statements like the plague.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenView Question on Stackoverflow
Solution 1 - CbtaView Answer on Stackoverflow
Solution 2 - CDarkWingDuckView Answer on Stackoverflow
Solution 3 - CJens GustedtView Answer on Stackoverflow
Solution 4 - CR.. GitHub STOP HELPING ICEView Answer on Stackoverflow
Solution 5 - CSteve BarnesView Answer on Stackoverflow