This is a book review of “Beautiful C++,” written by two well-known C++ experts and educators: Kate Gregory and Guy Davidson. The book’s unique style gives us a valuable perspective on effective and safe C++ code.
By Bartlomiej Filipek
From the review:
The book is split into five parts, and each part has six guidelines to help us understand the material more easily.
- Part one: Bikeshedding is bad - how to reduce the number of lines of your code and simplify it. For example, by using use in-class member initializers or avoiding getters and setters.
- Part two: Don’t hurt yourself - how to avoid messy code and minimize complexity. For example, by limiting explicit sharing of variables, proper use of ABI and more.
- Part three: Stop using that - some important bugs and bad C++ code style: singletons, casting away const or unsafe managing the ownership of resources.
- Part four: Use this new thing properly - efficient use of modern C++ style: enum classes, constexpr, templates.
- Part five: Write code well by default - good patterns for your code: type safety, immutable data structures avoiding uninitialized variables, and RAII.
In total, it’s around 300 pages + afterword and index.
If we look at a corresponding guideline like I.3: Avoid singletons you’ll see a short paragraph of introduction, some small example and a short discussion to alternatives. On the other hand, in the book, this guideline consists of 9 pages with unique examples, backstories, alternatives and discussions. The section shows things like:
- Why global objects are bad
- Singleton Design pattern overview
- Static initialization order fiasco
- Hiding singletons and making a namespace rather than a class
- And even how to use constexpr in some cases (
constexpris also covered in other sections)