-
Notifications
You must be signed in to change notification settings - Fork 87
Coding conventions
Alexander Grund edited this page Sep 15, 2015
·
9 revisions
- Read the README.md in the repository root for instructions on how to compile on Linux, Windows/Visual Studio, ...
- 64Bit compilation on MSVC is currently not supported
- Compilation MUST finish without any warnings
- Classes/Structs: UpperCamelCase
- Parameters, local variables: lowerCamelCase
- Member variables: Suffix "_" (e.g.
myMember_) - Do not shadow variables (e.g. using the name of a member as a parameter)
- Small include files
- Use forward declarations instead of including headers
- Include what you use (e.g. when using
std::vector, include<vector>) - Once class/struct per file, avoid "collections" of structs even when they belong partly together
- Check STL/Boost before rolling your own implementations
- Implementing any of the Big 3 (copy ctor, copy assignment, destructor) requires implementing the others as well. For classes, that should not be copied (e.g. having open handles) declare copy ctor AND copy assignment in private region and do not implement it. Put a comment there.
- Comment functions you write at least with a summary in the header. use JavaDoc syntax (Either
///or/** foo bar */) - Make sure assumptions are met ->
- Use static assert (boost) to check what is possible during compile-time
- Use exceptions for enforcing stuff at runtime (e.g. parameter values, switch(enum) defaults)
- Use
asserts for general conditions that should be met, but are less critical or performance-wise expensive to check
- Avoid unnecessary branches: No need for an
elseafter areturn. Additional branches require additional indentation which makes (especially) longer functions hard to read. - Avoid code duplication: If you write the same or very similar thing twice (even in one function), factor it out into a new function. This eases maintainability if one ever needs to change or fix that.
-
Document your functions!
- put at least a one-liner in the header describing what it does
- For more complex functions include also pre-/post-conditions, special cases, ...
- If you have problems understanding a function and finally figured it out: Help others and add that to the docu
- Do not describe trivial things like "this is a copy-ctor/sets foo/gets bar" as this should be obvious from the function name (
FooBar(const FooBar&)/void setFoo(foo)/Bar getBar())
- Use RAII idiom
- Encapsulate all resources once they are acquired
- Use STL collections (
vector,list, ...) - Use smart pointers (boost
unique_ptr,intrusive_shared_ptr,shared_pointerin that order)
-
delete[]should never be used. Use C++ structs (e.g.vector) - Use
boost::arrayinstead of plain C-arrays - Do NOT use magic constants. All constants should be made named consts (or defines). For iterations, use iterators or actual sizes
- Do not use printf/sprint/snprintf,fprintf,... They can easily cause buffer overflows. Use C++ streams instead
