Contributors should aim to stick to this style when submitting code for swerve. This is adapted from the suckless.org coding style.
- Comment with license, optional description of file
- Headers
- Macros
- Types
- Function declarations grouped alphabetically
- Global variables
- Function definitions grouped alphabetically
- Use C99 without extensions, as according to ISO/IEC 9899:1999
- Use
/* */
for sections of code,//
for notes on a single line
{
on the same line preceded by one space (except functions).}
on new line unless continuing statement (if else
,do while
, ...)- Use a block for a single statement if the inner statement needs a block.
- If one branch of a statement needs a block, give all of them blocks.
- Use tabs for indentation and spaces for alignment.
- Use spaces for multiline macros because that is considered aligning.
- Return type, modifiers, name, and argument list on the same line.
- Opening
{
on its own line. - Functions not used outside of source/header unit should be declared
static
.
- Global variables not used outside of source/header unit should be declared
static
. - In declaration of pointers, the
*
is adjacent to the variable name, not type.
- Use a space after
if
,for
,while
,switch
, as they are not function calls. - Do not use a space after the
(
or before the)
. - Use
()
withsizeof
. - Do not use a space with
sizeof()
.
- Place system/libc headers first in alphabetical order, using
<>
- Place local headers after an empty line in alphabetical order, using
""
. - Avoid cyclic header inclusion dependencies.
- Use
CamelCase
for type definitions. - Do not use
_t
suffix. - Do not typedef builtin types.
- Keep lines <=80 characters.
- Do not use
bool
types (instead use integers)
- Always test against
0
, not1
or-1
. - Use
goto
to cleanup when necessary instead of multiple nested levels. return
early on failures instead of multiple nested levels.- Unreachable code should have a NOTREACHED comment.
- Use enums for values that are grouped semantically, otherwise
#define
.