Changes between Initial Version and Version 1 of Coding_Conventions


Ignore:
Timestamp:
Feb 23, 2008, 4:18:58 AM (16 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Coding_Conventions

    v1 v1  
     1 * by Dave Loeser
     2 * Version 1. 12
     3 * 10 February 2004
     4
     5= Objective =
     6
     7The goal of this document is to provide a standardized coding methodology for the 0 A.D. programming team. With but a few guidelines such as Layout, Commenting and Naming conventions the team should feel as if they are reading their own code when reading someone else�s code.
     8
     9== Forewarning ==
     10
     11Since this document was created after the start of preliminary coding, there may be source code that does not follow the guidelines as dictated by this document.
     12
     13The team will, as time permits, make an effort to update those sources.
     14
     15= Layout =
     16
     17== Formatting ==
     18
     19Most editors allow for the conversion of tabs to spaces and most people prefer the use of tabs versus wearing the space-bar out.  The size of the tabs is up to each programmer � just ensure that you are using tabs.
     20
     21Limit the length of a line of code to not more than 80 characters; not everyone has a 1600x1200 resolution.  Functions that have many parameters and extend over 80 characters should be written:
     22
     23{{{
     24  SomeFunction(
     25      HWND hWnd_,
     26      BITMAP bmDeviceBitmap_,
     27      long lAnimationFrame_);
     28}}}
     29
     30as opposed to
     31
     32{{{
     33  SomeFunction(HWND hWnd_,
     34                BITMAP bmDeviceBitmap_,
     35                long lAnimationFrame_);
     36}}}
     37
     38Although the second method is commonly used, it is more difficult to maintain if the name of the function changed; you would need to re-align the parameters.
     39
     40== Brackets ==
     41
     42Brackets should be aligned, here�s an example of valid bracket alignment:
     43
     44=== Good Bracketing ===
     45
     46{{{
     47  void CGameObject::CleanUp()
     48  {
     49      if(NULL != m_ThisObject)
     50      {
     51          delete m_ThisObject;
     52      }
     53  }
     54}}}
     55
     56=== Bad Bracketing ===
     57
     58{{{
     59  void CGameObject::CleanUp(){
     60      if(NULL != m_ThisObject){
     61          delete m_ThisObject;
     62      }
     63  }
     64}}}
     65
     66We�re not out to save vertical lines on the screen; it�s about being able to read the code; reading code is a visual experience if not visceral.
     67
     68= Commenting and Documentation =
     69
     70== Commenting ==
     71
     72Commenting is a subject that is sure to cause debate. I prefer minimum comments and maximum expressiveness in the code.  Examples of what is considered good and bad commenting style are shown below.
     73
     74=== Bad Commenting Style ===
     75
     76{{{
     77  void CGameObject::SetModifiedFlag(bool flag)
     78  {
     79      // set the modified flag
     80     
     81      m_bModifiedFlag = flag;
     82  }
     83}}}
     84
     85The above comments do not tell us anything that we don�t already know from reading the code, here�s a better approach.
     86
     87=== Good Commenting Style ===
     88
     89{{{
     90  void CGameObject::SetModifiedFlag(bool flag)
     91  {
     92       // This sets the GameObjects modified
     93       // flag, which is used to determine
     94       // if this object needs to be serialized.
     95       
     96       m_bModifiedFlag = flag;
     97  }
     98}}}
     99
     100== Documentation ==
     101
     102Each programmer is responsible for properly documenting their code.  During code review the code reviewer will ensure that interfaces or API�s are properly documented.
     103
     104The suggested method of documenting a Class is:
     105
     106{{{
     107  /*                                                                                                   
     108        CLASS           : CCivilian                                                             
     109        AUTHOR          : Dave Loeser - daklozar@insightbb.com                                 
     110        DESCRIPTION     : An object that represents a civilian entity.                         
     111                                                                                                       
     112        NOTES           : Notes regarding usage and possible problems etc...                           
     113  */                                                                                                     
     114}}}
     115
     116Each method of a class should be documented as well and here is the suggested method of documenting a member function (continuing with CExample):
     117
     118{{{
     119  class CExample
     120  {
     121      public:
     122             CExample();
     123             ~CExample():
     124 
     125             // ShowExample()
     126             // This function does nothing, but is a good example of documenting
     127             // a member function.
     128             void ShowExample();
     129 
     130      private:
     131             U8     m_ExampleData;       // Holds the value of this example.
     132             U8     m_LongVariableName;  // Shows the lining up of comments
     133  };
     134}}}
     135
     136Essentially, the CTOR and DTOR need not be commented � everyone knows what they are and what they do.  !ShowExample(), on the other hand, provides a brief comment as to its purpose. You may also want to provide an example of a methods usage.  Member data is commented on the right side and it is generally a good ideal (when possible) to line up comments for easier reading.
     137
     138= Naming and Coding Conventions =
     139
     140Naming and coding conventions cover two main areas:
     141
     142 * Files and directory structures
     143 * Variables, functions and classes
     144
     145== Namespaces ==
     146
     147Namespaces are used as a mechanism to express logical grouping.
     148
     149== Global Scope Functions ==
     150
     151A function that is belongs to the global namespace should be prefixed with ::.
     152
     153''Example:'' The Win32 function '''::!OutputDebugString()''' belongs to the global namespace and is written with the scope operator preceding the function name.
     154
     155== Variable Naming ==
     156
     157Variables should use concise, descriptive names that provide innate clues as to the data that the variable represents.
     158
     159== Function Naming ==
     160
     161Functions should use concise, descriptive names that provide innate clues as to the functionality they provide.
     162
     163''Example:'' A function named '''!SetModifiedFlag()''' is preferred over '''!SetFlag()'''.
     164
     165Global and member functions should be named using ''Inclusive capitalisation.'' Inclusive capitalisation is a method that visually differentiates the words within a function name.
     166
     167''Example:'' A function named '''!SetModifiedFlag()''' is preferred over '''setModifiedFlag()''' or '''setmodifiedflag()'''.
     168
     169== Class Naming ==
     170
     171Classes should use concise, descriptive names that easily covey their use. Classes should be named using ''Inclusive capitalisation''. Inclusive capitalisation is a method that visually differentiates the words within a class name.
     172
     173''Example:'' Every game has objects and '''CGameObject''' is more descriptive than '''cGObj''' or '''CGobj'''.
     174 
     175== Header Files ==
     176
     177Here is a sample header file layout.
     178
     179{{{
     180  /*
     181 
     182         CExample interface file.
     183 
     184         AUTHOR        : Joe Coder � joecoder@email.com
     185         MODIFIED BY   : Coder Johnson - coder@email.com
     186 
     187         OVERVIEW      : This interface is difficult to write as it really
     188                         pertains to nothing and serves no purpose other than to
     189                         suggest a documentation scheme.
     190  */
     191 
     192  #ifndef __CEXAMPLE_H__
     193  #define __CEXAMPLE_H__
     194 
     195  //
     196  // INCLUDES and COMPILER DIRECTIVES
     197  //
     198  #include �utils.h�
     199 
     200  //
     201  // DECLARATIONS
     202 
     203  /*
     204         CLASS         : CExample
     205         AUTHOR        : Coder Johnson - coder@email.com
     206         DESCRIPTION   : The CExample class serves no purpose other than to
     207                         provide an example of documenting a class.
     208         NOTES         : Notes regarding usage and possible problems etc...
     209  */
     210 
     211  class CExample
     212  {
     213       public:
     214 
     215            CExample();
     216            ~CExample():
     217 
     218            // ShowExample()
     219            // This function does nothing, but is a good example of documenting
     220            // a member function.
     221            void ShowExample();
     222 
     223       protected:
     224       
     225       private:
     226 
     227            U8     m_ExampleData;       // Holds the value of this example.
     228            U8     m_LongVariableName;  // Shows the lining up of comments
     229  };
     230 
     231  #endif // __CEXAMPLE_H__
     232}}}
     233
     234From the above we can see that header guards are utilized. Header file comment blocks have been shortened to show the name of the module this file belongs to, the authors name and email address and a short overview.
     235
     236The order of declarations is public followed by protected and finally by private. See [http://code.wildfiregames.com/resources/development/CCivilian.h.html CCivilian.h] for an example.
     237
     238= Standard Template Library =
     239
     240We will make use of the Standard Template Library (STL). Although we may be capable of coding list, maps and queues ourselves and do so more efficiently. Our goal is to create a game not to recreate an existing library.
     241
     242Having said that I prefer to wrap up specific STL object in an effort to make the code more readable � see [http://code.wildfiregames.com/resources/development/CCivilian.h.html CCivilian.h] for an example.
     243
     244= Global variables or Singletons =
     245
     246Much debate regarding the use of global variables has been generated over the years so I will not re-enter that discussion. I will say that the Singleton design pattern does provide many benefits over that of a pure global variable.
     247
     248We will make use of the Automatic Singleton Utility as described by Scott Bilas in article 1.3 of the �Game Programming Gems�, volume I, �An Automatic Singleton Utility.�
     249
     250= Strings =
     251
     252A string class has been written, CStr, that should be used instead of directly using std::string or using C-style strings (i.e. char*).