Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
dev:dg_code_style_guide [2017/01/27 19:46] devadev:dg_code_style_guide [2017/09/13 09:04] (current) – [DrumGizmo Code Styleguide] deva
Line 3: Line 3:
 | **//Code is written once by one person but read many times by many people.//** | | **//Code is written once by one person but read many times by many people.//** |
  
-What this essentially boils down to is; don't make syntactic short-cuts that makes it easier to write the code if it makes it harder to read the the code.+What this essentially boils down to is; don't make syntactic short-cuts that makes it easier to write the code if it makes it harder to read the code.
  
 Always write comments to logical blocks in the code that explains for example what a for loop does or how a couple of function calls interact. Always write comments to logical blocks in the code that explains for example what a for loop does or how a couple of function calls interact.
Line 15: Line 15:
 =====Curly Braces===== =====Curly Braces=====
 Use [[https://en.wikipedia.org/wiki/Indent_style#Allman_style|BSD/Allman style]]. Use [[https://en.wikipedia.org/wiki/Indent_style#Allman_style|BSD/Allman style]].
-This basically boils down to 'curly braces always on a new line' with the only exception being switch statements (see later section) as this:+This basically boils down to 'curly braces always on a new line':
 <code c++> <code c++>
 +namespace Foo
 +{
 +
 void function() void function()
 { {
Line 24: Line 27:
   }   }
 } }
 +
 +} // Foo::
 </code> </code>
  
Line 37: Line 42:
 { {
   then_that();   then_that();
 +}
 +else
 +{
 +  else_this();
 } }
 </code> </code>
Line 43: Line 52:
 Plain switch case: Plain switch case:
 <code c++> <code c++>
-switch(value) {+switch(value) 
 +{
 case 1: case 1:
   break;   break;
Line 55: Line 65:
 If a variable needs to be declared inside a case further braces are needed. They should be indented as follows: If a variable needs to be declared inside a case further braces are needed. They should be indented as follows:
 <code c++> <code c++>
-switch(value) {+switch(value) 
 +{
 case 1: case 1:
   {   {
Line 78: Line 89:
 { {
 public: public:
-  MyClass(int someMember);+  MyClass(int some_member);
  
 protected: protected:
Line 86: Line 97:
  
 <code c++> <code c++>
-MyClass::MyClass(int someMember+MyClass::MyClass(int some_member
-  : someMember(someMember+  : some_member(some_member
-  , SomeBaseClass(someMember+  , SomeBaseClass(some_member
-  , SomeOtherBaseClass(someMember)+  , SomeOtherBaseClass(some_member)
 { {
   // ...   // ...
Line 121: Line 132:
 Use [[http://www.emacswiki.org/emacs/SmartTabs|"SmartTabs"]], ie. tabs for indentation and spaces for alignment. Use [[http://www.emacswiki.org/emacs/SmartTabs|"SmartTabs"]], ie. tabs for indentation and spaces for alignment.
  
-Tab-width is irrelevant as it is up to the editor to show them in whatever manner the programmer likes them.+<code> 
 +--->// Tab size: 4             ->// Tab size: 2 
 +--->if(foo)                    ->if(foo) 
 +--->                         ->{ 
 +--->--->char q[2][2] = {0, 1,  ->->char q[2][2] = {0, 1,  // "0" and "2" 
 +--->--->................2, 3}; ->->................2, 3}; // remain aligned 
 +--->                         ->} 
 +</code> 
 + 
 +With smart-tabs the tab-width doesn't affect the way the code is 
 +aligned which makes it up to the individual developer to choose the 
 +tab-width of his or her taste.
  
 Download [[https://raw.githubusercontent.com/jcsalomon/smarttabs/master/smart-tabs-mode.el|smart-tabs-mode.el]] and [[https://elpa.gnu.org/packages/cl-lib.html|cl-lib]] into the ''.emacs.d/lisp'' folder and add the following lines to the ''.emacs'' file: Download [[https://raw.githubusercontent.com/jcsalomon/smarttabs/master/smart-tabs-mode.el|smart-tabs-mode.el]] and [[https://elpa.gnu.org/packages/cl-lib.html|cl-lib]] into the ''.emacs.d/lisp'' folder and add the following lines to the ''.emacs'' file:
Line 193: Line 215:
 The following naming schemes apply:\\ The following naming schemes apply:\\
  
-//classes//:+//classes and structs//:
 <code c++> <code c++>
 class UpperCaseCamelCase class UpperCaseCamelCase
Line 200: Line 222:
 </code> </code>
  
-//methods//:+//methods and functions//:
 <code c++> <code c++>
 void lowerCaseCamelCase() void lowerCaseCamelCase()
 </code> </code>
  
-//variables//:+//variables and members//:
 <code c++> <code c++>
 int lower_case_snake_case = 42; int lower_case_snake_case = 42;
Line 212: Line 234:
 //defines//: //defines//:
 <code c++> <code c++>
-#define ALL_UPPERCASE_WITH_UNDERSCORES 42+#define SHOUTING_SNAKE_CASE 42
 </code> </code>
  
Line 241: Line 263:
   //! This is a method.   //! This is a method.
   //! \param foo The foo argument is an integer.   //! \param foo The foo argument is an integer.
-  //! \param bar The bar argument is a boolan.+  //! \param bar The bar argument is a boolean.
   //! \return This method returns a float.   //! \return This method returns a float.
   float method(int foo, bool bar);   float method(int foo, bool bar);
dev/dg_code_style_guide.1485542819.txt.gz · Last modified: 2017/01/27 19:46 by deva
Trace:
GNU Free Documentation License 1.3
Valid CSS Driven by DokuWiki Recent changes RSS feed Valid XHTML 1.0