Learn / Staying current

Read one open-source codebase

Lesson 31 of 37 · 7 min read ·

Why it works

You have read a lot of your own code and very little written by people better than you. Reading a well-regarded codebase is the cheapest available exposure to good judgement at scale — how to structure a project, how much abstraction is right, what production-grade error handling looks like, how to write tests that are worth having.

The obstacle is that opening a 200,000-line repository is overwhelming and most people close it in five minutes. So use a method.

The method

1. Do not start at the top. src/ alphabetically is the worst possible entry point. Start from a question.

2. Follow one path end to end. Pick a single operation you understand from the outside — one HTTP request, one CLI command, one function you have called. Find its entry point and follow it through every layer to where it returns. Ignore everything you pass that is not on the path. One complete trace teaches more than partial exposure to twenty files.

3. Read the tests first, actually. Tests are executable documentation with no ambiguity. They show the intended usage, the edge cases the authors worried about, and the bugs that have happened before. For an unfamiliar module, the test file is often the fastest way in.

4. Read the pull requests, not just the code. This is the step people skip and it is the most valuable. A merged PR shows you the reasoning: what was tried, what a reviewer objected to, what got rewritten. That discussion is the part you cannot get from the finished code. Find a significant recent PR and read it top to bottom.

5. Use git as an archaeologist. git log -p <file> and git blame on a strange-looking line will usually surface a commit message like "handle the case where the socket closes during handshake". Weird code is usually scar tissue. Learning what caused it is learning the domain.

6. Change something. Fix a typo, add a test, improve an error message. Getting a change through the contribution process — building locally, passing CI, responding to review — teaches you the project's real standards far faster than reading does.

What to look for

Read with specific questions rather than passively:

  • Where does this project put its boundaries? What is public API, what is internal?
  • How does it handle errors? Exceptions, result types, error codes? Consistently?
  • How much abstraction is there, and can you tell why each layer exists?
  • How are names chosen? Good naming is the most transferable thing you will pick up.
  • What is not there? Sometimes the lesson is the abstraction they chose not to build.

What to pick

  • A library you already use. You have context and questions, which is most of the battle.
  • Something the right size. A 5,000-line library is readable in a weekend; a browser engine is not.
  • Something in a language you know, so the language is not the obstacle.
  • Something well-regarded for its code, not just its popularity — Redis, SQLite, Flask, Requests, and Go's standard library are all frequently recommended for readability.

Gotchas

  • Not all popular code is good code. Popularity measures usefulness, not craft.
  • Old code follows old idioms. Do not copy patterns from a 2014 codebase uncritically.
  • Do not aim to understand everything. Understanding one path properly is a complete success.

Prove you know it

Pick a library you depend on, trace one function you call from its public entry point to its return, and write a paragraph explaining what happens in between. Then read one merged pull request on that repository and note one thing the reviewer caught that you would have missed.