Coding Style

Basic Rules

  1. indentation

    • use 4 spaces

      if (condition) {
          statements
      }
      else {
          statements
      }
      
  2. breaking long lines and strings (not a must)

    • keep lines within 80 characters

  3. placing braces and spaces

    • for all non-function statement blocks, put the opening brace last on the line, and put the closing brace first

      if (condition) {
          statements
      }
      else {
          statements
      }
      
    • for functions, put the opening brace at the beginning of the next line

      int function(int variable)
      {
          statements;
      }
      
    • for single statements, omit braces

      if (condition)
          one statement;
      else
          one statement;
      
    • but for the following cases, still keep them

      if (condition) {
          one statement;
          one statement;
      }
      else {
          one statement;
      }
      
      if (condition) {
         if (condition) {
            if (condition)
               statement
         }
      }
      
  4. spaces

    • use a space after the following keywords

      if, switch, case, for, do, while
      
    • but not with the following keywords

      sizeof, typeof, alignof, __attribute__
      
    • do not add spaces around parenthesized expressions

      if (condition)
      
    • adjacent * to the data name or function name

      int *variable;
      int *function(int variable);
      
    • use one space on each side of the following operators
      (where, &, *, … are used as binary or ternary operators)
      =, +, -, <, >, *, /, %, |, &, ^, <=, >=, ==, !=, ?, :
      
    • do not use space after the following operators
      (where, &, *, … are used as unary operators)
      &, *, +, -, ~, !, sizeof, typeof, alignof, __attribute__, defined
      
    • do not use space before the postfix increment or decrement unary operators

      ++, --
      
    • do not use space after the prefix increment or decrement unary operators

      ++, --
      
    • do not use space around member operators

      ., ->
      
    • do not leave trailing whitespace at the ends of lines

  5. naming

    • use the “small camel case” for variables or functions

      int datCst
      int getDatCst()
      
    • use the “big camel case” or small camel case with postfix _t for type

      Cst* datCst
      cst_t* datCst    // I prefer this one
      
    • use the “big camel case” or capitalization for class

      class Add
      class ADD    // I prefer this one
      
    • use the “big camel case” or capitalization for definitions or enums

      #define MACRO    value
      enums class enmCst_t{
         SAD = 0,
         SATD
      }
      
    • use the following prefix or postfix to indicate variable types:

      m_, class member
      s_, static class member
      g_, global variables
      b, boolean variables (could be omitted for names like flgSplit)
      _t, defined types
      

Supplemental Rules

The following rules are collected through code review.

  1. never use tab as indentation
    (they have already been mentioned in “Basic Rules”)
  2. pay attention to the use of space
    (they have already been mentioned in “Basic Rules”)
    • wrong

      int * dat
      & dat
      if(...){
      
    • right

      int *dat
      &dat
      if (...) {
      
  3. definition must be atomic

    • wrong:

      #define C    A + B
      
    • right:

      #define C    (A + B)
      
  4. use magic numbers as less as possible