Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 241
jwueller Avatar asked Nov 28 '10 23:11

jwueller


People also ask

Are indentations necessary in C?

Indentation improves the readability of the code. It is mainly used for code inside looping statements, control structures, functions etc. as good intended code is easy to maintain and is good looking. It makes the code more readable and easy to understand.

Does C language have access specifiers?

In C++, there are three access specifiers: public - members are accessible from outside the class. private - members cannot be accessed (or viewed) from outside the class. protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.

Why are access specifiers needed?

Access Modifiers or Access Specifiers in a class are used to assign the accessibility to the class members, i.e., they set some restrictions on the class members so that they can't be directly accessed by the outside functions.

What is the role of access specifiers in inheritance?

The access specifiers only affect whether outsiders and derived classes can access those members. Second, when derived classes inherit members, those members may change access specifiers in the derived class. This does not affect the derived classes' own (non-inherited) members (which have their own access specifiers).


2 Answers

Increasing indentation normally reflects entry to a new nested scope, whereas both access specifiers and switch case statements don't vary the scope (the same is true of labels generally). Access specifiers can be optional in that you may start implementing a class or struct and have all members only need the implied access (i.e. private and public respectively), but then the code evolves and you need to add a specifier for the non-implied-access members: is it really worth having to suddenly change the indentation on all members? Again, there's no nested scope, so I think that would be actively misleading.

If you work on fixed-width terminals and wrap your lines accordingly, reindenting's a pain. But, it is useful to have the access specifiers stand out from the members, which means putting them back to the left somewhere - either in line with the class keyword or part-way there.

Personally, I do this:

class X {   public:     int member_function()     {         switch (expression)         {           case X:             return 6;            default:           {             int n = get_value() / 6;             return n * n;           }         }     }    private:     int member_variable_; }; 

Why do I not indent code for each case further? I can't claim what I do is particularly logical, but factors include:

  • I don't want to deemphasise the general switch/case indentation, as visually communicating the extent of the switch is important to quickly understanding the code
  • I'm happy just to put the { and } around existing case code - more like a comment "hey, I need a scope" - rather than feeling compelling to indent everything further, which doesn't make much sense even to me but kind of feels right - I like having the code for each case line up
  • some compilers do warn if you introduce new variables inside a case statement without introducing a scope, even if there are no cases when the variable is later used potentially uninitialised
  • I'm an 80-column coder generally, with 4 space indents normally, so being inside a switch - and thus obviously a function - means remaining columns are valuable.

Same for class/struct:

  • I want to scan the left column and quickly find the end of the class, or easily count the small classes on screen or in a printout; access specifiers at exactly the same indentation level deemphasise the class keyword, but it does help to have them visually distinct from the members (if the class/struct is more than a few lines, I also add a blank line beforehand)
  • I don't want to change existing class/struct member indentation when I introduce a private or protected specifier

In conclusion: lots of small factors go into developing people's indentation preferences, and if you want to be a C++ programmer in a corporate environment - especially a contractor - you just have to go with the flow and be able to change your own style sometimes too (e.g. I'm stuck in camelCaseLand right now, with public member variables starting with an uppercase letter - yikes!). Don't sweat it - it's not worth it.

like image 153
Tony Delroy Avatar answered Oct 07 '22 18:10

Tony Delroy


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; }; 
like image 28
John Dibling Avatar answered Oct 07 '22 18:10

John Dibling