This page contains guidelines for writing new C source code for the Paparazzi project.
If you are using Eclipse, you can use Eclipse code style located in the repository (paparazzi_code_profile_eclipse.xml)
Formatting Guide
- remove any trailing white space at the end of lines.
- use 2 spaces for indentation; do NOT use tabs.
- use Unix line endings ('\n'); do NOT use DOS endings ('\r\n')
- limit code length to 100 chars per line
- limit adjacent empty lines to at most two (2).
- remove any trailing empty lines at the end of source files
- do not "comment out" code from the tree; instead, one should either:
- remove it entirely (git can retrieve the old version), or
- use an
#if/#endif
block.
Naming Rules
- most identifiers must use lower-case letters (and digits) only.
- macros should use upper-case letters (and digits) only.
Type Guidelines
- use the types from <inttypes.h>:
int8_t
, int16_t
, int32_t
, or int64_t:
signed types of specified size
uint8_t
, uint16_t
, uint32_t
, or uint64_t:
unsigned types of specified size
Functions
- static inline functions should be prefered over macros in most cases:
#define CUBE(x) ((x) * (x) * (x))
static inline int cube(int x) { return x * x * x; }
- Functions should be declared static unless required by other modules
- define static functions before first usage to avoid forward declarations.
- Functions should have no space between its name and its parameter list:
Switch statements
- specify a default case
- prefer an enum over defines for the different states
STATE_FOO = 1,
STATE_BAR = 2
};
case STATE_FOO:
break;
case STATE_BAR:
bar();
break;
default:
break;
}
If statements
- return statement on the new line even for ""simple if"" case
return 1;
}
if (x < other.x) {
return -1;
}
else if (x > other.x) {
return 1;
}
else {
return 0;
}
Preprocessor directives
- For conditional compilation use
#if
instead of #ifdef
. Someone might write code like: #ifdef USE_FOO
bar();
#endif
Someone else might compile the code with turned-off foo info like: gcc stuff.c -DUSE_FOO=0
So use #if
not #ifdef
to turn a feature on or off. This works fine, and does the right thing, even if USE_FOO is not defined at all (!) #if USE_FOO
bar();
#endif
- If you really need to test whether a symbol is defined or not, test it with the
defined
construct.