Back

Lesson 2/5

5.1

How to read other people's code

Artifact: one thing you'd steal from a file in a project you respect

1. The practice

Reading code is a skill, and almost no one practices it. People practice writing code obsessively and then wonder why their writing doesn't improve past a certain point. It can't. Writing improves on the back of reading. Always.

A novelist who only ever writes and never reads will plateau in their first year. A programmer who only ever writes and never reads plateaus the same way, for the same reason — they only have one taste sample to compare their work against: their own previous code, which they like by definition because they wrote it.

2. What to read

Read code that's like the thing you want to be able to write, but better than what you'd produce on your own. Not way better — slightly better. The gap should be small enough that you can see why each choice was made.

Concretely:

  • A library you actually use. Open its source. Find the file for the feature you use most. Read it.
  • An open-source project in your stack with at least a thousand stars and reasonable code quality. Pick one file, not the whole repo.
  • A senior engineer at your company's code. Their merged PRs are a goldmine. Most people never look.
  • The previous version of a file you're about to edit. Read the git history of changes on it for the last year.

What to avoid: code that's too far above your level (you'll bounce off), code from tutorials (it was written to be readable, not realistic), and your own code from last month (you'll just feel bad).

3. How to read

Reading code is not reading prose. You can't go line by line from the top.

The way that works:

  1. Read the outline first. What does this file export? What's the public surface?
  2. Pick one entry point that's interesting. Don't try to understand everything.
  3. Follow the calls. When the entry point calls something, jump to that, recursively, until you hit something boring or terminal.
  4. Ask "why this shape?" at each step. Why is this a class instead of a function? Why is this parameter passed instead of imported? Why is the error handled here, not earlier?
  5. Write down one thing you'd steal. A naming choice, a pattern, a way of organizing — one thing per session that you'll use in your next file.

Steal one thing per session. That's how your code improves.

4. Try it