Why don't people set C ++ access specifiers or cases?

I often see things like this:

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

This seems completely unnatural to me (the same applies to case -statements when using switch ). I expected something like this when I started using C ++ (a lot of time has passed since then, but I'm still interested):

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

Is there a good reason for breaking (otherwise) sequential indentation rules?

+21
c ++ coding-style indentation
Nov 28 '10 at
source share
10 answers

An increase in indentation usually reflects a record in a new nested scope, while both access specifiers and switch case do not change the scope (the same applies to labels in general). Access specifiers may be optional, because you can start implementing a class or structure, and all members only need implied access (for example, private and public, respectively), but then the code develops, and you need to add a specifier for the non-implied - access members: are they really should you suddenly change the indentation for all members? Again, there are no nested areas, so I think this will be misleading.

If you work on fixed-width terminals and wrap your lines accordingly, then overpowering the pain. But it’s useful that access specifiers stand out from members, which means that they are somewhere on the left — either according to the class keyword, or where there is a part.

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 don't I back off the code for each case further? I can not say that I am doing this especially logically, but factors include:

  • I don’t want to discount the general indentation of switch / case , since the visual transmission of the degree of switching is important for quick understanding of the code.
  • I'm glad to just put { and } on the existing case code - rather like the comment “hey, I need volume” - instead of feeling irresistible to indent further and further, I mean even for me, but I feel right - I like to have code for each line of case
  • some compilers warn if you enter new variables inside the case without entering a scope, even if there are no cases where this variable is later used by a potentially uninitialized one.
  • I, as a rule, have an 80-column encoder, with 4 indents, therefore, being inside a switch - and, therefore, as a function, means that the rest of the columns are valuable.

Same thing for class / struct :

  • I want to scan the left column and quickly find the end of the class, or easily count small classes on the screen or in the printout; access specifiers at the same level of indentation invalidate the class keyword, but this helps to visually distinguish them from members (if the class / structure is more than a few lines, I also add an empty line in advance)
  • I do not want to modify the existing indent of the existing class / when I enter the private or protected specifier

In conclusion: many small factors go into the development of indentation preferences for people, and if you want to be a C ++ programmer in a corporate environment - especially a contractor - you just need to go with the flow and be able to change your own style sometimes too (for example, I stuck in camelCaseLand right now, with public member variables starting with an uppercase letter - yikes!). Do not sweat - not worth it.

+27
Nov 29 '10 at 0:48
source share

Access specifiers are simply shortcuts (as in those used by goto ). Typically, people are not indented or overlaid by the surrounding code. Therefore, I would say that this does not contradict at all.

EDIT:

The standard also uses this style for access specifiers. Example from paragraph 2 of chapter 10:

 class Base { public: int a, b, c; }; 
+12
Nov 28 '10 at 23:25
source share

Imagine a class definition like this:

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

With the indentation style that you suggest, you would need to change this to

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

(which doesn’t look very good for many people, especially if the “implicit” section is long enough).




I personally prefer semi-indexing such specifications:

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

But this is a matter of personal taste.

+8
Nov 28 2018-10-28T00:
source share

As with many other things, it’s important not to allow rules with purpose. The purpose of the indentation makes the code more understandable and understandable, providing an additional visual hint of what belongs. Now, in the specific cases that you mention, along with namespace s, in many cases the extra indentation does not help in readability. case in the switch can be understood as if-else s, where you do not add extra indentation. Cases are block delimiters, similar to braces.

 switch ( var ) { case 1: // if ( var == 1 ) { code; case 2: // } else if ( var == 2 ) { code; } 

At the class level, access modifiers can be considered block separators at the same level as the cluster. Adding an extra level of indentation does not make the code more understandable:

 class test { public: void foo(); private: int member; }; 

The same thing happens with namespaces, where some people avoid retreating from the entire level of the namespace. In all three cases, there is no clear advantage in adding extra indentation, and if you observe short lines of code (80/100 characters) and sufficiently large levels of indentation (8 or even 4 characters), then there may be an advantage not to indent.

Personally, I have never backed off case or access modifiers, in the case of namespace s, it depends ... a namespace that spans the entire source file will most likely not be indented, and namespaces that only take part in the source file will be indented - it is reasonable that in the first case it does not add any actual value, and in the second it does.

+7
Nov 28 '10 at 23:51
source share

Two possible reasons:

  • that Bjarn Straustup puts them off in his books

  • most text editors automatically indent them automatically

+6
Nov 28 '10 at 23:34
source share

All about the scope and grouping of branches. If they are not affected, then do not add an indent level.

Take for example the following:

 if( test == 1 ) { action1( ); } else if( test == 2 ) { action2( ); } else { action3( ); } 

Pay attention to the operator block levels. Now rewrite it as an indented case statement:

 switch( test ) { case 1: action1( ); break; case 2: action2( ); break; default: action3( ); break; } 

This does the same functionally, but the indentation does not match my actions. And I think that it was precisely this inconsistency that finally forced me to change the rejection of an extra false indent. (Despite the fact that I am not against the half-indent proposed by others, mind you).

+3
Jan 28 '13 at 16:50
source share

There are very few access modifier lines in a class, and most editors have color modifiers differently from everything else. They stand out in abundance on their own, so why add extra tabs?

+1
28 nov. '10 at 23:27
source share

Since public and private are labels that do not introduce a new scope, I prefer not to give them any special indentation, this way:

 class foo { public: void something(); void something_else(); private: int top_secret; }; 

Thus, a consistent indentation rule is equal to "padding is equal to scope".

+1
Nov 29 '10 at 12:10
source share

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 a long or long file with many long switch statements, I use more indentation to make reading easier

I sometimes do what I feel like an old style

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

This sometimes simplifies the work when a file can contain more than 20 or 50 functions, so you can easily determine the beginning of each function

0
Nov 29 '10 at 2:36
source share

As mentioned earlier (although argued for immeasurable access modifiers), access modifiers form logical blocks. Although they are at the same level as the class brackets, they are special.

Thus, it is useful to have padding to clearly show where each block begins and ends.

I personally think that it makes the code more understandable. Others will disagree.

This is a rather subjective question.

0
Dec 03 '10 at 16:20
source share



All Articles