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?
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.
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!
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).
精彩评论