Style guidelines and formatting rules are not covered here, as they are specified in the .clang-format file. Be sure to use ClangFormat to format your code before committing.
All the below guidelines are not strictly enforced, but they are highly recommended to maintain code quality.
Style consistency matters far more than picking the best and the most readable style. Following a consistent style will make your code automatically more readable :).
- Use
UPPER_SNAKE_CASEfor macros. - Use
lower_snake_casefor everything else. - Don't use acronyms or abbreviate names unless they are widely known.
- Use
lower_snake_casefor files and directory names. - Use specificity-first convention for file names, e.g.,
project_logo.pnginstead oflogo_project.png.
- Use
///or///<style comments for Doxygen documentation. - Prefer limiting the documentation to 80 characters per line, though this is not strictly enforced.
- Avoid overuse of
@tags. Use them only when necessary, such as important params, noted return, general notes, etc. - Do not over document. You do not need to document every single function or variable. Moreover, you do not need to document every single parameter.
Below are some niche styles that I believe you should keep in mind.
- Avoid using
autounless the type is obvious from the context but verbose. - Same rule applies for use in
forloops or lambda parameters.
- Prefer using range-based for loops over traditional for loops when iterating over containers and index is not needed.
- Prefer using reference capture for all lambdas unless you need to copy the variable.
- Mark your function with
staticif it is meant to be used only in a specific source file.
- Prefer using
structoverclass. - Avoid using
privateandprotectedaccess specifiers. Usepublicfor everything.
Note: Even though I use struct, I refer to them as "classes" in the documentation.
- Prefer making your types aggregate types (i.e., no constructors, no private members).
- Prefer using
enum classoverenum.
- Prefer using
std::shared_ptroverstd::unique_ptrunless you are sure that the object will not be shared.