Indentation is four spaces -- no tabs. Only file types that absolutely require tabs (e.g. `Makefile`) may use them.
With the exception of documentation code, no line of any code or test file shall exceed 132 characters. If a line break is required, then it shall be after the first function parenthesis:
If a conditional, then after a completed conditional, for example:
Comments for extern
functions should be included in the static
functions and implementation-specific notes for extern
functions (i.e., not of interest to the general user) should be included in the
Inline comments shall start at character 69 and must not exceed the line length of 132. For example:
Variable names use camel case with the first letter lower-case.
Variable names should be descriptive. Avoid
Type names use camel case with the first letter upper case:
typedef struct MemContext <...>
typedef enum {<...>} ErrorState;
#define Constants
#define
constants should be all caps with
The value should be aligned at column 69 whenever possible.
This type of constant should mostly be used for strings. Use enums whenever possible for integer constants.
String Constants
String constants can be declared using the STRING_STATIC()
macro for local strings and STRING_EXTERN()
for strings that will be externed for use in other modules.
Externed strings should be declared in the header file as:
And in the C file as:
Static strings declared in the C file are not required to have a #define
if the #define
version is not used. Externed strings must always have the #define
in the header file.
Enum Constants
Enum elements follow the same case rules as variables. They are strongly typed so this shouldn't present any confusion.
Note the comma after the last element. This reduces diff churn when new elements are added.
Macro names should be upper-case with underscores between words. Macros (except simple constants) should be avoided whenever possible as they make code less clear and test coverage harder to measure.
Macros should follow the format:
If the macro defines a block it should look like:
Continuation characters should be aligned at column 132 (unlike the examples above that have been shortened for display purposes).
To avoid conflicts, variables in a macro will be named
Variadic functions are an exception to the capitalization rule.
Use
Use
C allows braces to be excluded for a single statement. However, braces should be used when the control statement (if, while, etc.) spans more than one line or the statement to be executed spans more than one line.
No braces needed:
Braces needed:
Braces should be added to switch
statement cases that have a significant amount of code. As a general rule of thumb, if the code block in the case
is large enough to have blank lines and/or multiple comments then it should be enclosed in braces.
Hints are to be formatted with capitalized
Warning and errors shall be lowercase with the exceptions for proper names and acronyms and end without punctuation.
Don't get exotic - use the simplest type that will work.
Use
Don't use a macro when a function could be used instead. Macros make it hard to measure code coverage.
Object-oriented programming is used extensively. The object pointer is always referred to as
An object can expose internal struct members by defining a public struct that contains the members to be exposed and using inline functions to get/set the members.
The header file:
THIS_PUB()
ensures that this != NULL
so there is no need to check that in the calling function.
And the C file:
The public struct must be the first member of the private struct. The naming convention for the public struct is to add
Variadic functions can take a variable number of parameters. While the printf()
pattern is variadic, it is not very flexible in terms of optional parameters given in any order.
This project implements variadic functions using macros (which are exempt from the normal macro rule of being all caps). A typical variadic function definition:
Continuation characters should be aligned at column 132 (unlike the example above that has been shortened for display purposes).
This function can be called without variable parameters:
Or with variable parameters:
If the majority of functions in a module or object are variadic it is best to provide macros for all functions even if they do not have variable parameters. Do not use the base function when variadic macros exist.
The
Subsequent code that is uncoverable for the same reason is marked with // {+uncoverable}
.
Marks code that is not tested for one reason or another. This should be kept to a minimum and an excuse given for each instance.
Subsequent code that is uncovered for the same reason is marked with `// {+uncovered}`.