|
| 1 | +API Code Conventions |
| 2 | +==================== |
| 3 | +When writing user-facing code follow the [Arduino Style Guide](https://www.arduino.cc/en/Reference/StyleGuide) |
1 | 4 |
|
| 5 | +Implementation Code Conventions |
| 6 | +=============================== |
| 7 | +When writing code that is "hidden" from the end user (such as the actual *implementation* of Serial.println) please follow these guidelines: |
| 8 | + |
| 9 | +White space: |
| 10 | +* Expand tabs to 4 spaces. |
| 11 | +* Don't leave trailing whitespace at the end of a line. |
| 12 | +* For control blocks (if, for, while), put 1 space between the keyword and the opening parenthesis. |
| 13 | +* Put 1 space after a comma, and 1 space around operators. |
| 14 | + |
| 15 | +Braces: |
| 16 | +* Use braces for all blocks, even no-line and single-line pieces of code. |
| 17 | +* Put opening braces on the end of the line it belongs to, not on a new line. |
| 18 | +* For else-statements, put the else on the same line as the previous closing brace. |
| 19 | + |
| 20 | +Header files: |
| 21 | +* Header files should be protected from multiple inclusion with #if directives. See an existing header for naming convention. |
| 22 | + |
| 23 | +Names: |
| 24 | +* Use underscore_case, not camelCase for all names. |
| 25 | +* Use CAPS_WITH_UNDERSCORE for enums and macros. |
| 26 | +* When defining a type use underscore_case and put '_t' after it. |
| 27 | + |
| 28 | +Integer types: |
| 29 | +* Preference for using the <stdint.h> types |
| 30 | +* Use size_t for things that count bytes / sizes of objects. |
| 31 | + |
| 32 | +Comments: |
| 33 | +* Be concise and only write comments for things that are not obvious. |
| 34 | +* Use // prefix, NOT /* ... */. No extra fluff. |
| 35 | + |
| 36 | + |
| 37 | +Examples |
| 38 | +======== |
| 39 | + |
| 40 | +Braces, spaces, names and comments: |
| 41 | +``` |
| 42 | +#define TO_ADD (123) |
| 43 | +
|
| 44 | +// This function will always recurse indefinitely and is only used to show |
| 45 | +// coding style |
| 46 | +int foo_function(int x, int some_value) { |
| 47 | + if (x < some_value) { |
| 48 | + foo(some_value, x); |
| 49 | + } else { |
| 50 | + foo(x + TO_ADD, some_value - 1); |
| 51 | + } |
| 52 | +
|
| 53 | + for (int my_counter = 0; my_counter < x; my_counter++) { |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +Type declarations: |
| 59 | +``` |
| 60 | +typedef struct _my_struct_t { |
| 61 | + int member; |
| 62 | + void *data; |
| 63 | +} my_struct_t; |
| 64 | +``` |
0 commit comments