Why don't people indent C++ access specifiers/case statements?

前端 未结 10 1703
悲哀的现实
悲哀的现实 2020-12-16 11:13

I often see stuff like this:

class SomeClass {
public:
    void someMethod();
private:
    int someMember;
};

This seems totally unnatural

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 11:45

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

提交回复
热议问题