I often see stuff like this:
class SomeClass {
public:
void someMethod();
private:
int someMember;
};
This seems totally unnatural
As with many other things, it is important not to mistake the rule with the purpose. The purpose of indentation is making code clearer and easier to read by providing an extra visual hint on what belongs where. Now, in the particular cases you mention, together with namespace
s, in many cases the extra indentation does not help in readability. The case
s in a switch can be understood as if-else
s, where you would not add the extra indentation. The cases are block delimiters similar to the curly braces.
switch ( var ) {
case 1: // if ( var == 1 ) {
code;
case 2: // } else if ( var == 2 ) {
code;
}
At class level, access modifiers can be considered to be block delimiters at the same level that the class braces. Adding an extra level of indentation does not make the code clearer:
class test {
public:
void foo();
private:
int member;
};
The same way goes with namespaces, where some people avoid indenting the whole namespace level. In all three cases, there is no clear advantage in adding the extra indentation and if you abide to short code lines (80/100 characters) and big enough indentation levels (8 or even 4 characters) then there might be an advantage to not indenting.
Personally, I never indent case
or accessor modifiers, in the case of namespace
s, it depends... a namespace
that covers the whole source file will most probably not be indented, while namespaces that only take part of the source file will be indented --the rationale is that in the former case it adds no actual value, while in the second it does.
The access specifiers are really just labels (as in those used by goto
). People generally don't indent labels, or outdent them one level wrt the surrounding code. So I would say this is not inconsistent at all.
The Standard also uses this style for access specifiers. Example from Chapter 10, paragraph 2:
class Base {
public:
int a, b, c;
};
Because public
and private
are labels which don't introduce a new scope, I prefer not to give them any special indentation, thus:
class foo {
public:
void something();
void something_else();
private:
int top_secret;
};
This way, the consistent indentation rule is "indentation equals scope".
Two possible reasons:
that's how Bjarne Stroustrup indents them in his books
most text editors indent them that way automatically
It's all about scoping and grouping of branches. If these are not affected, then do not add an indentation level.
Take for instance the following:
if( test == 1 ) {
action1( );
} else if( test == 2 ) {
action2( );
} else {
action3( );
}
Take note of the levels of the statement blocks. Now re-write it as a case statement with indented cases:
switch( test ) {
case 1:
action1( );
break;
case 2:
action2( );
break;
default:
action3( );
break;
}
This does the exact same functionally, yet the indentations don't match of my actions. And I think it is this inconsistency that finally made me change to dropping the extra spurious indentation. (Even though I don't mind the half-indenting proposed by others, mind you.)
Imagine such a class definition:
class SomeClass {
void ImplicitlyPrivateMethod();
public:
void someMethod();
private:
int someMember;
};
With the style of indentation you propose, one would need to change this into
class SomeClass {
void ImplicitlyPrivateMethod();
public:
void someMethod();
private:
int someMember;
};
(which looks not so nice to many people, especially if the "implicit" section is long enough).
I personally prefer half-indentation of such specifiers:
class SomeClass {
void ImplicitlyPrivateMethod();
public:
void someMethod();
private:
int someMember;
};
But this is a matter of personal taste.