This is a special edition book printed for CppCon 2021 that contains all fixes in the current digital edition plus several new Work-In-Progress (WIP) chapters added to the end.
My goal as a trainer and a contractor (seems to be) to work me out of a job. I want everyone to:
Im thinking about changing my title from C++ Trainer to C++ Guide. I always adapt my courses and material to the class I currently have. We might agree on a class about X, but I change it to Y halfway through the first day to meet the organizations needs.
Along the way, we experiment and learn as a group. I often also learn while teaching. Every group is unique; every class has new questions.
Many of the questions I get in classes are the same ones repeatedly to the point where I get to look like a mind reader as I anticipate the next question that will be asked.
Hence, this book, and the Twitter thread that it came from, to help spread the word on the long-standing best practices.
I wrote the book I wanted to read. It is intentionally straightforward, short, to the point, and has specific action items.
About Best Practices
Best Practices, quite simply, are about
- Reducing common mistakes
- Finding errors quickly
- Without sacrificing (and often improving) performance
Why Best Practices?
First and foremost, lets get this out of the way:
Your Project Is Not Special
If you are programming in C++, you, or someone at your company, cares about performance. Otherwise, theyd probably be using some other programming language. Ive been to many companies who all tell me they are special because they need to do things fast!
Spoiler alert: they are all making the same decisions for the same reasons.
There are very few exceptions. The outliers who make different decisions: they are the organizations that are already following the advice in this book.
Whats The Worst That Can Happen?
I dont want to be depressing, but lets take a moment to ponder the worst-case scenario if your project has a critical flaw.
GameSerious flaws lead to remote vulnerability or attack vector.FinancialSerious flaws lead to large amounts of lost money, accelerating trades, market crash.AerospaceSerious flaws lead to lost spacecraft or human life.Your IndustrySerious flaws lead to Lost money? Lost jobs? Remote hacks? Worse?
Examples
Examples throughout this book use struct
instead of class
. The only difference between struct
and class
is that struct
has all members and base classes by default public
. Using struct
makes examples shorter and easier to read.
Exercises
Each section has one or more exercises. Most do not have a right or wrong answer.
Exercise: Look for exercises
Throughout the following chapters, youll see exercises like this one. Look for them!
Exercises are:
- Practical, and apply to your current code base to see immediate value.
- Make you think and understand the language a little bit deeper by doing your own research.
Links and References
Ive made an effort to reference those who I learned from and link to their talks where possible. If Ive missed something, please let me know.
Use the Tools: Automated Tests
You need a single command to run tests.
If you dont have that, no one will run the tests.
- Catch2 - popular and well supported testing framework from Phil Nash and Martin Hoeovsk
- doctest - similar to catch2, but trimmed for compile-time performance
- Google Test
- Boost.Test - testing framework, boost style.
ctest is a test runner for CMake that can be used with any of the above frameworks. It is utilized via the add_test
feature of CMake.
You need to be familiar with these tools, what they do, and pick from them.
Without automated tests, the rest of this book is pointless. You cannot apply the practical exercises if you cannot verify that you did not break the existing code.
Oleg Rabaev on CppCast stated:
- If a component is hard to test, it is not properly designed.
- If a component is easy to test, it is a good indication that it is properly designed.
- If a component is properly designed, it is easy to test.
Exercise: Can you run a single command to run a suite of tests?
- Yes: Excellent! Run the tests and make sure they all pass!
- No: Does your program produce output?
- Yes: Start with Approval Tests, which will give you the foundation you need to get started with testing.
- No: Develop a strategy for how to implement some minimal form of testing.
Resources
- CppCon 2018: Phil Nash Modern C++ Testing with Catch2
- CppCon 2019: Clare Macrae Quickly Testing Legacy C++ Code with Approval Tests
- C++ on Sea 2020: Clare Macrae Quickly and Effectively Testing Legacy C++ Code with Approval Tests
Use the Tools: Continuous Builds
Without automated tests, it is impossible to maintain project quality.
In the C++ projects I have worked on throughout my career, Ive had to support some combination of:
On
When you start to combine multiple compilers across multiple platforms and architectures, it becomes increasingly likely that a significant change on one platform will break one or more other platforms.
To solve this problem, enable continuous builds with continuous tests for your projects.
- Test all possible combinations of platforms that you support
- Test Debug and Release separately
- Test all configuration options
- Test against newer compilers than you support or require
If you dont require 100% tests passing, you will never know the codes state.
Exercise: Enable continuous builds
Understand your organizations current continuous build environment. If one does not exist, what are the barriers to getting it set up? How hard would it be to get something like GitLab, GitHub actions, Appveyor, or Travis set up for your projects?