A Practical Guide to Reading Other People’s Code

Reading code you did not write is most of the job, once you have been working long enough. It is also a skill that is taught far less often than writing a fresh example. The aim is not to admire the style and not, at first, to improve it. The aim is to understand what the program does, where the risk is, and how a change might land.

Source code on a computer screen
Image: joncutrer, CC0 1.0

Start from the edges

Reviewing someone else's code
Image: Tony Webster from Minneapolis, Minnesota, United States, CC BY 2.0

I rarely begin on line one of a large file. I look for the way work enters the system: a command, a route, a message, a scheduled job. Then I follow one path all the way through to a result, ignoring branches I do not need yet. Tests, if they exist and if they are honest, are a map. A README that names the real entry points is a gift. A folder of unused abstractions is not the place to start.

I keep a notepad beside the code and write down words the project uses in a special way. Every codebase has a few of those. If I guess at their meaning, I will misread every function that uses them. When I am truly stuck, I will often copy a small, isolated piece into a scratch tool, which is one reason I still like writing small command-line tools: they are a way of asking a program a question without standing inside all of it.

Read for behaviour, then for taste

It is easy to get sniffy about formatting, about an older idiom, or about a library you would not have chosen. That can wait. First I want to know what happens when the input is empty, when a service is down, and when two things happen at once. Comments that explain a why are worth more than comments that restate a line. If the code is a web front end, I try to separate the parts that have lasted from the parts that were fashionable; that is the same instinct as in my note on CSS that has aged well.

When I have a working picture, I write a short summary for myself before I change anything. That summary often goes into the same notebook I use for other technical memory, described in a personal knowledge base without the ceremony. If I am later tempted to replace a whole service rather than read it, I try to be honest about whether I have actually understood it, a question that also appears in when to leave a cloud service.

Reading well is slower than people admit, and faster than rewriting in the dark. Follow one path, learn the local language, and write down what you think is true before you touch the file.