Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, January 30, 2016

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

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?

Answer by John Dibling for Why don't people indent C++ access specifiers/case statements?


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;  };  

Answer by robert for Why don't people indent C++ access specifiers/case statements?


There are very few access modifier lines per class, and most editors color modifiers differently from everything else. They stand out plenty on their own, so why add unnecessary tabs?

Answer by Vlad for Why don't people indent C++ access specifiers/case statements?


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.

Answer by please delete me for Why don't people indent C++ access specifiers/case statements?


Two possible reasons:

  • that's how Bjarne Stroustrup indents them in his books

  • most text editors indent them that way automatically

Answer by David Rodrguez - dribeas for Why don't people indent C++ access specifiers/case statements?


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.

Answer by Tony D for Why don't people indent C++ access specifiers/case statements?


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.

Answer by Dr. Mina Mounir for Why don't people indent C++ access specifiers/case statements?


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

Answer by Karl Knechtel for Why don't people indent C++ access specifiers/case statements?


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

Answer by Srdjant for Why don't people indent C++ access specifiers/case statements?


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.

Answer by Marius for Why don't people indent C++ access specifiers/case statements?


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


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.