liveBook · Manning

Code review

As explained in C++ Concurrency in Action, Second Edition

Code review is the systematic examination of source code to identify defects, improve quality, and check conformance to standards.

Code review is a deliberate, detailed inspection of source code to find potential defects. For multithreaded code, examine concurrency risks as well as ordinary bugs; a quick standards check or scan for obvious problems is not enough. section 11.2

A reviewer can be the code’s author or someone else. To review your own work, explain how it operates line by line—as if to another person—and write down the explanation if useful. This can counter the tendency to read what you intended rather than what the code actually does. section 11.2.1 section 11.2

For each line, consider what it does and ask questions such as:

  • Which data needs protection from concurrent access, and how is it protected? section 11.2.1
  • Where might other threads be, and which mutexes might they or this thread hold? section 11.2.1
  • Do operations across threads have ordering requirements, and how are those requirements enforced? section 11.2.1
  • Is the data still valid, or could another thread have changed it? section 11.2.1

Review by explaining

Explain a piece of code to a rubber chicken or write detailed notes. At each line, ask what could happen and what data it accesses; try assuming a bug exists and trace how it could arise. section 11.2.1

A careful review can still miss bugs, so follow it with testing to check that the code works. section 11.2

How C++ Concurrency in Action, Second Edition covers it

How deep it goes
Develops line-by-line questions about data, thread positions, mutexes, and operation ordering, while noting that review can miss bugs.
Examples
Use self-explanation, written notes, or a rubber chicken, then consider whether another thread could modify the data.
What you can do after
Diagnose potential defects by tracing data access, mutex ownership, thread interactions, and ordering requirements.
How it connects
Connects concurrency review to data races, deadlocks, mutexes, and multithreaded testing.

Also explained in

Compare how 10 books explain code review →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C++ Concurrency in Action, Second Edition. The section links point to the parts of the book it draws on.