Refactor static variables into a structure
[betaflight.git] / docs / development / CodingStyle.md
blob7e915b1387e8d056eaa15aea04c4075131c0e46e
1 #General
2 This document overrides the original Baseflight style that was referenced before.
3 This document has taken inspiration from that style, from Eclipse defaults and from Linux, as well as from some Cleanflight developers and existing code.
5 There are not so many changes from the old style, if you managed to find it.
7 #Formatting style
9 ##Indentation
10 K&R indent style with 4 space indent, NO hard tabs (all tabs replaced by spaces).
12 ##Tool support
13 Any of these tools can get you pretty close:
15 Eclipse built in "K&R" style, after changing the indent to 4 spaces and change Braces after function declarations to Next line.
16 ```
17 astyle --style=kr --indent=spaces=4 --min-conditional-indent=0 --max-instatement-indent=80 --pad-header --pad-oper --align-pointer=name --align-reference=name --max-code-length=120 --convert-tabs --preserve-date --suffix=none --mode=c
18 ```
19 ```
20 indent -kr -i4 -nut
21 ```
22 (the options for these commands can be tuned more to comply even better)
24 Note: These tools are not authorative.
25 Sometimes, for example, you may want other columns and line breaks so it looks like a matrix.
27 Note2: The Astyle settings have been tested and will produce a nice result. Many files will be changed, mostly to the better but maybe not always, so use with care. 
29 ##Curly Braces
30 Functions shall have the opening brace at the beginning of the next line.
32 All non-function statement blocks (if, switch, for) shall have the opening brace last on the same line, with the following statement on the next line.
34 Closing braces shall be but on the line after the last statement in the block.
36 If it is followed by an `else` or `else if` that shall be on the same line, again with the opening brace on the same line.
38 A single statement after an `if` or an `else` may omit the "unnecessary" braces only when ALL conditional branches have single statements AND you have strong reason to know it will always be that way.
40 If in doubt, do not omit such "unnecessary" braces.
41 (Adding a statement to a branch will break the logic if the braces are forgotten and otherwise make the PR longer).
43 ##Spaces
44 Use a space after (most) keywords.  The notable exceptions are sizeof, typeof, alignof, and __attribute__, which look somewhat like functions (and are usually used with parentheses).
45 So use a space after these keywords:
46 ```
47 if, switch, case, for, do, while
48 ```
49 but not with sizeof, typeof, alignof, or __attribute__.  E.g.,
50 ```
51 s = sizeof(struct file);
52 ```
53 When declaring pointer data or a function that returns a pointer type, the preferred use of '*' is adjacent to the data name or function name and not adjacent to the type name.  Examples:
54 ```
55 char *linux_banner;
56 memparse(char *ptr, char **retptr);
57 char *match_strdup(substring_t *s);
58 ```
59 Use one space around (on each side of) most binary and ternary operators, such as any of these:
60 ```
61 =  +  -  <  >  *  /  %  |  &  ^  <=  >=  ==  !=  ?  :
62 ```
63 but no space after unary operators:
64 ```
65 &  *  +  -  ~  !  sizeof  typeof  alignof  __attribute__  defined
66 ```
67 no space before the postfix increment & decrement unary operators:
68 ```
69 ++  --
70 ```
71 no space after the prefix increment & decrement unary operators:
72 ```
73 ++  --
74 ```
75 and no space around the '.' and "->" structure member operators.
77 '*' and '&', when used for pointer and reference, shall have no space between it and the following variable name.
79 #typedef
80 enums with a count should have that count declared as the last item in the enumeration list,
81 so that it is automatically maintained, e.g.:
82 ```
83 typedef enum {
84     PID_CONTROLLER_MW23 = 0,
85     PID_CONTROLLER_MWREWRITE,
86     PID_CONTROLLER_LUX_FLOAT,
87     PID_COUNT
88 } pidControllerType_e;
89 ```
90 It shall not be calculated afterwards, e.g. using PID_CONTROLLER_LUX_FLOAT + 1;
92 typedef struct definitions should include the struct name, so that the type can be forward referenced, that is in:
93 ```
94 typedef struct motorMixer_s {
95     float throttle;
96 ...
97     float yaw;
98 } motorMixer_t;
99 ```
100 the motorMixer_s name is required.
102 #Variables
104 ##Naming
105 Generally, descriptive lowerCamelCase names are preferred for function names, variables, arguments, etc.
106 For configuration variables that are user accessible via CLI or similar, all_lowercase with underscore is preferred.
108 Variable names should be nouns.
110 Simple temporary variables with a very small scope may be short where it aligns with common practice.
111 Such as "i" as a temporary counter in a `for` loop, like `for (int i = 0; i < 4; i++)`.
112 Using "temporaryCounter" in that case would not improve readability.
114 ##Declarations
115 Avoid global variables.
117 Variables should be declared at the top of the smallest scope where the variable is used.
118 Variable re-use should be avoided - use distinct variabes when their use is unrelated.
119 One blank line should follow the declaration(s).
121 Hint: Sometimes you can create a block, i.e. add curly braces, to reduce the scope further.
122 For example to limit variable scope to a single `case` branch.
124 Variables with limited use may be declared at the point of first use. It makes PR-review easier (but that point is lost if the variable is used everywhere anyway).
126 ##Initialisation
127 The pattern with "lazy initialisation" may be advantageous in the Configurator to speed up the start when the initialisation is "expensive" in some way.
128 In the FC, however, it’s always better to use some milliseconds extra before take-off than to use them while flying.
130 So don't use "lazy initialisation".
132 An explicit "init" function, is preferred.
134 ##Data types
135 Be aware of the data types you use and do not trust implicit type casting to be correct.
137 Angles are sometimes represented as degrees in a float. Sometimes as decidegrees in a uint8_t.
138 You have been warned.
140 Avoid implicit double conversions and only use float-argument functions.
142 Check .map file to make sure no conversions sneak in, and use -Wdouble-promotion warning for the compiler
144 Instead of sin() and cos(), use sin_approx() and cos_approx() from common/math.h.
146 Float constants should be defined with "f" suffix, like 1.0f and 3.1415926f, otherwise double conversion might occur.
148 #Functions
150 ##Naming
151 Methods that return a boolean should be named as a question, and should not change any state. e.g. 'isOkToArm()'.
153 Methods should have verb or verb-phrase names, like deletePage or save. Tell the system to 'do' something 'with' something. e.g. deleteAllPages(pageList).
155 Non-static functions should be prefixed by their class. Eg baroUpdate and not updateCompass .
157 Groups of functions acting on an 'object' should share the same prefix, e.g.
159 float biQuadFilterApply(...);
160 void biQuadFilterInit(...);
161 boolean biQuadIsReady();
163 rather than
165 float applyBiQuadFilter(...);
166 void newBiQuadLpf(...);
167 boolean isBiQuadReady();
170 ##Parameter order
171 Data should move from right to left, as in memcpy(void *dst, const void *src, size_t size).
172 This also mimics the assignment operator (e.g. dst = src;)
174 When a group of functions act on an 'object' then that object should be the first parameter for all the functions, e.g.:
176 float biQuadFilterApply(biquad_t *state, float sample);
177 void biQuadNewLpf(biquad_t *state, float filterCutFreq, uint32_t refreshRate);
179 rather than
181 float biQuadFilterApply(float sample, biquad_t *state);
182 void biQuadNewLpf(float filterCutFreq, biquad_t *state, uint32_t refreshRate);
185 ##Declarations
186 Functions not used outside their containing .c file should be declared static (or STATIC_UNIT_TESTED so they can be used in unit tests).
188 Non-static functions should have their declaration in a single .h file.
190 Don't make more than necessary visible for other modules, not even types. Pre-processor macros may be used to declare module internal things that must be shared with the modules test code but otherwise hidden.
192 In the .h file:
194 #ifdef MODULENAME_INTERNALS_
195 … declarations …
196 #endif
198 In the module .c file, and in the test file but nowhere else, put `#define MODULENAME_INTERNALS_` just before including the .h file.
200 Note: You can get the same effect by putting the internals in a separate .h file.
202 ##Implementation
203 Keep functions short and distinctive.
204 Think about unit test when you define your functions. Ideally you should implement the test cases before implementing the function.
206 Never put multiple statements on a single line.
207 Never put multiple assignments on a single line.
208 Never put multiple assignments in a single statement.
210 Defining constants using pre-processor macros is not preferred.
211 Const-correctness should be enforced.
212 This allows some errors to be picked up at compile time (for example getting the order of the parameters wrong in a call to memcpy).
214 A function should only read data from the HW once in each call, and preferably all at one place.
215 For example, if gyro angle or time is needed multiple times, read once and store in a local variable.
217 Use `for` loops (rather than `do` or `while` loops) for iteration.
219 The use of `continue` or `goto` should be avoided.
220 Same for multiple `return` from a function and multiple `break` inside a `case`.
221 In general, they reduce readability and maintainability.
222 In rare cases such constructs can be justified but only when you have considered and understood the alternatives and still have a strong reason.
224 Use parentheses around each group in logical and mathematical statements,
225 rather than relying on the implicit logic and operator priority.
226 The compiler knows what it’s doing but it should be easy for people too.
228 #Includes
229 All files must include their own dependencies and not rely on includes from the included files or that some other file was included first.
231 Do not include things you are not using.
233 "[#pragma once](https://en.wikipedia.org/wiki/Pragma_once)" is preferred over "#include guards" to avoid multiple includes.
236 #Other details
237 No trailing whitespace at the end of lines or at blank lines.
239 Stay within 120 columns, unless exceeding 120 columns significantly increases readability and does not hide information.
240 (Less is acceptable. More than 140 makes it difficult to read on Github so that shall never be exceeded.)
242 Take maximum possible advantage of compile time checking, so generally warnings should be as strict as possible.
244 Don't call or reference "upwards". That is don't call or use anything in a software layer that is above the current layer. The software layers are not that obvious in Cleanflight, but we can certainly say that device drivers are the bottom layer and so should not call or use anything outside the device drivers.
246 Target specific code (e.g. #ifdef CC3D) should be absolutely minimised.
248 `typedef void handlerFunc(void);` is easier to read than `typedef void (*handlerFuncPtr)(void);`.
250 Code should be spherical.
251 That is its surface area (public interfaces) relative to its functionality should be minimised.
252 Which is another way of saying that the public interfaces shall be easy to use,
253 do something essential and all implementation should be hidden and unimportant to the user
255 Code should work in theory as well as in practice.
256 It should be based on sound mathematical, physical or computer science principles rather than just heuristics.
257 This is important for test code too. Tests shall be based on such principles and real-world properties so they don't just test the current implementation as it happens to be.
259 Guidelines not tramlines: guidelines are not totally rigid - they can be broken when there is good reason.