94

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?

fasseg
  • 17,504
  • 8
  • 62
  • 73
  • 2
    Didn't realise this until now! It's probably because switch was invented before indentation)) – rodion Dec 22 '10 at 12:22
  • 1
    NB does indent *switch* by default (and it looks more readable for me too) – barti_ddu Dec 22 '10 at 12:37
  • possible duplicate of [Why don't people indent C++ access specifiers/case statements?](http://stackoverflow.com/questions/4299729/why-dont-people-indent-c-access-specifiers-case-statements) – Johannes Schaub - litb Apr 10 '11 at 20:07
  • 3
    The official documentation doesn't follow the code convention: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.11 – Roland Mar 27 '14 at 14:19
  • Idea formats with indents – gstackoverflow Jul 25 '16 at 14:58
  • 1
    @gstackoverflow IDEA 15.0.5 it doesn't do it for me. I wish it did. I prefer indentation to correspond to logical groupings and the switch is one logical element, the effects of which one generally wants to consider as a unit. Thus I want to see it as a unit (e.g. indented). I also tend to add braces around the statements for the case sections too. – Gus Jul 27 '16 at 16:57

6 Answers6

62

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.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 11
    To put it another way, the contents of the `switch` block are indented one level from the `switch` itself, but the `case` s are "outdented" one level from the code they're mixed in with. – Steve Jessop Dec 22 '10 at 13:40
  • I was on the fence about this, leaning towards indenting since I thought it was a little more readable. This answer, along with the benefit of preventing excessive indentation/wrapping, changed my opinion. Thinking of the cases as labels rather than if conditions seems to help readability slightly when not indented. – Pilot_51 Sep 24 '13 at 15:52
  • 3
    The `case` parts act like labels in that the flow of execution will proceed through the label from the code above to the code below without a `break`. I still prefer to see them indented. Without the indent it hides, to my eye, the `switch` statement itself, making it hard to see the start of things. But this is only a real problem when there are too many cases and that a whole 'nother problem. – Lee Meador Oct 07 '13 at 17:18
  • 1
    What about the default clause? In Eclipse (Java) the default case of the switch is automatically indented, it is not "un-dented" like the cases are. I would think that easy readability for what happens in default cases equals to the need for readability in defined cases. – Chexxor Nov 28 '16 at 10:42
  • In Oracle's own documentation `switch` {`case`} is not indented: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – Eric D Apr 02 '18 at 13:14
  • 1
    What if you have braces for `case`s too? You'll end up with 2 closing braces at the same indentation level on different lines. – Iulian Onofrei Jul 24 '18 at 22:03
  • @Iul yes. Doesn't bug me, though. – Johannes Schaub - litb Jul 25 '18 at 07:13
  • In Oracle's own documentation cases are indented: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html, https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.11 – Bochu Oct 22 '19 at 13:53
  • I often uses braces (selectively) on cases, turning them into mini-blocks, in which case the indenting does make sense. My preference is to indent case labels, as it makes the code easier to read by showing that the cases are internal to the switch stmt, not global. – Alcamtar Jun 15 '20 at 20:54
28

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.

Community
  • 1
  • 1
kriss
  • 23,497
  • 17
  • 97
  • 116
  • 1
    I wouldn't say that they are necessarily labels, because you can think of them both as labels and as syntax sugar for a sequence of `if-else`s. Although, since they look like labels then maybe the indentation is more appropriate that way. – rodion Dec 22 '10 at 12:56
  • 31
    I don't think that rule describes the style in question. `switch` does introduce a block, and `case` doesn't. And yet `case` appears to introduce a new level of indentation, whereas `switch` doesn't. – Steve Jessop Dec 22 '10 at 13:37
  • 1
    @rodion: in terms of complexity C switch implementations are O(1) and the equivalent if-else strings are O(n). if-else expect to be followed by exactly one statement, not case, etc. You can also find references to cases as labels in C standard (didn't checked for the exact quote, but I'm quite sure I've seen it). – kriss Dec 22 '10 at 14:08
  • @Steve: `switch` doesn't by itself introduce a block. It is just block statement that people usually use as dependent statement that introduces the block. – Jens Gustedt Dec 22 '10 at 15:21
  • 1
    @kriss: All automatic variable declarations are definitions. They're just not initialized. There's no harm in putting initialized variable definitions at the beginning of a switch, but the initialization will never be performed, so it's useless. Also of course, static variable definitions are valid at the beginning of a switch, regardless of whether they're initialized. – R.. GitHub STOP HELPING ICE Dec 22 '10 at 16:06
  • @kriss: Oh, I see. Now I understand why expressions are not allowed in cases. Live and learn. – rodion Dec 23 '10 at 08:00
  • @R...: that is what I meant, no initialization, but you explain is better. Of course you are right, declaration is about saying some variable exists, definition is about memory allocation and it is definitely done. That is indeed initialization that is not performed for automatic variables. I should rephrase my answer. – kriss Dec 23 '10 at 09:23
  • @rodion: implementation details: C switches are usually implemented internally as jump tables, one value one jump label. – kriss Dec 23 '10 at 09:28
14

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.

Travis Well
  • 947
  • 10
  • 32
Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
7

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
}
rodion
  • 14,729
  • 3
  • 53
  • 55
6

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...

mingos
  • 23,778
  • 12
  • 70
  • 107
  • 3
    same for me, i really hate cases not indented, been programming for 20+ years, but i can also buy it's a subjective matter... – rupps May 13 '13 at 00:21
2

FWIW, another option is using two half-indents:

switch (i) {
  case 1:
    ...
  case n:
    ...
}
Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45