File: CodeStyle.md

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (292 lines) | stat: -rw-r--r-- 7,051 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# The UCX code style

## Style
  * 4 spaces, no tabs
  * Up to 80 columns
  * Single space around operators
  * No spaces in the end-of-line
  * Indent function arguments on column
  * Indent structure fields on column
  * Scope: open on same line, except function body, which is on a new line.
  * Indent multiple consecutive assignments on the column
  * 2 space lines between types and prototypes (header files)
  * 1 space line between functions (source files) 


## Naming convention:
  * Lower case, underscores
  * Names must begin with ucp_/uct_/ucs_/ucm_
  * Macro names must begin with UCP_/UCT_/UCS_/UCM_
  * An output argument which is a pointer to a user variable has _p suffix
  * Value types (e.g struct types, integer types) have _t suffix
  * Pointer to structs, which are used as API handles, have _h suffix
  * Macro arguments begin with _ (e.g _value) to avoid confusion with variables
  * No leading underscores in function names

### Header file name suffixes:
  * _fwd.h   for a files with a types/function forward declarations
  * _types.h if contains a type declarations
  * .inl     for inline functions
  * _def.h   with a preprocessor macros


## C++
  * Used only for unit testing
  * Lower-case class names (same as stl/boost)
  * The unit tests in test/gtest are written using [C++11](https://en.cppreference.com/w/cpp/11). Whenever applicable, the usage of advanced language features is allowed and preferred over legacy code. For example:
    * Prefer references over pointers
    * `auto` for type deduction 
    * Use [move semantics](https://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html) where applicable
    * `constexpr` for compile-time values, instead of `const` or `#define`
    * `using` [instead of](https://en.cppreference.com/w/cpp/language/type_alias) `typedef`
    * [List initialization](https://en.cppreference.com/w/cpp/language/list_initialization)
    * [Range-based](https://en.cppreference.com/w/cpp/language/range-for) `for` loop
 

## Include order:
   1. config.h
   2. specific internal header
   3. UCX headers
   4. system headers


## Doxygen
  * All interface H/C files should have doxygen documentation.
 

## Error handling
  * All internal error codes must be `ucs_status_t`.
  * Using `status` for `ucs_status_t` and `ret` for `int` is preferred.
  * A function which returns error should print a log message.
  * The function which prints the log message is the first one which decides which
    error it is. If a functions returns an error because it's callee returned 
    erroneous `ucs_status_t`, it does not have to print a log message.
  * Destructors are not able to propagate error code to the caller because they
    return void. also, users are not ready to handle errors during cleanup flow.
    therefore a destructor should handle an error by printing a warning or an
    error message.


## Testing
  * Every major feature or bugfix must be accompanied with a unit test. In case
    of a fix, the test should fail without the fix.


## Miscellaneous examples

### Boolean expression

Use explicit checks with added parenthesis like below.

Good
```C
    if (ptr == NULL) {

    if (a == 0) {

    if ((ret == UCS_KH_PUT_BUCKET_EMPTY) ||
        (ret == UCS_KH_PUT_BUCKET_CLEAR)) {
```

### Variable definition

Variables defined with a value should be first. Add a blank line after
definitions.

```C
void function(int value)
{
    int a     = 1;
    void *ptr = &value;
    int b;
    ucs_status_t status;

    a += value;
```

### if style

Use blank line after if/else blocks
```C
    if (value > 0) {
        value = 23;
    } else {
        value = 12;
    }

    value *= 2;
```

Check return values close to functions

Good
```C
    if (val != XXX) {
        ret = f1();
        if (ret < 0) {
        }
        ...
    } else {
        ret = f2();
        if (ret < 0) {
        }
        ...
    }
```

Bad
```C
    if (val != XXX) {
        ret = f1();
        ...
    } else {
        ret = f2();
        ...
    }

    if (ret < 0) {

    }
```

General formatting rules

Good
```C
    if (val != XXX) {
        /* snip */
    } else if (val == YYY) {
        /* code here */
    } else {
        /* code here */
    }
```

Bad
```C
    if(val != XXX) {   /* Require space after if */
    if (val != XXX){   /* Require space after )  */
    if ( val != XXX) { /* Remove space after (   */
```

### goto style

Labels should be named preferably `out`, `err` or with prefix `err_`/`out_`.

Good
```C
    return;

err_free:
    ucs_free(thread);
err:
    --ucs_async_thread_global_context.use_count;
out_unlock:
    ucs_assert_always(ucs_async_thread_global_context.thread != NULL);
    *thread_p = ucs_async_thread_global_context.thread;
```

Bad
```C
    return;
/*    !!!Add blank line!!!      */
err_free:
    ucs_free(thread);
/*    !!!Remove this line!!!    */
err:
    --ucs_async_thread_global_context.use_count;
```

### structure assignment

Good

```C
    event.events   = events;
    event.data.fd  = event_fd;
    event.data.ptr = udata;

```

Bad
```C
    /* Align = position */
    event.events = events;
    event.data.fd = event_fd;
    event.data.ptr = udata;
```

### comment in C file

Good
```C
/* run-time CPU detection */
```

Bad: require C style `/* .. */` comment.

```C
// run-time CPU detection
```

### no spaces in the end-of-line

Good
```C
    int fd;
```

Bad
```
    int fd;  
        /* ^^ Remove trailing space */
```

### macro definition

Good
```C
    #define UCS_MACRO_SHORT(_obj, _field, _val) \
        (_obj)->_field = (_val)

    #define UCS_MACRO_LONG(_obj, _field1, _field2, _val1, _val2) \
        { \
            ucs_typeof((_obj)->_field1) sum = (_val1) + (_val2); \
            \
            (_obj)->_field1 = sum; \
            (_obj)->_field2 = sum; \
        }

    #define UCS_MACRO_LONG_RET_VAL(_obj, _field, _val, _func) \
        ({ \
            ucs_status_t status; \
            \
            (_obj)->_field = (_val); \
            \
            status = _func(_obj); \
            status; \
        })
```

Bad
```C
    #define UCS_MACRO_SHORT(_obj, _field, _val) \
        _obj->_field = _val /* need to wrap macro arguments by () */

    #define UCS_MACRO_LONG(_obj, _field1, _field2, _val1, _val2) \
            /* possible mixing declarations and code */ \
            typeof((_obj)->_field1) sum = (_val1) + (_val2); \
            \
            (_obj)->_field1 = sum; \
            (_obj)->_field2 = sum;

    #define UCS_MACRO_LONG_RET_VAL(_obj, _field, _val, _func) \
        ({                                                    \
            ucs_status_t status;                              \
                                                              \
            (_obj)->_field = (_val);                          \
                                                              \
            status = _func(_obj);                             \
            status;                                           \
        }) /* wrong alignment of "\" */
```