8

Possible Duplicate:
Why don't people indent C++ access specifiers/case statements?

I have a syntax question... not about the how, but rather the why. Many IDEs such as Eclipse and Qt Creator automatically indent a switch like this:

Day randomDay = getRandomDay(); /* returns 'enum Day' */

switch (randomDay) {
default:
case Monday:
  /* ... */
  break;
case Tuesday:
  /* ... */
  break;
/* ... */
}

I've always found that this is inconsistent with general code indentation rules and I prefer to do this:

Day randomDay = getRandomDay(); /* returns 'enum Day' */

switch (randomDay) {
  default:
  case Monday:
    /* ... */
    break;
  case Tuesday:
    /* ... */
    break;
  /* ... */
}

Similarly, C++ class definitions are often indented like this:

class MyClass {
public:
  /* ... */
}

As opposed to:

class MyClass {
  public:
    /* ... */
}

Why have some people chosen not to indent the case statements?

Community
  • 1
  • 1
Pieter
  • 31,619
  • 76
  • 167
  • 242
  • 5
    Because they disagree with you, obviously. Can't argue about taste. – Hans Passant Apr 10 '11 at 19:59
  • 4
    But what's their logic behind not indenting the `case` statements? There most be a reason why some prefer doing it that way, otherwise no one would do it. – Pieter Apr 10 '11 at 20:00
  • Is the question "why do some people choose to not indent?" If so, then the answer is one of the programmer's preference. If the question is "why do some IDEs refuse to indent?" then the answer often is that, because indention is usually a configurable option, the programmer has not told the IDE to do any different. – Pete Wilson Apr 10 '11 at 20:02
  • this is a duplicate. I wish there was an automatic duplicate finder on SO... I know this has been asked before. ... You find it here: http://stackoverflow.com/questions/4299729/why-dont-people-indent-c-access-specifiers-case-statements – Johannes Schaub - litb Apr 10 '11 at 20:03
  • Duplicate of http://stackoverflow.com/questions/4509039/why-the-strange-indentation-on-switch-statements – Daniel Rose Apr 10 '11 at 20:05
  • @Johannes: there is a duplicate finder, but its results usually don't include the duplicates because the wording is different. – Pieter Apr 10 '11 at 20:11
  • @Pieter ah in fact, the "related" list shows one of the duplicates :) – Johannes Schaub - litb Apr 10 '11 at 20:28
  • @Johannes: really? In that case, shame on me for wasting my own and everyone else's time. – Pieter Apr 10 '11 at 20:32
  • @Pieter but perhaps that's only because now we are linking to that question out of the comments and such. Maybe it wasn't there before. – Johannes Schaub - litb Apr 10 '11 at 20:37
  • I agree with ThiefMaster♦ that labels need'nt to be indented. The logic inside switch is like, do different things by different input, so there actually is only 1 level of indent. Therefore, the case labels are not part of the logic, so they dont have 1 level of indent. I personally also align the `break;` with `case`, because that looks more like a pair of brackets. – Marson Mao Nov 27 '13 at 06:16

3 Answers3

14

Code styles are like assholes. Everyone has one but only few people like the ones of other people.

This applies here, too. Some people liked it and thus did it when writing the indentation logic for an IDE. Most good IDEs have an option to configure this behavior though.

A possible reason for this style is that it seems to be common to not indent labels - and cases are a special kind of labels.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

This is purely a personal preference thing. Indenting levels, tabs vs. spaces, and curly-bracket placement are, and will always be, the topic of programmer flame wars.

Certain editors or IDEs have defaults which are setup according to "common" conventions among certain programmers in the target audience. If you don't like the defaults, there almost always a way to customize it.

If you are working on a team of programmers, you'll have to decide on certain conventions, otherwise, you'll end up reformatting eachother's code until the end of time!

Andy White
  • 86,444
  • 48
  • 176
  • 211
0

Eclipse allows a choice out of several preset styles, e.g. K&R, each of which is fully customizable, or creating your own defaults.

There is a perfectly reasonable explanation for each style choice, Stroustrup also addresses this in his C++ FAQ. And it does not matter - "do what feels good" (within reason).

davka
  • 13,974
  • 11
  • 61
  • 86