Coding Standards / Coding Convention

Coding rules written as we go

Code formatting (and adjacent Sublime Text settings)

  • Line ending is LF (on all platforms, including Windows); Set your editor to read LF correctly and interpret it as new line; Save only files that end each line with LF and not CRLF:
    {"default_line_ending": "unix"}
  • Ensure you have a new line (empty line) before the end of file: {"ensure_newline_at_eof_on_save": true}
  • Indentation is 4 spaces, always use spaces instead of tab (no new tab character allowed in files); In maintenance projects, this is important especially for newly introduced lines;
    {"tab_size": 4, "detect_indentation": false, "translate_tabs_to_spaces": true}
  • Depending on the project:
    • new projects: convert all spaces to tabs 
    • maintenance projects: insert spaces instead of tabs only in new code
      For this purpose you may find this setting useful
      {"draw_white_space": "all"}
  • No trailing spaces at the end of the line before the last printable character (between this one and LF)
    {"trim_trailing_white_space_on_save": true}
  • Do not wrap lines prematurely at 80 columns hard stop, please use a hard stop at 120 lines and a soft limit at 80 columns. This setting may help you:
    {"rulers": [80, 120]}

Rule #1: Braces

Braces ({ }) shall always surround the blocks of code (a.k.a., compound statements) following if, else, switch, while, do, and for keywords. Single statements and empty statements following these keywords shall also always be surrounded by braces.

Reasoning: There is considerable risk associated with the presence of empty statements and single statements that are not surrounded by braces. Code constructs of this type are often associated with bugs when nearby code is changed or commented out. This type of bug is entirely eliminated by the consistent use of braces.

MISRA-C 2004 Rule 14.8 (required): The statement forming the body of a switch, while, do … while or for statement shall be a compound statement.  

The statement that forms the body of a switch statement or a while, do … while or for loop, shall be a compound statement (enclosed within braces), even if that compound statement contains a single statement.


Rule #2: Spaces

a. Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls. In other words, a keyword followed by a parenthesis should be separated by a space but a blank space should not be used between a method name and its opening parenthesis.

b. A blank space should appear after commas in argument lists. All binary operators except . should be separated from their operands by spaces.  Blank spaces should never separate unary operators such as unary minus, increment ("++"), and decrement ("--") from their operands.

c. The expressions in a for statement should be separated by blank spaces

d. Casts should be followed by a blank space.

References:  http://caxapa.ru/thumbs/468328/misra-c-2004.pdf

# Prefer named functions over variables with lambda values

Rule N: Use parentheses around boolean expressions

When using assigning a boolean expression to a variable always enclose the right value into a pair of parenthesis. 

This is very important in PHP for example, because if someone replaces && with and, the precedence of the assignment operator will change.

PHP Rule N+1: Avoid using "and"/"or" operators.

In PHP, the "and" operator is not simply an alias of &&; when you use and instead of &&, the precedence of the assignment operator will change. For this reason we advice against using these operator s whenever possible.

Niciun comentariu:

Trimiteți un comentariu