Style Guide
AquaPack Robotics Embedded Programming Style Guide
Note, this page is currently in the process of being written
Table of Contents
- Table of Contents
- Overview
- Rules
- General Rules
- Comment Rules
- White Space Rules
- Module Rules
- Data Type Rules
- Procedure Rules
- Variable Rules
- Statement Rules
Overview
This guide follows the general outline and some of the recommendations of Barr Group's Embedded C Coding Standard, set forth by Michael Barr.
Many of these rules are common sense and you probably are already following them in your own coding style. However, for consistency across the team it is practical to write them explicitly.
??? info Last updated: 2023-09-06 12:16 PM
Rules
General Rules
Line Width
- No line width is enforced as we do not do paper code reviews. Please use your best discretion for when a line is considered too long (if you're not sure, try to keep the longest line to around 80 characters).
- The preprocessor directive (
#define
) should not be used to alter or rename any keyword or other part of the C programming language. - The minimum target standard for both C and C++ is C/C++ 11. However, if newer versions such as 14 or 17 are available for the toolchain in use, they are preferred.
Braces
-
Left braces should appear on the same line as the statement they are connected with. Right braces should always be on a new line after their associated block of code.
Exampleif (condition) { code block; } else { code block line 2; code block line 1; code block line 3; }
Parentheses
- To maintain clarity of long expressions, parentheses should be used to ensure proper execution order and provide better information to maintainers.
- For further clarity, each side of a logical operator should be surrounded with parentheses, unless it is a single level operation.
Example
(current_depth == target_depth) && (current_depth < pool_depth)
soft_arm && hard_arm
Common Abbreviations
- Unless it is an abbreviation in common use and understood widely among programmers, refrain from using abbreviations as they can obfuscate the meaning of code.
- Common variable names, such as
i
for a loop counter can be used under the condition that it is of limited scope and purpose, and that the context it is in is exceedingly clear.
Casts
- Should a cast be required, the relevant lines should be commented to explain assumptions made.
Keywords
- The use of
goto
should be exceedingly limited to only cases where it is justifiably required, and no other solution can be found (including rewriting other code to make thegoto
unecessary). - Uses of the
continue
keyword should be limited, but are allowed where it simplifies the code structure and makes it more legible. - Generally if something can be defined as
static
, it should be. const
should be applied in all locations where appropriate. In some cases, it may be more applicable than a preprocessor directive, while the preprocessor directive may be more applicable in others.volatile
should be used in all cases where a variable can be modified by anything outside of the normal control flow of the code (i.e. an ISR, variables shared between RTOS threads, etc).
Comment Rules
- Both single (//) and multi (/*) comment styles are considered acceptable, and should be used as applicable. When multi-line comments are used, all lines should begin with an asterisk (*)
- Multi-line comments should maintain a level of indentation with the leading asterisks vertical to each other, as shown below.
- One leading space should be put between the comment text and the comment symbol.
- If placed at the end of a line of code, all comments should be vertically aligned (within the context of a file).
Example
the first line of code; // comment text
/* comment text
* comment text
* comment text
*/
line of code; // comment text
another line of code; // comment text
- Assume the reader is competent in the language, and avoid explaining the obvious. (You aren't trying to hit a comment count for an assignment, you're trying to actually be helpful to yourself and others in the future)
- Single line comments should be used where the function of a single line may be ambiguous.
Comment Locations
- Functions should be preceeded by a short comment block explaining the relevant values (inputs, outputs) and an extremely brief description of the purpose of the function. This comment block should follow Doxygen styling, linked here.
Example
/**
* Sums two numbers
* @param A an integer
* @param B an integer
* @return The summation
*/
int sum(int A, int B) {
int c;
c = a + b;
return c;
}
White Space Rules
- The keywords
if, while, for, switch,
andreturn
should be followed by a single space if there is more text on the same line. - Unless specified by another whitespace rule, all operators should be surrounded on either side by a single space.
- Unary operators should not have a space between the operator and the operand.
- Structure pointer and member operators should have no whitespace on either side.
- Parentheses, braces, and brackets should not have an adjacent space on the interior side.
- Excluding the end of a line, separating commas should should be followed by one space.
- Semicolons should follow a preceeding statement without a space, but should always be followed by a space unless they end a line.
- Use whitespace to separate blocks of code from each other in a logical fashion. It is suggested to use either 1 or 3 lines of separation, case dependent.
Pointer (* and &) Operators
- Pointer operators should be written without a space on the operand side (i.e.
type *name
). - Pointer variables should be declared on a line separate from non-pointer variables of the same type, to avoid confusion between the two.
Alignment Rules
- The # of a preprocessor directive should always be left aligned, regardless of the indentation of the line.
- Align relevant blocks of code together (Don't be stupid). TODO: Make this nicer?
Tab Rules
- The unicode tab character should not appear in files. Instead, the specified tab size is 4 spaces.
Module Rules
Naming Conventions
- File names should consist only of lower case letters, numbers, and underscores.
- File names are recommended to be no longer than 16 characters. Ideally file names are specific and short, like the file itself.
- All module names should not overlap with those of the standard library.
- All files that contain a
main()
function should include "main" within the filename. In general, there will be only one file containing a main function. In this case, the file hsould be namedmain.c
ormain.cpp
.
Header Files
- Include preprocessor guards against multiple inclusion within all header files (#ifndef NAME_H)
#pragma once
should not be used for protection against multiple files, see previous rule for alternative.- No variables should be defined within a header file, nor should any storage be allocated.
- Header files should concern themselves strictly with the information that is needed to be known by other modules. (Everything else should be declared within the source file itself)
- Unless required for a specific purpose, headers and source files should always be paired. Multiple source files per header file is discouraged.
Source Files
- Source files should be specific, and contain only the information relevant to controlling one 'domain' (UART driver, Servo control, etc).
- Source files should always
#include
the corresponding header file. - No absolute or relative paths should be used in
#include
statements. Instead, the build system should be used to include relevant links. - Do not include extraneous
#include
statements. - Do not directly
#include
another source file; always#include
the relevant header file. - Any variables shared internally across the entire module but that aren't declared according to the rules of the header file should be declared at the top of the module's source file.
Data Type Rules
Type Definitions
- When defining new data types, they should always be appended with the
_t
designation. - Structs and enums should be implemented only through
typedef
declarations. - Any data types that are
public
should have their module name prepended to them in the patternmodule_type
. - Any data types that are
public
are to be declared in their module's respective header file. - Any data types that are
private
are to be declared in their module's respective source file.
Use of Types
- Any time a specific number of bits needs to be used, one of the fixed-width integer types defined in
stdint.h
should be used, rather than one of the named types (short, long, long long). char
types should be used only when related to strings; See rule 1 in this subsection.bool
types should be used from the C standard library (stdbool.h
), rather than defined using enums or preprocessor directives.- Bit fields--such as those used for register mapping--should be defined only within unsigned integer types.
- Bitwise operations should only be done on unsigned integer types.
- Signed and unsigned integers should preferably never be mixed within operations
- Avoid floating point operations wherever practical. (There is some discretion required here. If it makes sense to do floating point (you have an FPU, it would violate common mathematical assumptions to do otherwise, etc.) proceed accordingly.)
TODO: Add content pertaining to unions
Procedure Rules
TODO: content
Variable Rules
TODO: content
Statement Rules
TODO: content