I often see stuff like this:
class SomeClass {
public:
void someMethod();
private:
int someMember;
};
This seems totally unnatural to me (the same applies to case
-statements when using switch
). I expected something like this, when i started using C++ (it's been a long time since then, but i am still wondering):
class SomeClass {
public:
void someMethod();
private:
int someMember;
};
Is there a funded reason to break (otherwise) consistent indentation rules?
Most editors Indent them automatically, for me i leave them as they are in small classes or small files or short switch statements but for long ones or long file with many long switch statements i use more indentation for easier readability
I sometimes do this which i feel as old style
This sometimes make it easier when a file may contain more than 20 or 50 function so u can easily spot the beginning of every function
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.EDIT:
The Standard also uses this style for access specifiers. Example from Chapter 10, paragraph 2:
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. Thecase
s in a switch can be understood asif-else
s, where you would not add the extra indentation. The cases are block delimiters similar to the curly braces.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:
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 ofnamespace
s, it depends... anamespace
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.Because
public
andprivate
are labels which don't introduce a new scope, I prefer not to give them any special indentation, thus:This way, the consistent indentation rule is "indentation equals scope".
As mentioned earlier (though argued for non-indented access modifiers), access modifiers form logical blocks. While these are at the same level as the class braces, they are special.
Thus it's useful to have indentation to clearly show where each block starts and ends.
I personally think it makes the code clearer. Others will dissagree.
This is a rather subjective question.
Imagine such a class definition:
With the style of indentation you propose, one would need to change this into
(which looks not so nice to many people, especially if the "implicit" section is long enough).
I personally prefer half-indentation of such specifiers:
But this is a matter of personal taste.