Code Review in C++: What to Check and How to Give Feedback
Code review is a mandatory stage of development, especially in C++. This language provides enormous freedom, but with it comes responsibility. For beginner developers, the review process can seem intimidating, but in reality, it is a powerful learning tool. In this article, we will break down what to look for when checking C++ code and how to give feedback that is helpful, not hurtful.
1. Basic Correctness and Memory Safety
The first thing to check in any C++ code is correct memory management. C++ does not have a garbage collector, so errors here are critical.
What to check:
- Memory leaks: Every
newmust have a correspondingdelete, everynew[]must have a correspondingdelete[]. Better yet, use smart pointers (std::unique_ptr,std::shared_ptr). - Array out-of-bounds: Using
std::vector::at()instead of the[]operator to catch errors. - Dangling pointers: Pointers referencing already freed memory.
- Double free: Calling
deletetwice on the same memory block.
// Bad: manual memory managementint* arr = new int[100];// ... work ...delete[] arr; // Easy to forget
// Good: using RAIIstd::vector<int> arr(100);// Memory will be freed automaticallyTip for the reviewer: If you see raw pointers (int* ptr) or manual new/delete — that's a red flag. Suggest using std::unique_ptr or STL containers.
2. Using the Standard Library (STL)
C++20/23 provide a rich standard library. Beginners often reinvent the wheel where ready-made solutions could be used.
Checklist:
- Containers: Instead of raw arrays —
std::vector,std::array,std::map. - Algorithms: Instead of manual loops —
std::find,std::sort,std::copy. - Strings:
std::stringinstead ofchar*. - Streams:
std::ifstream/std::ofstreaminstead of C-stylefopen.
// Bad: manual searchbool found = false;for (int i = 0; i < vec.size(); ++i) { if (vec[i] == target) { found = true; break; }}
// Good: STL algorithmbool found = std::find(vec.begin(), vec.end(), target) != vec.end();How to give feedback: Instead of "This is wrong," say: "Great logic! The STL has a ready-made function std::find that does the same thing, but shorter and safer. Shall we try using it?"
3. Code Style, Naming, and Const Correctness
Code readability is half the battle. Even perfectly working code is useless if it cannot be maintained.
What to look for:
- Naming: Variables —
snake_case(per C++ standard), classes —PascalCase. Avoid single-letter names (exceptifor counters). - Const correctness: Parameters that are not modified should be
const. Methods that do not modify the object should beconst. - Formatting: Consistent indentation style (usually 4 spaces), spaces around operators.
- Magic numbers: Extract them into constants (
constexpr int MAX_SIZE = 100;).
// Bad: unreadableint a = 5;for(int i=0;i<10;i