-
Notifications
You must be signed in to change notification settings - Fork 39
Coding Conventions
Štěpán edited this page May 21, 2018
·
3 revisions
- High warnings - /W4 in VisualC++
- Disable only 3rd party warnings
- Have the code warning-free (0 warnings on compile), you can use /Werror
- Use C++17
- CONST is your friend, use const ALL THE TIME, you should have a reason for NOT using const, rather than for using const
- do not use #defines unless neccessary, const float Pi = 3.14f is better than #define PI 3.14
- do not leave dead code -- old code that is commented out, nobody knows what it did, it doesn't compile and we just leave it there because it's fun
- RAII - Resource acquisition is initialization, familiarize yourself with this concept.
- Do NOT use new or delete. std::unique_ptr is always better.
- Same goes for mutexes, use std::lock_guard
- Use struct if it's a data object - struct coord {float x; float y;}
- Use class if the object has data and operates on them.
- Class definition should first list all defines, typedefs and class/struct declarations. Then all members, first public, then private (this should not happen frequently). Last is all methods, first public, then private.
- ClassName
- variableThatIsVeryLong - do NOT use "vrblvlng"
- MACRO(), GLOBAL_VARIABLE
- mClassMember - debatable, but probably the better case
-
Use C++, not C. Meaning:
- Use references &, which really are only const pointers
- Output parameters should be used with & if they are large (std::vectors)
- Small output parameters should be returned via std::tuple
- Familiarize yourself with move semantics
- Use static_cast<T>(variable) instead of T(variable) or (T)variable
- For null pointers do not use 0, use nullptr
- Comparing pointers should be done with nullptr, eg.: while(pointer != nullptr) { ... }
-
Input paramters are ALWAYS const
-
Use auto reasonably - auto cislo = 10 / 5; is wrong. auto lambdaFunction = [](){ ... } is correct.
-
Use asserts() to check different "invariants" - my function did not get an empty vector of triangles. The number of elements to average is not 0, etc.
- Install clang-format, use provided .clang-format file. Get clang format here., install LLVM package and the plugin to VisualStudio. Allow LLVM to add to path.
- Update: Visual Studio 15.7.1 supports .clang-format files without the need for plugins: Tools → Options... → Text Editor → C/C++ → Formatting → General → ClangFormat execution.
- Install git, use git.
- Use google tests. When adding a new functionality, add unit tests that go along with it. Try to be hard and test the code proper.
- Do NOT commit code that doesn't compile, or code that you know is WRONG.
- Use COMMIT MESSAGES AND commit descriptions. Commit message should look something like: "Added basic paint brush, few features missing", with the missing features listed in the description.
- The smaller the commits, the better. Git gui is able to commit only chunks of code in the same file. Use this, it allows you to commit separate features into separate commits.
- Use branches, name them coherently - "ptbrush_new" versus "feature/paint-brush"
- Resolve your conflicts, --pull again, then push. No force pushes
❤️ Pepr3D 2018-2019 · Home