title: C++ coding style
body: <p>not sure if my question suits all rules for StackOverflow question, but I think it will be helpful for future users.</p>
  
  <p>Now I need to choose coding style for a few C++ projects where I'm participating. These projects are big enough and there will be a few programmers working on every. So we need to equalize our code style. Also we want to choose coding style which is <em>socially acceptable</em>, so first I'll tell what we already decided to have. My question is, if some of our choices is not <em>socially unaccepted</em> and also what are others commonly using C++ coding style rules.</p>
  
  <p>So here what we chosen:</p>
  
  <h1>File naming</h1>
  
  <p>Start with a capital letter and have a capital letter for each new word (no underscores, no spaces).</p>
  
  <p>For example:</p>
  
  <pre><code>VeryImportantClass.h
  VeryImportantClass.cpp
  </code></pre>
  
  <h1>Namespace naming</h1>
  
  <p>Start with a capital letter and have a capital letter for each new word (no underscores, no spaces). Also alignment should be appropriate.</p>
  
  <p>For example:</p>
  
  <pre><code>namespace Drinks
  {
      namespace AlcoholDrinks
      {
  
      }
  }
  </code></pre>
  
  <h1>Namespace structure</h1>
  
  <p>In header file the have only functions/methods prototypes, realization in <code>cpp</code> file, avoid using <code>using namespace</code> for realization file.</p>
  
  <p>Example:</p>
  
  <pre><code>//header
  namespace CommonStuff
  {
      namespace SystemParameters
      {
          bool IfWindows();
          //some more stuff...
      }
  }
  
  //cpp file
  namespace CommonStuff
  {
      namespace SystemParameters
      {
          bool IfWindows()
          {
              //some stuff...
              return ...;
          }
      }
  }
  </code></pre>
  
  <h1>Classes and structures naming</h1>
  
  <p>Start with a capital letter and have a capital letter for each new word (no underscores, no spaces). No <em>C</em> like class prefix or <em>S</em> like struct prefix. We decided - it is just more typing.</p>
  
  <p>Example:</p>
  
  <pre><code>class MyClass
  {
  
  };
  
  struct MyStruct
  {
  
  };
  </code></pre>
  
  <h1>class or struct</h1>
  
  <p>In some cases it is difficult to understand if we need class or struct. If structure just keep some grouped data - it is <code>struct</code>. If structure keeps data and has methods - it is <code>class</code>. Exceptional methods are constructor, destructor and comparison operators.</p>
  
  <p>Example:</p>
  
  <pre><code>class MyClass
  {
  public:
      MyClass();
      ~MyClass();
      void SetValue(int value);
      int GetValue();
      void PrintValue();
  private:
      int m_value;
  };
  
  struct MyStruct
  {
      MyStruct();
      ~MyStruct();    
      int value;
  };
  </code></pre>
  
  <h1>Type names</h1>
  
  <p>Start with a capital letter and have a capital letter for each new word (no underscores).</p>
  
  <p>For example:</p>
  
  <pre><code>typedef std::string String;
  typedef std::vector&lt;String&gt; StringVector;
  </code></pre>
  
  <h1>Variable types</h1>
  
  <p>Use our own predefined types, we have:</p>
  
  <pre><code>typedef std::string String;
  typedef std::vector&lt;String&gt; StringVector;
  typedef unsigned char Byte;
  typedef std::vector&lt;Byte&gt; ByteVector;
  //etc.
  </code></pre>
  
  <h1>Variable naming</h1>
  
  <p>Start with a lower letter and have a capital letter for each new word (no underscores).</p>
  
  <p>Example:</p>
  
  <pre><code>String messageLicenseExpired = "Your product version is expired, please...";
  int importantNumber = 13;
  </code></pre>
  
  <h1>Class variables naming</h1>
  
  <p>Starts with prefix <strong>m_</strong> then word starts with a lower letter and have a capital letter for each new word (no underscores).</p>
  
  <p>Example:</p>
  
  <pre><code>int m_myVariable;
  int m_otherVariable;
  </code></pre>
  
  <h1>Constants</h1>
  
  <p>Use all capitals with underscores.</p>
  
  <p>Example:</p>
  
  <pre><code>const String PRODUCT_NAME = "our product";
  const Byte IMPORTANT_NUMBER = 13;
  </code></pre>
  
  <h1>Constants or preprocessor</h1>
  
  <p>If value will be checked using <code>#ifdef</code> or some others, then it must be preprocessor definition. Otherwise it is <code>const</code>.</p>
  
  <p>For example:</p>
  
  <pre><code>#define FAILURE_FACTOR_FOR_DEBUG 50
  const int MAGIC_NUMBER = 5;
  
  //some code...
  
  String newString = someString.substr(MAGIC_NUMBER);
  
  //some code...
  
  //not the best example, but I think it is understandable.
  #ifdef _DEBUG
      int someValue = FAILURE_FACTOR_FOR_DEBUG;
  #else
      int someValue = 0;
  #end
  </code></pre>
  
  <h1>Function and methods naming</h1>
  
  <p>Start with a capital letter and have a capital letter for each new word (no underscores).</p>
  
  <p>For example:</p>
  
  <pre><code>int CalculateSometing (int n);
  void ToUpper (String&amp; someStr);
  </code></pre>
  
  <h1>Braces</h1>
  
  <p>Braces should always go into new line, except initializing.</p>
  
  <p>Example:</p>
  
  <pre><code>int arr[] = {1, 2, 3};
  if (arr[0] &gt; 10)
  {
      //do something
  }
  else
  {
      //do something else
  }
  </code></pre>
  
  <h1>else</h1>
  
  <p><code>else</code> belongs to new line, see previous example.</p>
  
  <h1>if statement and braces</h1>
  
  <p>Even single code line after <code>if</code> or <code>else</code> should be enclosed.</p>
  
  <p>Example:</p>
  
  <pre><code>if (someInt &gt; 100)
  {
      someInt = 100;
  }
  else
  {
      someInt /= 2;
  }
  </code></pre>
  
  <h1>Methods calling</h1>
  
  <p>No space around arrow and dot.</p>
  
  <p>Example:</p>
  
  <pre><code>Object obj;
  Object* oPtr = new Object();
  
  obj.Method();
  obj-&gt;Method();
  </code></pre>
  
  <h1>Header files</h1>
  
  <ol>
  <li>use <code>#pragma once</code> in stead of define guards. (<code>#pragma once</code> is not standart so in some compilers define guards are must)</li>
  <li>One header for one class only.</li>
  <li>Header files only for definitions. Execution instructions must be in related <code>cpp</code> file even if it getter or setter. It is because changes in header leads into long compiling.</li>
  </ol>
  
  <h1>Pointers and references</h1>
  
  <p>Use reference instead of pointer if it is possible. If possible pass parameter as a reference (for objects), prefer to pass as a <code>const</code> reference if value will not be changed. </p>
  
  <p>Example:</p>
  
  <pre><code>String ToUpper(String str);           //bad
  String ToUpper(String&amp; str);          //better
  String ToUpper(const String&amp; str);    //best
  void ToUpper(String&amp; str);            //also solution
  </code></pre>
  
  <h1>Error handling</h1>
  
  <p>If function may fail, it must return <code>bool</code> value <code>true</code> for success and <code>false</code> for failure. For classes method <code>GetLastError()</code> is a must. For function that may fail error code should be returned through additional parameter, e.g. <code>bool Function(int param, int* errorCode = NULL)</code> Also we decided not to use exceptions in our code.</p>
  
  <h1>class structure</h1>
  
  <p>In header file first public methods (constructors and destructors at the top of them), protected methods, protected variables, private methods, private variables. No public variables, use getters and setters.</p>
  
  <p>Example:</p>
  
  <pre><code>class MyClass
  {
  public:
      MyClass();
      ~MyClass();
      int GetPrivateValue();
      void SetPrivateValue(int value);
      int GetProtectedValue();
      void SetProtectedValue(int value);
  protected:
      void SomeMethod();
      int m_protectedValue;
  private:
      void SomePrivateMethod();
      int m_privateValue;
  };
  </code></pre>
  
  <h1>Formatting</h1>
  
  <ol>
  <li>Alignment use 4 spaces or tab.</li>
  <li>Long line wrapping, no longer lines then 120 symbols.</li>
  </ol>
  
  <h1>Self documenting code</h1>
  
  <p>Comments are always welcome but the best choice is to give name for variables and functions which explains everything.</p>
  
  <p>Example:</p>
  
  <pre><code>void Function (const String&amp; str, const String&amp; str2, StringVector&amp; vect);   //very bad
  
  //This functions tokenize string, str is input string, str2 is delimiters string, vect output
  void Function (const String&amp; str, const String&amp; str2, StringVector&amp; vect);    //quite bad
  
  void Tokenize (const String&amp; inputString, const String&amp; delimiters, StringVector&amp; output);    //good, anyway comments using this declaration also welcome.
  </code></pre>
  
  <h1>&amp; and * position</h1>
  
  <p>Write <code>&amp;</code> and <code>*</code> just after variable type.</p>
  
  <p>Example:</p>
  
  <pre><code>String* strPtr;
  String&amp; strRef;
  </code></pre>
  
  <p>It is all we decided to use, the question is, haven't we missed something? Also, is there anything <em>globally unacceptable</em>?</p>
  
  <p>Feel free to comment and ask if something is not clear, why we chosen some.</p>
  
  <p>Hope it will be helpful for latter readers.</p>