C++ naming::FAQ



Keywords: c++ naming, cpp naming, code style, code formating, coding convention.
Description: My vison of naming convention in C++ programing language.


Questions:
 Q: Do I use this naming convention?
 Q: Easy to read vs. easy to write?
 Q: Why there are different levels of naming?
 Q: Why Mpu_ or something similar is not prefix for public?
 Q: Ds_ vs. Dl_ ? GTs_ vs. GTl_? ... where l comes from local?
 Q: Why underscore?
 Q: Why prefix and not postfix?
 Q: Why are base classes and derived classes under same letter?
 Q: Why are members and functions of base classes and derived classes under same letter?
 Q: Is it possible to mix different levels of naming?
 Q: M0s_foo vs. Ms0_foo?
 Q: Why Irc_, Irsc_ exist?
 Q: Is it possible to mix with other rules?
 

 IoC: Instead of conclusion.

 Q&A:

 
 Q: Do I use this naming convention?
 A: YES!
  TOP

 Q: Easy to read vs. easy to write?
 A: We write code only "once", and read it zillion times.
    Answers to other questions are answer to this question too.
  TOP

 Q: Why there are different levels of naming?
 A: People write programs and people are lazy.
    All classes are not that important that we have to write M1sc_blah_blah.
    That's why there are simple ways of naming ( putting prefixes to be precise ) for simple cases.
  
 TOP

 Q: Why Mpu_ or something similar is not prefix for public?
 A: My goal was to make prefix as small as possible.
    If prefix would be bigger this convention would massive.
    In that case it would be useless.
    Number was the only choice.
    Even though it was not the best solution it was the only one.
    There are also other letters which are taken from first letters.
    Let's add next question on this one.
  TOP

 Q: Ds_ vs. Dl_ ? GTs_ vs. GTl_? ... where l comes from local?
 A: This motive comes from static function and static class members.
    Also letter s is easy to distinguish form 1 (one).
  TOP

 Q: Why underscore?                                                                      
 A: Underscore is replacement for space.
    Words in simple text is separated by space.
    While naming the need to put more words in names occurred.
    Following the rules like in simple text underscore is the only solution.
    If people wanted to save paper they would come out with some "better" letter.
    Reason for using blank probably has psychologically nature.
    It represents physical border between two words, which make reader to start analyzing word he read.
    There are lot of things to be said concerning this matter.
    Here are some practical examples:
     with: F_blah_foo_yes_no, Fv_Blah_Foo_Yes_Yo, Fc_Blah_Foo_Yes_Yo 
     without: FBlahFooYesNo,    fvBlahFooYesNo, FcBlahFooYesNo.
  TOP

 Q: Why prefix and not postfix??                                                   
 A: Why prefix:                                                                        
    While writing:
     In some way prefix makes us think in advance.
     Worming up for work, it represents fore-play and everything else concerning introduction.
     When we write prefix we already knew what we want and why.
     Everything else after that is easy and it easy to write real name.
     Word completion programs sort word in lexical order!
    While reading:
     It is similar as in writing,     
     except that in some way our eyes are focused on middle on the word, where the real name is.
     The truth is the physical brunt has been moved forward but 
     underscore keeps bringing it back toward the brunt of real name.
     While reading eyes are going from left to right, so that prefix is neglected and real name is read without disturbance.
     
   Why not postfix:                                                                    
    While writing:
     When we finish point of the story we hate to thinking where or who the story is coming from.
     With prefix we finished that job at the beginning.
     We payed toll at the entrance.
    While reading:
     It is similar as in writing.
     After we read that little fact it starts to disturb us a little bit.
     It looks like a runner who come in first and triped at the finish line.
     Ia ko je stigao prvi stalno ce ga peci svest sto nije iz sebe izvukao maksimum.
     Even though he come in first he will always regret because he did not do his best.
     While we are reading our conscience is making us to read till the end, 
     and again we have previous situation.
     
    Conclusion: PREFIX rulez!!!
    
  TOP

 Q: Why are base classes and derived classes under same letter?
 A: Code is liable to quick changes.
    Base class easily becomes derived class, and vice versa.
    In that case Search/replace can be very painfully if it is necessary to perform it on many files.
  TOP

 Q: Why are members and functions of base classes and derived classes under same letter?
 A: Code is liable to quick changes.
    Base class easily becomes derived class, and vice versa.
    In that case Search/replace can be very painful if it is necessary to perform it on many files.
  TOP

 Q: Is it possible to mix different levels of naming?
 A: Yes. When there is a point of doing it and it is not ambiguous.
  TOP

 Q: M0s_ vs. Ms0_ ?
 A: Information about access is little bit important then static.
  TOP

 Q: Why Irc_, Irsc_ exist?
 A: Maybe they are not that important but sometimes ( very rarely ) we can find them useful.
   TOP

 Q: Is it possible to mix with other rules?
 A: If we look at namings as filters then mixing can come down to following sequence of applying.

   Example:
    One of the rules demands that first letters describe type of variable.
    Examples:
    
      int       iFoo  // prefix i indicates that Foo is int
      unsigned  uFoo  // prefix u indicates that Foo is unsigned
      void     *pvFoo // prefix i indicates that Foo is void pointer

    There two ways of mixing these two types.
    
    The first way is to use previous style on witch we add my style. 
     Example: 
              int I_iFoo // Prefix i is added to name Foo which means Foo is int
                         // and then I_ is added on preveous which mean that Foo is instance. 
                                     
       unsigned  M_uFoo  // was added to name Foo which means that Foo is unsigned
                         // and then M_ was added too which indicates that Foo is member of class.
                         
                         
       void     *P_pvFoo // was added on name Foo which means is void*
                         // and then P_ was added on previous which means that Foo parameter of input/output type.

    The other one is vice verse.

       int       iI_Foo  // Prefix I_ was added to name Foo which means that Foo is variable.
                         // and then we ad prefix i to indicate int.
                         
       unsigned  uM_Foo  // M_ was added to name Foo which means that Foo is  member of class
                         // and then we add prefix u to indicate unsigned.
                         
       void     *pvP_Foo // Prefix P_ was added to name Foo which means that Foo is parameter of input/output type
                         // an then we ad prefix pv to indicate void*.

    I would NOT recommend that other way because it can be very confusing since capital letter is not really 
    a barrier between these two stiles.
    
  TOP
 


Instead of conclusion: This is 90% of basic idea. The rest of it will come with time.