Skip to content
gihanmudalige edited this page Dec 10, 2014 · 1 revision

Coding Guidelines

Checking your commit conforms to coding guidelines

Install a Git pre-commit hook to automatically check for tab and whitespace errors before committing. In the root directory of your local Git repository do

mv .git/hooks/pre-commit.sample .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

The repository contains a .gitattributes file, which sets the appropriate level of whitespace checks for each file. The following will be treated as errors:

  • whitespace at the end of a line
  • blank lines at EOF
  • mixing tabs and whitespace in indents

In all files but Makefiles tabs generally are not allowed for indentation.

Please configure your editor to strip trailing whitespace when you save a file.

C/C++

  1. Indentation by spaces only (no tabs) with two spaces per level.

  2. Opening braces on the same line as the statement creating the scope (K&R style, not ANSI):

    routine (args) {
      if ( i>0 ) {
        ...
      }
      for ( i=0; i<N; i++ ) {
        ...
      }
    }
  3. Lines should not exceed 72 characters.

  4. Function return type on same line as remaining function signature.

  5. A space after the function name in a function call/definition and after the keywords for and if. No extra spaces for functions inside the braces enclosing arguments (but spaces between arguments, after the comma). Extra spaces inside the brackets after for and if.

    // no space
    int x = f (par1, par2, par3);
    
    // space
    if ( condition )
    for ( ; ; )
  6. One-line comments introduced with //

  7. No trailing white space.

Clone this wiki locally