What I Learned from Scott Meyers' Effective C++, Part 5
Chapter 5: Implementations
In my previous chapter, we discussed Designs and Declarations. In this chapter, we'll talk about what I learned in chapter 5 of the book, items 26 to 31.
Item 26: Postpone variable definitions as long as possible
This simple technique of postponing variable definitions until they're absolutely required helps you improve performance and lower the memory print of your programs. Though be careful around loops and avoid declarations inside loops where possible.
Item 27: Minimize casting
Casting in C++, especially dynamic_casts can cause performance issues, try to develop cast-free alternatives to your solution to avoid casting. If casting is necessary try to hide them inside functions so that your clients are not required to cast themselves. C++ provides modern casting solutions such as static_cast, dynamic_cast and reinterpret_cast. These are preferred in C++ code in comparison to C style casts. These casts are easier to find in code and they also hold the intent of casting.
Item 28: Avoid returning “handles” to object internals
C++ lets you return object internals such as references, pointers and iterators. This is bad practice as it is against the encapsulation principles and increases the chances of creating dangling handles as well as helps protect semantic constness.
Item 29: Strive for exception-safe code
Exception-safe functions ensure no resource leaks and corruptions occur even when exceptions are thrown from them. There are different levels of exception safety guarantees. It is important to understand what you can offer and what you require for a certain implementation. Basic, strong and no-throw guarantees are your options here. The strong guarantee can be implemented with the copy and swap technique but since this method has some copying overhead it might not be practical for every solution. It is important to understand that the weakest guarantee function within a function is the best guarantee it can offer.
Item 30: Understand the ins and outs of inlining
Inlining is essentially copy-pasting the body of a function into where it is meant to be called which avoids the function call overhead. However, when used generously it can create code bloat and debugging difficulties. So it's best to limit inlining to small and frequently called functions to maximize your chances of improved performance. It is also an important note to avoid declaring function templates inline and to not forget that the inline keyword is only a suggestion for the compiler
Item 31: Minimize compilation dependencies between files
C++ headers are an important tool to manage your compilation dependencies and minimize your compilation times. The general idea here is to depend on declarations instead of definitions such as using handle or interface classes. Your library headers should only exist in full and declaration-only forms regardless of whether templates are involved or not.
That's it for this chapter, next article will be about Chapter 6, Inheritance and Object-Oriented Design