Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't case statement inside a switch being indented by some IDEs? [duplicate]

Tags:

c++

c

syntax

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?

like image 790
Pieter Avatar asked Apr 10 '11 19:04

Pieter


2 Answers

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.

like image 78
ThiefMaster Avatar answered Oct 24 '22 06:10

ThiefMaster


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!

like image 1
Andy White Avatar answered Oct 24 '22 06:10

Andy White