Difference between revisions of "Coding Guidelines/Coding Style"

From The Open Source Backup Wiki (Amanda, MySQL Backup, BackupPC)
Jump to navigationJump to search
 
 
Line 56: Line 56:
 
* Complex condition:
 
* Complex condition:
 
The operator at the end of the line. Use parenthesis to clarify. Indent accordingly to the level.
 
The operator at the end of the line. Use parenthesis to clarify. Indent accordingly to the level.
         if(condition_a &&
+
         if (condition_a &&
          ((condition_b) || (condition_c) ||
+
            ((condition_b) || (condition_c) ||
            (condition_d) &&
+
            (condition_d) &&
          (condition_e)) {
+
            (condition_e)) {
  
  

Latest revision as of 11:20, 26 June 2014

  • Indentation is 4 characters, use <tab>
  • Space after each comma
  • Line should not be more than 80 characters
  • Brace placement for block

The opening brace '{' is at the end of the line, preceded by a space. The closing brace '}' is aligned with the control instruction.

       if (condition_a) {
           instruction_a;
       }

The closing brace '}' is alone on a line except for an else clause or a do..while loop

       if (condition_a) {
           ...
       } else if (condition_b) {
           ...
       } else {
           ...
       }
       do {
           body_of_loop;
       } while (condition_c);

The braces are not needed if there is only one instruction and it fit on one line, otherwise you should write the braces. If the if clause or else clause need the braces, then you should write them for both.

  • Function prototype

On one line if possible, otherwise, align the second line with the first parameter.

       int *my_function(int one, int *two, char *three,
                        char a_really_long_name);

Try to align the function name if you have many prototype:

       int         *my_function(...);
       type_1      *my_function1(...);
       a_long_type  my_function2(...);
       type_2      *my_function3(...);
  • Function declaration

Write a small comment for things that are not obvious, what the function does, the parameters, the return result. Do not over comment.

Return type on the first line. Function name on the second line with the '('. Followed by a line for each parameter. 4 spaces before the type. Align the parameter name, if needed, put the '*' just before the parameter name. opening brace '{' alone on the next line.

       int *
       my_function(
           int    one,
           int   *two,
           char  *three,
           char   a_really_long_name)
       {
       }
  • Function call

On one line if possible, otherwise, align the second line with the first parameter.

       my_function5(one, two, three, a_really_long_name,
                    another_really_long_name);
  • Complex condition:

The operator at the end of the line. Use parenthesis to clarify. Indent accordingly to the level.

       if (condition_a &&
           ((condition_b) || (condition_c) ||
            (condition_d) &&
           (condition_e)) {


  • Write only one instruction by line.

Don't write an instruction on the same line than the control instruction:

  BAD : if (condition_a) return;
  GOOD: if (condition_a)
            return;