Version 1 (modified by trac, 16 years ago) ( diff )

--

  • by Dave Loeser
  • Version 1. 12
  • 10 February 2004

Objective

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

Forewarning

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

The team will, as time permits, make an effort to update those sources.

Layout

Formatting

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

Limit 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:

  SomeFunction(
      HWND hWnd_,
      BITMAP bmDeviceBitmap_,
      long lAnimationFrame_);

as opposed to

  SomeFunction(HWND hWnd_,
                BITMAP bmDeviceBitmap_,
                long lAnimationFrame_);

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

Brackets

Brackets should be aligned, here�s an example of valid bracket alignment:

Good Bracketing

  void CGameObject::CleanUp()
  {
      if(NULL != m_ThisObject)
      {
          delete m_ThisObject;
      }
  }

Bad Bracketing

  void CGameObject::CleanUp(){
      if(NULL != m_ThisObject){
          delete m_ThisObject;
      }
  }

We�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.

Commenting and Documentation

Commenting

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

Bad Commenting Style

  void CGameObject::SetModifiedFlag(bool flag)
  {
      // set the modified flag
      
      m_bModifiedFlag = flag;
  }

The above comments do not tell us anything that we don�t already know from reading the code, here�s a better approach.

Good Commenting Style

  void CGameObject::SetModifiedFlag(bool flag)
  {
       // This sets the GameObjects modified
       // flag, which is used to determine
       // if this object needs to be serialized.
       
       m_bModifiedFlag = flag;
  }

Documentation

Each programmer is responsible for properly documenting their code. During code review the code reviewer will ensure that interfaces or API�s are properly documented.

The suggested method of documenting a Class is:

  /*													
  	CLASS		: CCivilian								
  	AUTHOR		: Dave Loeser - daklozar@insightbb.com					
  	DESCRIPTION	: An object that represents a civilian entity.				
 													
  	NOTES		: Notes regarding usage and possible problems etc...				
  */													 

Each method of a class should be documented as well and here is the suggested method of documenting a member function (continuing with CExample):

  class CExample
  {
      public:
             CExample();
             ~CExample():
 
             // ShowExample()
             // This function does nothing, but is a good example of documenting
             // a member function.
             void ShowExample();
 
      private:
             U8     m_ExampleData;       // Holds the value of this example.
             U8     m_LongVariableName;  // Shows the lining up of comments
  };

Essentially, 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.

Naming and Coding Conventions

Naming and coding conventions cover two main areas:

  • Files and directory structures
  • Variables, functions and classes

Namespaces

Namespaces are used as a mechanism to express logical grouping.

Global Scope Functions

A function that is belongs to the global namespace should be prefixed with ::.

Example: The Win32 function ::OutputDebugString() belongs to the global namespace and is written with the scope operator preceding the function name.

Variable Naming

Variables should use concise, descriptive names that provide innate clues as to the data that the variable represents.

Function Naming

Functions should use concise, descriptive names that provide innate clues as to the functionality they provide.

Example: A function named SetModifiedFlag() is preferred over SetFlag().

Global and member functions should be named using Inclusive capitalisation. Inclusive capitalisation is a method that visually differentiates the words within a function name.

Example: A function named SetModifiedFlag() is preferred over setModifiedFlag() or setmodifiedflag().

Class Naming

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

Example: Every game has objects and CGameObject is more descriptive than cGObj or CGobj.

Header Files

Here is a sample header file layout.

  /*
 
         CExample interface file.
 
         AUTHOR        : Joe Coder � joecoder@email.com
         MODIFIED BY   : Coder Johnson - coder@email.com
 
         OVERVIEW      : This interface is difficult to write as it really
                         pertains to nothing and serves no purpose other than to
                         suggest a documentation scheme.
  */
 
  #ifndef __CEXAMPLE_H__
  #define __CEXAMPLE_H__
 
  //
  // INCLUDES and COMPILER DIRECTIVES
  //
  #include �utils.h�
 
  //
  // DECLARATIONS
 
  /*
         CLASS         : CExample
         AUTHOR        : Coder Johnson - coder@email.com
         DESCRIPTION   : The CExample class serves no purpose other than to
                         provide an example of documenting a class.
         NOTES         : Notes regarding usage and possible problems etc...
  */
 
  class CExample
  {
       public:
 
            CExample();
            ~CExample():
 
            // ShowExample()
            // This function does nothing, but is a good example of documenting
            // a member function.
            void ShowExample();
 
       protected:
        
       private: 
 
            U8     m_ExampleData;       // Holds the value of this example.
            U8     m_LongVariableName;  // Shows the lining up of comments
  };
 
  #endif // __CEXAMPLE_H__

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

The order of declarations is public followed by protected and finally by private. See CCivilian.h for an example.

Standard Template Library

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

Having said that I prefer to wrap up specific STL object in an effort to make the code more readable � see CCivilian.h for an example.

Global variables or Singletons

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

We 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.�

Strings

A string class has been written, CStr, that should be used instead of directly using std::string or using C-style strings (i.e. char*).

Note: See TracWiki for help on using the wiki.