Why the strange indentation on switch statements?

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.


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.

Tags:

C

Java

Syntax