Why don't people indent C++ access specifiers/

2019-01-26 03:22发布

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?

10条回答
做个烂人
2楼-- · 2019-01-26 03:51

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

Class CClass
    {
            CClass();
            ~CClass();
        Public:
            int a;
        Private:
            int b;
     };
CClass::CClass
      {
           TODO code;
      }

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

查看更多
女痞
3楼-- · 2019-01-26 03:52

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:

class Base {
public:
  int a, b, c;
};
查看更多
成全新的幸福
4楼-- · 2019-01-26 03:53

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 namespaces, in many cases the extra indentation does not help in readability. The cases in a switch can be understood as if-elses, 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 namespaces, 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.

查看更多
Rolldiameter
5楼-- · 2019-01-26 03:54

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".

查看更多
Root(大扎)
6楼-- · 2019-01-26 03:54

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.

查看更多
7楼-- · 2019-01-26 03:56

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.

查看更多
登录 后发表回答