Introduction: Why Code Review is Needed in C++
Code review is one of the key practices of professional development. In the world of C++, where memory management errors or incorrect use of STL can lead to hard-to-find bugs and leaks, high-quality review becomes critically important. This is not just a style check, but a systematic search for potential problems: from logical errors to violations of RAII (Resource Acquisition Is Initialization).
In this article, we will look at what to pay attention to during code review of C++ code, how to formulate comments so that they are useful rather than offensive, and how to introduce a culture of healthy review in a team.
What to Check During Code Review of C++ Code
Review is not reading code like a book. It is active analysis. Use the following checklist to avoid missing important things.
1. Memory Safety and RAII
C++ does not forgive carelessness with resources. Check:
- Are smart pointers (
std::unique_ptr,std::shared_ptr) used instead of rawnew/delete. - Are there any memory leaks if the code uses raw pointers in complex scenarios.
- Is the RAII principle followed: all resources (files, sockets, mutexes) should be wrapped in classes with correct constructors and destructors.
// Bad: raw pointer, manual managementvoid process() { int* data = new int[100]; // ... forgot delete[] — leak}
// Good: smart pointervoid process() { auto data = std::make_unique<int[]>(100); // memory will be freed automatically}2. Correct Use of STL and Algorithms
The C++ standard library is powerful, but it is easy to use incorrectly.
- Check if a manual loop is written where
std::find,std::copy,std::transformcould be used. - Pay attention to iterator invalidation (e.g., after
push_backinstd::vector). - Is
constused for references when the object is not modified (const std::string&instead ofstd::string).
// Bad: copying a string and manual searchbool hasValue(std::string s, char c) { for (size_t i = 0; i < s.size(); ++i) { if (s[i] == c) return true; } return false;}
// Good: const reference and algorithmbool hasValue(const std::string& s, char c) { return std::find(s.begin(), s.end(), c) != s.end();}3. Modern C++ Rules (C++11/14/17/20)
Outdated constructs are a red flag.
- Replacing
typedefwithusing. - Using
autofor complex types (but not everywhere — readability is more important). - Using
nullptrinstead ofNULLor0. - Checking for
constexprandnoexceptwhere appropriate.
4. Performance and Architecture
Even correct code can be slow or unmaintainable.
- Avoid unnecessary copying: passing large objects by value is a common mistake.
- Check if a virtual function is called in a hot loop (if not justified).
- Evaluate class coupling: does the code violate SOLID principles.
How to Give Feedback: Rules for Constructive Review
The technical part is half the battle. The other half is the human factor. An incorrectly delivered comment can demotivate the author or cause conflict.
1. Separate Criticism of Code and Personality
Talk about the code, not the programmer. Instead of "You wrote terrible code," say "In this place, it is better to use a smart pointer to avoid a leak."