Why the strange indentation on switch statements?

JavaCSyntax

Java Problem Overview


Why is the imho missing indentation of the "case" - keywords in a switch statement considered good style?

No indentation of the "case" keyword seems to be the default formatting option in about every IDE:

switch (i){
case 0:
    break;
case 1:
    break;
}

while I find this format more intuitive:

switch (i){
    case 0:
        break;
    case 1:
        break;
}

Is there some logic behind this, that eludes me?

Java Solutions


Solution 1 - Java

The cases are logically labels. Many people put labels at the same indentation level as the block they are in. In my opinion, that way it's easier to read through the text.

I compare it with a timeline you can scroll through. You have markers on the time line itself, not indented into the content. You can then quickly point out where labels/markers are, without moving your eye away from the base-line.

Solution 2 - Java

In 4 words: no blocks, no indentation.

Cases are not opening a block. In C or C++ you can even put variables declarations (but the initializers are not called, except for static variables, that's a pitfall) at the beginning of the switch block. You can do many weird things with switch, like Duff's device.

Hence, as cases are just labels, indenting them does not seem that intuitive, and not indenting is the style chosen by most styles.

Solution 3 - Java

The 1999 official Oracle Code Conventions for the Java TM Programming Language (section 7.8) recommends a switch style where case statements are not indented relative to the switch statement as a whole.

This is a subjective choice, but Sun decided it is better if everyone stick to one style, and picked this.

Solution 4 - Java

Maybe it is to keep the same indentation level as its logical equivalent expressed in if statments? That is:

switch(i){
case 0:
  //do something 1
case 1:
  //do something 2
}

Would look similar to its logical equivalent:

if(i==0){
  //do something 1
}else if(i==1){
  //do something 2
}

Solution 5 - Java

There are different indentation styles to choose from. AFAIK, none is considered better style than the others as long as you consistently use an indentation style at all. For me, indenting case labels is more readable, same goes for private, protected and public labels in classes, however, my IDE won't do the indentation my way. My code isn't as readable as I'd like it to be this way. Oh well...

Solution 6 - Java

FWIW, another option is using two half-indents:

switch (i) {
  case 1:
    ...
  case n:
    ...
}

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
QuestionfassegView Question on Stackoverflow
Solution 1 - JavaJohannes Schaub - litbView Answer on Stackoverflow
Solution 2 - JavakrissView Answer on Stackoverflow
Solution 3 - JavaJohan KotlinskiView Answer on Stackoverflow
Solution 4 - JavarodionView Answer on Stackoverflow
Solution 5 - JavamingosView Answer on Stackoverflow
Solution 6 - JavaSolomon UckoView Answer on Stackoverflow