Coding style

From OpenFLUID

Jump to: navigation, search

The coding style conventions are often designed for a specific language. The following recommendations can mainly be applied to C++, but can be applied to other language (Java, C, ...)

The coding style can be automatically used in Eclipse IDE through the coding style configuration (Window > Preferences, C/C++ > Coding style). You can automatically set up this configuration using this Eclipse coding style configuration file (Right-click on link, Save As...)


Contents

Naming conventions

Files

The files should have understandable and short names.
When a file is a declaration or a definition of a class, the file name should refer to the class name. For example, the file declaring the PluggableFunction should be named PlugFunction.h
The main file of a program should be named main.xxx (when possible)


Include guard

The include guard for a .h file begins and ends with a double underscore '__', is the file name in upper-case, and the dot '.' characters are replaced by an underscore.
For example, the include guard of the file PlugFunction.h is __PLUGFUNCTION_H__

#ifndef  __PLUGFUNCTION_H__
#define  __PLUGFUNCTION_H__
 
// place the source code here
 
#endif

Identifiers

identifier rule example
Local variable use PascalCase MyVariable
Global variable begins with a lower case 'g', followed by a '_' (underscore), followed by the name using PascalCase g_Variable
Variable in a macro begins with a '_' (underscore), followed by an upper case 'M', followed by a '_' (underscore), followed by the name of the variable _M_Variable
Constant value use UPPERCASE, every sub-part of the name are separated by a '_' (underscore) MY_CONSTANT
Type name use PascalCase, followed by a '_' (underscore), followed by a 't' Type_t
Function name use camelCase, preferably begins with a verb in lower case doThisJob(...)
Parameter use PascalCase TheParameter
Class name use PascalCase ClassName
Class attribute (private) begins with a lowercase 'm', followed by a '_' (underscore), followed by the name in PascalCase m_Member
Class pointer attribute (private) begins with a lowercase 'mp', followed by a '_' (underscore), followed by the name in PascalCase mp_PtrMember
Class method (public or private) use camelCase, preferably begins with a verb in lower case doThisJob(...)


The formats:

  • UPPERCASE: use only uppercase letters
  • lowercase: use only lowercase letters
  • PascalCase: every sub-part begins with an uppercase letter, everything else in lowercase
  • camelCase: every sub-part begins with an uppercase letter except the first subpart, everything else in lowercase


class Vehicle
{
  private: 
    std::string m_Brand;
    std::string m_Model;
    std::string m_Color;
 
  public:
    Vehicle(std::string Brand, std::string Model, std::string Color);
 
    ~Vehicle() { };   
 
    std::string getBrand() { return m_Brand; };
 
    std::string getModel() { return m_Model; };
 
    std::string getColor();
};
 
 
Vehicle::Vehicle(std::string Brand, std::string Model, std::string Color)
{
  m_Brand = Brand;
  m_Model = Model;
  m_Color = Color;
}
 
 
std::string Vehicle::std::string getColor()
{
  return m_Color;
}


Code appearance

Language

The spoken language used in the source code (identifiers, comments, ...) should be english. This will ensure that the source code is easy to read and maintain by people from different countries.


Indentation and statement blocks

The indentation is done using two spaces ' '. The use of tabs should be avoided.
The blocks of statements should be indented (functions, methods, loops, conditions, ...)

In curly bracket programming languages (such as c++), the opening and closing brackets should be alone on a line. The indentation will begin just after the opening bracket and stop just before the closing bracket.

int a;
int i;
 
a = 18;
 
for (i=0; i<a; i++)
{
  if (i % 2 == 0)
  {
    printf("%d is an even number",i);  
  }
}


Line length

For better reading and printing, each line of text in your code should be at most 80 characters long.

VarA = MyFunction(Param1, Param2,  
                  Param3, Param4, 
                  Param5);
 
VarB = VarB * 2;


White spaces and white lines

In order to write a more readable code, you are encouraged to use horizontal and vertical whitespaces, and "group" lines of code by object. For example, you should group all declarations of variables first, then initialization and assignments, then processing.

int a;   // declarations
int i;
 
a = 18;  // assignments
 
for (i=0; i<a; i++)  // processing
{
  printf(i*i);
}
Personal tools