|
Resurgence (PY2022)
Codebase for the Husky Robotics 2021-2022 rover Resurgence
|
This style guide is intended to maintain a consistent, readable style, and ensure the use of best practices in our code. It is not set in stone, and any part of it can be changed if enough members wish to do so and are willing to help update the codebase to match the proposed style - there isn't much point in a style guide if the code doesn't follow it!
You can automatically apply the formatting rules using the clang-format tool; see Automatic Formatting with `clang-format`Automatic Formatting with clang-format for more information.
When defining variables or parameters that have pointer or reference types, the * or & symbols should be aligned to the left, next to the type name, as they describe the type of the variable rather than the variable itself.
For example:
as opposed to
Include directives should be ordered as follows:
For example:
Good: ```c #include "Globals.h"
#include <cstdint>
#include <signal.h>
Bad: c #include <iostream> #include <signal.h>
#include "Globals.h" ```
In C++ code, whenever dynamic memory allocation is required, the new and delete/delete[] keywords should be used as opposed to malloc() and free(); the former are more type-safe, easier to use with more complex types, and have various other advantages - see this post on StackOverflow for a pretty good description of the differences.
There are a few exceptions:
The formatting rules can be applied automatically using LLVM's clang-format tool. On Ubuntu, it can be installed with sudo apt install clang-format; on Mac OS, it can be installed from Homebrew with brew install clang-format.
Please note that clang-format can only automatically apply formatting changes, and cannot implement the "best practices" rules above.
To format a file with clang-format, run
with as many file names as you want. For example:
You can even run something like
to format all C++ source files in a directory.
There are also plugins for several editors that integrate support for clang-format:
The style rules for clang-format can be found in .clang-format file in the top-level directory of the repository. When proposing changes to the style guide, please include the corresponding change to the .clang-format file as well (if possible).