Book Review – Beautiful C++: 30 Core Guidelines for Writing Clean, Safe, and Fast Code

Written by on July 11, 2022 in Book Review, C++ with 0 Comments

Title: Beautiful C++: 30 Core Guidelines for Writing Clean, Safe, and Fast Code

Authors: J.Guy Davidson, Kate Gregory

Publisher:  Pearson Education

Year: 2022

My earliest introduction to C++ guidelines was through Scott Meyers’ excellent book Effective C++ that first appeared in 1992. After programming in C++ for around 3 years at that point, the book made a lot of sense to me. Of course, C++ continued to evolve and Scott Meyers published updated versions of the book. Another book along similar lines is C++ Coding Standards, by Herb Sutter and Andrei Alexandrescu. A few other books that teach the essentials of C++ devote a couple of chapters to good programming tips (please see the last section of this article). Overall, there is no dearth of books on C++ guidelines and coding standards.

C++ Core Guidelines was initially announced as an open source project on GitHub by Bjarne Stroustrup and Herb Sutter in 2015. There have been several contributions from others as well and the project is quite alive and active. The present book, Beautiful C++, discusses in detail 30 of these core guidelines.

This book is not meant for beginner C++ programmers. Read the book only if you have a fairly good understanding of a majority of the C++ language features and have done some serious programming. Otherwise, some of the ideas won’t sink in.

The book presents 30 guidelines in 5 sections, each section containing 6 chapters. Each chapter is devoted to a single guideline. The sections do not directly correspond to the section labelling used in the core guidelines, but when a guideline is taken up for discussion, the book mentions the associated core guideline section label for easy reference.  

Given that the authors of this book have rich experience in C++, the detailed explanation of each guideline along with numerous examples helps us understand how the guideline contributes to code quality. Although I felt that in some cases the explanation was a bit verbose, I am sure it will appeal to a wider audience.

I guess it is OK to disagree with some of the core guidelines as long as you understand their true purpose. As an example, I might not take the guideline “ES.10: Declare one name (only) per declaration” seriously. I believe it is a matter of style and taste. However, the authors explain clearly why this guideline makes sense and I very much appreciate that.

The guideline in Chapter 2.6, “T.120: Use template meta programming only when you really need to” might surprise some C++ programmers because TMP is generally considered fashionable and is supposed to be a special feature of Templates. However, the authors’ explanation along with relevant examples is quite convincing. One of the valid arguments put forth by the authors is that because TMP is a compile-time feature, debugging the program can be difficult. 

I definitely enjoyed going through all the guidelines, but the one I loved most is Chapter 5.5 “Per.7: Design to enable optimization”. Using the simple and well-known example of qsort function, the authors gradually refine its interface, each time explaining why it makes sense. It is difficult to forget the guideline after reading this chapter!

The book includes topics from almost all the important sections in the core guidelines. I would have loved to see a few more from the Concurrency and Parallelism section, but then that is just my preference. Overall, the authors have done a great job of covering important guidelines.

Another commendable effort by the authors is that the complete sample code included in the book is available for experimentation with the popular Compiler Explorer.

Adopting guidelines such as the ones included in this book will definitely go a long way in improving the quality of C++ code that we write. I strongly believe that companies and teams should establish proper code review procedures (automated and/or manual) as part of the QA process to ensure the guidelines are followed. The good news is that Visual Studio 2020, for example, includes a static code analyzer that can be configured to check for violations of the core guidelines in a project. Eventually, compiler support will become widely available and that would immensely benefit developers.

If you program in C++ for a living, then this book is a must-read and more importantly, must-follow! I hope the authors will come out with a sequel explaining the remaining core guidelines.

Recommended Additional Reading

1) Clean C++ 20: Sustainable Software Development Patterns and Best Practices (2nd Edition), Stephen Roth, Apress, 2021.

2) Embracing Modern C++ Safely, John Lakos, Vittorio Romeo, Rostislav Khlebnikov, and Alisdair Meredith, Pearson Education, 2022.

3) Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C+14, Scott Meyers, O’Reilly, 2014.

4) C++Coding Standards: 101 Rules, Guidelines, and Best Practices, Herb Sutter and Andrei Alexandrescu, Addison-Wesley, 2005.

 

Have a wonderful week ahead!

Tags:

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published.

Top