best_practices
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
best_practices [2016/03/11 12:28] – [Tips & tricks] jmgr | best_practices [2023/04/25 16:52] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 5: | Line 5: | ||
==== Tips & tricks ==== | ==== Tips & tricks ==== | ||
- | === Always | + | === Always |
<code cpp> | <code cpp> | ||
Line 14: | Line 14: | ||
// Good | // Good | ||
int myVariable{}; | int myVariable{}; | ||
- | std::string myString; | + | std::string myString; |
</ | </ | ||
Line 108: | Line 108: | ||
MyObject *object = new MyObject(parentObject); | MyObject *object = new MyObject(parentObject); | ||
</ | </ | ||
+ | |||
+ | Note the use of std:: | ||
=== Never mix C and C++ code === | === Never mix C and C++ code === | ||
Line 181: | Line 183: | ||
</ | </ | ||
- | === When using events, prefer using lambdas (nameless | + | === When using events, prefer using lambdas (anonymous |
<code cpp> | <code cpp> | ||
Line 207: | Line 209: | ||
<code cpp> | <code cpp> | ||
- | i++; | ||
// Bad | // Bad | ||
+ | i++; | ||
// Good | // Good | ||
Line 288: | Line 290: | ||
constexpr int MyConstantValue = 42; // Note the naming change here, caps should only be used for preprocessor defines | constexpr int MyConstantValue = 42; // Note the naming change here, caps should only be used for preprocessor defines | ||
</ | </ | ||
+ | |||
+ | === Prefer using enum classes rather that enums === | ||
+ | |||
+ | <code cpp> | ||
+ | // Bad | ||
+ | enum Color | ||
+ | { | ||
+ | BlueColor, | ||
+ | RedColor | ||
+ | }; | ||
+ | |||
+ | Color myColor = BlueColor; | ||
+ | |||
+ | // Good | ||
+ | enum class Color | ||
+ | { | ||
+ | Blue, | ||
+ | Red | ||
+ | }; | ||
+ | |||
+ | Color myColor = Color:: | ||
+ | </ | ||
+ | |||
+ | Please note that contrary to enums, enum classes cannot be implicitly converted to an integer. Use a static_cast if you need to convert from an integer to an enum class and vice-versa. | ||
+ | |||
+ | === Never use C-style casts, use C++ ones === | ||
+ | <code cpp> | ||
+ | enum class Color | ||
+ | { | ||
+ | Blue, | ||
+ | Red | ||
+ | }; | ||
+ | |||
+ | Color color{Color:: | ||
+ | int colorAsInteger{}; | ||
+ | |||
+ | // Bad | ||
+ | colorAsInteger = (int)color; | ||
+ | Color otherColor = (Color)colorAsInteger; | ||
+ | |||
+ | // Good | ||
+ | colorAsInteger = static_cast< | ||
+ | Color otherColor = static_cast< | ||
+ | </ | ||
+ | |||
+ | When dealing with QObject-based classes you should use [[http:// | ||
+ | |||
+ | === Header files should not depend on other #includes === | ||
+ | To test this, just include your header in an empty source file, if it doesn' | ||
+ | |||
+ | === Header files should be grouped by library and sorted, system headers should be at the bottom === | ||
+ | This helps readability. Putting system headers at the bottom helps ensuring that the previous rule is enforced. | ||
+ | |||
+ | <code cpp> | ||
+ | // Local headers | ||
+ | #include " | ||
+ | |||
+ | // Library headers | ||
+ | #include < | ||
+ | |||
+ | // System headers | ||
+ | #include < | ||
+ | #include < | ||
+ | </ | ||
+ | |||
+ | === Prevent dependency contamination === | ||
+ | Use case: you are using an " | ||
+ | |||
+ | <file cpp myclass.hpp> | ||
+ | #pragma once | ||
+ | |||
+ | #include < | ||
+ | |||
+ | class MyClass final | ||
+ | { | ||
+ | public: | ||
+ | MyClass() = default; | ||
+ | | ||
+ | private: | ||
+ | HWND m_window; | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | In this case, every file #including " | ||
+ | This dependency cannot be removed because you need to #include Windows.h to be able to use the type HWND. | ||
+ | |||
+ | Solution: here we can use the " | ||
+ | |||
+ | <file cpp myclass_private.hpp> | ||
+ | #pragma once | ||
+ | |||
+ | #include < | ||
+ | |||
+ | struct MyClass_Private | ||
+ | { | ||
+ | HWND window; | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | <file cpp myclass.hpp> | ||
+ | #pragma once | ||
+ | |||
+ | #include < | ||
+ | |||
+ | struct MyClass_Private; | ||
+ | |||
+ | class MyClass final | ||
+ | { | ||
+ | public: | ||
+ | MyClass(); | ||
+ | | ||
+ | private: | ||
+ | std:: | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | <file cpp myclass.cpp> | ||
+ | #include " | ||
+ | #include " | ||
+ | |||
+ | MyClass:: | ||
+ | m_private{std:: | ||
+ | { | ||
+ | // Access window by using m_private-> | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | And that's it. It is indeed an increase in complexity and has a slight impact on performance, | ||
=== Write small functions instead of huge ones === | === Write small functions instead of huge ones === | ||
=== Never call virtual functions from a constructor === | === Never call virtual functions from a constructor === | ||
Reference: [[https:// | Reference: [[https:// | ||
+ | === Classes and variables === | ||
+ | You should not consider a class as a collection of variables, but rather like a service provider. That is the reason why you should put the public functions first, before the private ones. Variables should be put at the end, since they are an implementation detail. | ||
+ | |||
+ | ===== Comments ===== | ||
+ | What amount of comments should you write? Too many comments will make the code unreadable (see AutoHotKey for an example). | ||
+ | |||
+ | TODO: Add something about Doxygen and how to write compatible comments | ||
+ | |||
+ | <code cpp> | ||
+ | // Bad | ||
+ | void stuff(int blih) | ||
+ | { | ||
+ | int blah = 5; // We set blah to 5 here | ||
+ | int bloh = std:: | ||
+ | | ||
+ | // | ||
+ | | ||
+ | return blah; // We return the value of the variable blah of type integer. We then terminate this function and continue our merry way into our program. Yep. That's it. Bye. | ||
+ | } | ||
+ | |||
+ | // Good | ||
+ | // Note that this " | ||
+ | void stuff(int blih) | ||
+ | { | ||
+ | int blah = 5; | ||
+ | // We need to compute bloh here because... | ||
+ | int bloh = std:: | ||
+ | | ||
+ | // | ||
+ | | ||
+ | return blah; | ||
+ | } | ||
+ | </ | ||
+ | |||
===== Qt specific ===== | ===== Qt specific ===== | ||
best_practices.1457699323.txt.gz · Last modified: 2023/04/25 16:52 (external edit)