Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Coding Style

AndreLDM

Coding Style

Filenames

  • Filename extension should be .cpp, .c or *.h.
  • Filename should be all lowercase and can include underscores (_), but never spaces.

Header File

  • All header files should have #define guards to prevent multiple inclusion. The format of the symbol name should be <FILENAME>_H_.
  • Use standard order for readability and to avoid hidden dependencies: C library, C++ library, other libraries' .h, the project's .h.
  • Remove all unnecessary includes from the header and move what possible to the source file.

Scoping

  • Prefer nonmember functions within a namespace or static member functions to global functions.
  • Place a function's variables in the narrowest scope possible, and initialize variables in the declaration.

Classes

  • Classes names should be in singular and camel case, ex. FooClass, EventNext.
  • Avoid doing complex initialization in constructors (in particular, initialization that can fail or that requires virtual method calls).
  • Use a struct only for passive objects that carry data; everything else is a class.
  • Avoid the use of friend classes.
  • Avoid the use of Multiple Inheritance.
  • Make member variables private and prefixed with "m_", ex. m_my_variable_name.
  • Provide access to member variables through accessor methods as needed, getMyVariableName and setMyVariableName.

Interfaces

  • Interfaces names should be prefixed with 'I', ex. IDao.

Comments

  • Use either the // or / / syntax. Try to describe things consistently and briefly.

Formatting

  • All files should be UTF-8 encoded.
  • Avoid non-ASCII characters.
  • Use spaces instead of tabs, and indent 4 spaces at a time.
  • There's no line length limit, but try keep it smaller than 100 characters long.
  • Pointers declarations should follow: Type* foo.
  • Function calls should be on one line if it fits, otherwise, wrap arguments at the parenthesis:

    smallFunction(arg1);
    longFunction(argument1, argument2 + argument3, x,
                 y, argument4);
    
  • Conditionals should follow this pattern:

    if(condition1 && (x + y > z)) {
        do_something();
    } else if(condition2) {
        do_something_else();
    } else {
        do_whatever();
    }
    

Misc

  • Avoid C++11 usage in order to keep the compatibility and portability.
  • Be very cautious with macros. Prefer enums and const variables to macros.
  • Do not use C++ exceptions.
  • Use inner classes and inner enums, do not pollute global namespace.
  • Don't use global variables and singletons. Use only global constants.
  • All identifiers, comments and string literals should be written in English and should be syntactically and grammatically correct.

Sources:
Google C++ Style Guide
CppEvents Wiki


Related

Wiki: Home