Skip to content

Commit c007f3f

Browse files
authored
Update appendix3.md
1 parent b7d1a3f commit c007f3f

1 file changed

Lines changed: 70 additions & 2 deletions

File tree

‎book/en-us/appendix3.md‎

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,71 @@
11
# 31 nooby C++ habits you need to ditch
2-
1. using namespace std;
3-
2.
2+
https://youtu.be/i_wDa2AS_8w
3+
4+
1. `using namespace std;`
5+
1. don't do it in the header file
6+
2. `using std::string, std::cout`;
7+
2. use `std::endl` in a loop
8+
1. it flushes the buffer. thus should use `"\n"`
9+
3. iterate using index
10+
1. use `for(const auto& x: vec)`
11+
4. rewriting std algo
12+
1. find positive:
13+
2. `std::find_if(data.cbegin(), data.cend(), [](const auto& x){return x > 0;});`
14+
5. use c style array
15+
1. `template <std::size_t size>`
16+
2. `void f_arr(std::array<int, size> &arr)`
17+
6. any use of `reinterpret_cast`
18+
1. the only we allowed to do with the result is to `reinterpret_cast` back
19+
2. use `static_cast` instead
20+
7. cast away const using `const_cast`
21+
1. instead, for `std::map`, don't use `m[x]` but use const access `m.at(x)`
22+
8. not knowing map bracket inserts element
23+
9. ignoring const-correctness
24+
1. mark parameter as const whenever it should be
25+
10. not knowing string literal lifetime
26+
1. entire life time lvalue
27+
11. not using structured bindings
28+
1. `for(const auto&[name, hex]: mcolor)`
29+
12. out-params instead of returning a struct
30+
13. not using `constexpr`
31+
1. use it in function return type for compile time calculations
32+
14. forgetting to mark destructor virtual: it will only call base destructor
33+
15. thinking class members init in order of init list
34+
1. they actually in order of class member declarations
35+
16. not knowing about default vs value initialization
36+
1. default initialization: using default constructor, may contain garbages
37+
1. `int x;`
38+
2. `int *x = new int`
39+
2. value initialization:
40+
1. `int y{};`
41+
2. `int* y = new int{}`
42+
3. `int* y = new int()`
43+
17. MAGIC NUMBERS
44+
1. use `constexpr` with good name
45+
18. modifying a container while looping over it
46+
1. `auto x: v` will use the `end()`, which may change when adding new elements
47+
2. use index instead
48+
19. returning `std::move` of a local
49+
1. compiler will always do return value optimization for local variables
50+
20. thinking `std::move` moves something
51+
1. it just cast to rvalue reference
52+
21. thinking evaluation order is left to right
53+
22. unnecessary heap allocations
54+
1. stack may be enough
55+
23. not using `std::unique_ptr` and `std::shared_ptr`
56+
24. not using `std::make_unique` and `std::make_shared`
57+
25. any use of `new` or `delete`
58+
26. any manual resource management
59+
1. use `std::ifstream` instead of `FILE`
60+
2. RAII: Resource Acquisition Is Initialization
61+
27. thinking raw pointers are bad
62+
1. if the ownership is not a concern of the function, just use raw pointers
63+
2. if the pointer is coming from some c function, use self-defined Deleter
64+
1. `std::unique_ptr<int, CustomFreeDeleter>(some_c_function())`
65+
28. using shared ptr when unique ptr would do
66+
1. `unique_ptr` can be easily moved to `shared_ptr` or just assign to it.
67+
29. thinking shared ptr is thread-safe
68+
1. only reference counting is atomic and thus thread-safe
69+
30. mixing up const ptr vs ptr to const
70+
1. const applies to right, unless no right identifier
71+
31. ignoring compiler warnings

0 commit comments

Comments
 (0)