...
Good | Not Good | Notes | |
---|---|---|---|
Include guards |
|
| Although Note that include guards are used to prevent double inclusion of headers. |
Conditionals and loops |
OR
|
OR
OR
| Braces are always required, even for single-line statements. They should be on the same line as the conditional. Adequate spacing should be provided between statements. In general, please be consistent. |
Indentation | 2 space indents | Anything else | Only use spaces. Do not use tabs. Use Unix-style line endings. |
Variable names | uint8_t descriptive_name = 0; | int a = 0; | Prefer descriptive, reasonable length variable names. Note that function and variable names follow the underscore_lowercase naming convention. |
Variable names (static) | static uint8_t s_descriptive_name = 0; static void prv_func(void) { | Anything else | A static variable that is global to a file should be prefixed with s_. You should not have static variables in a function, since your functions should not be maintaining state (unless you have a really good reason). |
Function names (public) | void module_func(void); | void func(void); | Prefix public functions with the module's name. |
Function names (private) | static void prv_func(void); | static void func(void); | Prefix private functions with prv_ to denote that it is not visible. |
Macros |
| #define macro(x) x*2 | Avoid macros as functions, but they should be ALL_CAPS and their parameters should be enclosed in brackets if used. |
Enums |
|
| Typedef enums because we generally use them to group bitmasks, and it would get messy quickly. Declare them in ALL_CAPS prefixed with the enum name. The final entry in any enum should be NUM_ENUM_TYPES. Note the plurality of the type and the prefix NUM. This allows for the writing of nice guard clauses. |
Datatypes | uint8_t, uint16_t, int16_t, etc. | char, int, short, etc. | Use explicit standard datatypes. |
Structs |
|
| Use UpperCamelCase for struct definitions. Typedef'd structs make it easier to think about them as objects, and provide some abstraction. The original struct should still be named to allow for forward declarations. |
Struct/array initialization |
|
| Structs and arrays should have 1 space between each brace and the first/last elements. GNU Style initializers are encouraged as they are more explicit and ensure consistency in the result of changes in definitions of structs/enums. The trailing As of August 2017 there is a bug in clang-format so that
|
Line lengths | Around 100 characters | Above 100 characters | Try to keep line lengths reasonable. Around 100 characters is a good length. |
Bitwise operations | REG |= BIT1 | BIT2; | REG |= BIT1 + BIT2; | Make use of bitwise operations whenever working with bitmasks. Use macro or enums to define bitmasks. Try to keep the order of operations from MSB to LSB. |
Comments |
int8_t a = 0; // This is also ok. | /* We do not use multi-line comments */ | Use the Comments can also be trailing to a line |
Pointer formatting |
|
| The asterisk should always be adjacent to the variable. Do not declare multiple variables in the same declaration if any are pointers. |
...