CODING_STYLE.md 2.69 KB
Newer Older
1

Peter Hutterer's avatar
Peter Hutterer committed
2 3 4
- Indentation in tabs, 8 characters wide, spaces after the tabs where
  vertical alignment is required (see below)

5 6 7
**Note: this file uses spaces due to markdown rendering issues for tabs.
  Code must be implemented using tabs.**

Peter Hutterer's avatar
Peter Hutterer committed
8 9 10 11
- Max line width 80ch, do not break up printed strings though

- Break up long lines at logical groupings, one line for each logical group

12 13 14
```c
int a = somelongname() +
        someotherlongname();
Peter Hutterer's avatar
Peter Hutterer committed
15

16 17 18
if (a < 0 &&
    (b > 20 & d < 10) &&
    d != 0.0)
Peter Hutterer's avatar
Peter Hutterer committed
19 20


21 22 23 24
somelongfunctioncall(arg1,
                     arg2,
                     arg3);
```
Peter Hutterer's avatar
Peter Hutterer committed
25 26 27 28

- Function declarations: return type on separate line, {} on separate line,
  arguments broken up as above.

29 30 31 32
```c
static inline int
foobar(int a, int b)
{
Peter Hutterer's avatar
Peter Hutterer committed
33

34
}
Peter Hutterer's avatar
Peter Hutterer committed
35

36 37 38 39 40 41 42
void
somenamethatiswaytoolong(int a,
                         int b,
                         int c)
{
}
```
Peter Hutterer's avatar
Peter Hutterer committed
43

44
- `/* comments only */`, no `// comments`
Peter Hutterer's avatar
Peter Hutterer committed
45

46
- `variable_name`, not `VariableName` or `variableName`. same for functions.
Peter Hutterer's avatar
Peter Hutterer committed
47 48 49 50 51 52 53 54 55 56

- no typedefs of structs, enums, unions

- if it generates a compiler warning, it needs to be fixed
- if it generates a static checker warning, it needs to be fixed or
  commented

- declare variables at the top, try to keep them as local as possible.
  Exception: if the same variable is re-used in multiple blocks, declare it
  at the top.
57
  Exception: basic loop variables, e.g. for (int i = 0; ...)
Peter Hutterer's avatar
Peter Hutterer committed
58

59 60 61
```c
int a;
int c;
Peter Hutterer's avatar
Peter Hutterer committed
62

63 64
if (foo) {
        int b;
Peter Hutterer's avatar
Peter Hutterer committed
65

66 67 68
        c = get_value();
        usevalue(c);
}
Peter Hutterer's avatar
Peter Hutterer committed
69

70 71 72 73 74
if (bar) {
        c = get_value();
        useit(c);
}
```
Peter Hutterer's avatar
Peter Hutterer committed
75

76 77 78 79
- do not mix function invocations and variable definitions.

  wrong:

80 81 82 83 84 85
```c
{
        int a = foo();
        int b = 7;
}
```
86 87

  right:
88 89 90 91
```c
{
        int a;
        int b = 7;
92

93 94 95
        a = foo();
}
```
96

97 98
  There are exceptions here, e.g. `tp_libinput_context()`,
  `litest_current_device()`
99

100 101 102 103
- if/else: { on the same line, no curly braces if both blocks are a single
  statement. If either if or else block are multiple statements, both must
  have curly braces.

104 105 106 107 108 109 110 111
```c
if (foo) {
        blah();
        bar();
} else {
        a = 10;
}
```
112

113 114
- public functions MUST be doxygen-commented, use doxygen's `@foo` rather than
  `\foo` notation
Peter Hutterer's avatar
Peter Hutterer committed
115

116
- `#include "config.h"` comes first, followed by system headers, followed by
Peter Hutterer's avatar
Peter Hutterer committed
117 118 119
  external library headers, followed by internal headers.
  sort alphabetically where it makes sense (specifically system headers)

120 121
```c
#include "config.h"
Peter Hutterer's avatar
Peter Hutterer committed
122

123 124
#include <stdio.h>
#include <string.h>
Peter Hutterer's avatar
Peter Hutterer committed
125

126
#include <libevdev/libevdev.h>
Peter Hutterer's avatar
Peter Hutterer committed
127

128 129
#include "libinput-private.h"
```
130 131 132 133

- goto jumps only to the end of the function, and only for good reasons
  (usually cleanup). goto never jumps backwards

134
- Use stdbool.h's bool for booleans within the library (instead of `int`).
135
  Exception: the public API uses int, not bool.