Radek Krejci | 71f2029 | 2015-06-11 11:55:55 +0200 | [diff] [blame] | 1 | # libyang Coding Style |
| 2 | |
| 3 | This file describes the coding style used in most C files in the libyang |
Radek Krejci | fb11189 | 2015-06-16 10:33:52 +0200 | [diff] [blame] | 4 | library. |
Radek Krejci | 71f2029 | 2015-06-11 11:55:55 +0200 | [diff] [blame] | 5 | |
| 6 | ## Basics |
| 7 | |
| 8 | - Use space instead of tabs for indentations. |
| 9 | |
| 10 | - There is no strict limit for the line length, However, try to keep lines in a |
Radek Krejci | fb11189 | 2015-06-16 10:33:52 +0200 | [diff] [blame] | 11 | reasonable length (120 characters). |
Radek Krejci | 71f2029 | 2015-06-11 11:55:55 +0200 | [diff] [blame] | 12 | |
| 13 | - Avoid trailing spaces on lines. |
| 14 | |
| 15 | - Put one blank line between function definitions. |
| 16 | |
| 17 | - Don't mix declarations and code within a block. Similarly, don't use |
| 18 | declarations in iteration statements. |
| 19 | |
| 20 | ## Naming |
| 21 | |
| 22 | Use underscores to separate words in an identifier: `multi_word_name`. |
| 23 | |
| 24 | Use lowercase for most names. Use uppercase for macros, macro parameters and |
| 25 | members of enumerations. |
| 26 | |
| 27 | Do not use names that begin with `_`. If you need a name for "internal use |
| 28 | only", use `__` as a suffix instead of a prefix. |
| 29 | |
| 30 | ## Comments |
| 31 | |
| 32 | Avoid `//` comments. Use `/* ... */` comments, write block comments with the |
| 33 | leading asterisk on each line. You may put the `/*` and `*/` on the same line as |
| 34 | comment text if you prefer. |
| 35 | |
| 36 | ```c |
| 37 | /* |
| 38 | * comment text |
| 39 | */ |
| 40 | ``` |
| 41 | |
| 42 | ## Functions |
| 43 | |
| 44 | Put the return type, function name, and the braces that surround the function's |
| 45 | code on separate lines, all starting in column 0. |
| 46 | |
| 47 | ```c |
| 48 | static int |
| 49 | foo(int arg) |
| 50 | { |
| 51 | ... |
| 52 | } |
| 53 | ``` |
| 54 | |
| 55 | When you need to put the function parameters on multiple lines, start new line |
| 56 | at column after the opening parenthesis from the initial line. |
| 57 | |
| 58 | ```c |
| 59 | static int |
| 60 | my_function(struct my_struct *p1, struct another_struct *p2, |
| 61 | int size) |
| 62 | { |
| 63 | ... |
| 64 | } |
| 65 | ``` |
| 66 | |
| 67 | In the absence of good reasons for another order, the following parameter order |
| 68 | is preferred. One notable exception is that data parameters and their |
| 69 | corresponding size parameters should be paired. |
| 70 | |
| 71 | 1. The primary object being manipulated, if any (equivalent to the "this" |
| 72 | pointer in C++). |
| 73 | 2. Input-only parameters. |
| 74 | 3. Input/output parameters. |
| 75 | 4. Output-only parameters. |
| 76 | 5. Status parameter. |
| 77 | |
| 78 | Functions that destroy an instance of a dynamically-allocated type should accept |
| 79 | and ignore a null pointer argument. Code that calls such a function (including |
| 80 | the C standard library function `free()`) should omit a null-pointer check. We |
| 81 | find that this usually makes code easier to read. |
| 82 | |
| 83 | ### Function Prototypes |
| 84 | |
| 85 | Put the return type and function name on the same line in a function prototype: |
| 86 | |
| 87 | ```c |
| 88 | static const struct int foo(int arg); |
| 89 | ``` |
| 90 | |
| 91 | ## Statements |
| 92 | |
| 93 | - Indent each level of code with 4 spaces. |
| 94 | - Put single space between `if`, `while`, `for`, etc. statements and the |
| 95 | expression that follow them. On the other hand, function calls has no space |
| 96 | between the function name and opening parenthesis. |
| 97 | - Opening code block brace is kept at the same line with the `if`, `while`, |
| 98 | `for` or `switch` statements. |
| 99 | |
| 100 | ```c |
| 101 | if (a) { |
| 102 | x = exp(a); |
| 103 | } else { |
| 104 | return 1; |
| 105 | } |
| 106 | ``` |
| 107 | |
| 108 | - Start switch's cases at the same column as the switch. |
| 109 | |
| 110 | ```c |
| 111 | switch (conn->state) { |
| 112 | case 0: |
| 113 | return "data found"; |
| 114 | case 1: |
| 115 | return "data not found"; |
| 116 | default: |
| 117 | return "unknown error"; |
| 118 | } |
| 119 | ``` |
| 120 | |
| 121 | - Do not put gratuitous parentheses around the expression in a return statement, |
| 122 | that is, write `return 0;` and not `return(0);` |
| 123 | |
| 124 | ## Types |
| 125 | |
| 126 | Use typedefs sparingly. Code is clearer if the actual type is visible at the |
| 127 | point of declaration. Do not, in general, declare a typedef for a struct, union, |
| 128 | or enum. Do not declare a typedef for a pointer type, because this can be very |
| 129 | confusing to the reader. |
| 130 | |
| 131 | Use the `int<N>_t` and `uint<N>_t` types from `<stdint.h>` for exact-width |
| 132 | integer types. Use the `PRId<N>`, `PRIu<N>`, and `PRIx<N>` macros from |
| 133 | `<inttypes.h>` for formatting them with `printf()` and related functions. |
| 134 | |
| 135 | Pointer declarators bind to the variable name, not the type name. Write |
| 136 | `int *x`, not `int* x` and definitely not `int * x`. |
| 137 | |
| 138 | ## Expresions |
| 139 | |
| 140 | Put one space on each side of infix binary and ternary operators: |
| 141 | |
| 142 | ```c |
| 143 | * / % + - << >> < <= > >= == != & ^ | && || ?: = += -= *= /= %= &= ^= |= <<= >>= |
| 144 | ``` |
| 145 | |
| 146 | Do not put any white space around postfix, prefix, or grouping operators with |
| 147 | one exception - `sizeof`, see the note below. |
| 148 | |
| 149 | ```c |
| 150 | () [] -> . ! ~ ++ -- + - * & |
| 151 | ``` |
| 152 | |
| 153 | The "sizeof" operator is unique among C operators in that it accepts two very |
| 154 | different kinds of operands: an expression or a type. In general, prefer to |
| 155 | specify an expression |
| 156 | ```c |
| 157 | int *x = calloc(1, sizeof *x); |
| 158 | ``` |
| 159 | When the operand of sizeof is an expression, there is no need to parenthesize |
| 160 | that operand, and please don't. There is an exception to this rule when you need |
| 161 | to work with partially compatible structures: |
| 162 | |
| 163 | ```c |
| 164 | struct a_s { |
| 165 | uint8_t type; |
| 166 | } |
| 167 | |
| 168 | struct b_s { |
| 169 | uint8_t type; |
| 170 | char *str; |
| 171 | } |
| 172 | |
| 173 | struct c_s { |
| 174 | uint8_t type; |
| 175 | uint8_t *u8; |
| 176 | } |
| 177 | ... |
| 178 | struct a_s *a; |
| 179 | |
| 180 | switch (type) { |
| 181 | case 1: |
| 182 | a = (struct a_s *)calloc(1, sizeof(struct b_s)); |
| 183 | break; |
| 184 | case 2: |
| 185 | a = (struct a_s *)calloc(1, sizeof(struct c_s)); |
| 186 | break; |
| 187 | ... |
| 188 | ``` |