Module 045 min

Reading code before writing it

Every change starts with a question: where does this behavior actually live? Reading is the skill that makes the answer cheap.

After this module
  • You have a repeatable method for tracing a feature through code you've never seen.
  • You know the four shortcuts that replace "read everything" — and why reading tests beats reading code.

New engineers often try to read a codebase like a book, front to back. It doesn't work: real systems are not linear, and most of the code is irrelevant to your question. The efficient method is the opposite — start at the edge the user touches, and follow one path until you can point at the line that decides the behavior you care about.

ONE REQUEST, END TO END browserclick router/orders/:id handlervalidate servicethe rule repositoryquery db the response takes the same path back, transformed at each step start here · the URL or the button
Follow one path, not the whole tree. Once you can point at the service method, you know where the change goes — and which layers it must not leak into.

4.1Trace it, step by step

Try it · follow "GET /orders/42"6 steps

Search the code for the route string: /orders/:id. In most stacks that lands in one file with every route, or next to the handler. Resist opening anything else yet.

Shortcut: search for a user-visible string from the UI ("Order total") — it often appears in both the template and the code that builds it.

4.2Four shortcuts worth memorising

4.3Leave it slightly better

You now understand one path through the code better than anyone did an hour ago. That knowledge decays fast, so spend it immediately and in small amounts: rename the variable that lies about what it holds, add the test that would have caught the bug you just fixed, put one sentence in the README about the behavior you had to reverse-engineer.

This is the only sustainable form of cleanup. Grand rewrites fail because they require understanding everything before changing anything — the opposite of how understanding actually arrives. One honest improvement per visit, a hundred visits, and a codebase becomes pleasant to work in without anyone scheduling a project for it.

The trap · rewriting what you haven't understood

The code looks wrong, so you replace it — and the bug it was guarding against comes back three weeks later. That odd check might be load-bearing. Find the commit that added it before you delete it; if there isn't one, ask the person who did.

Check yourselfQuestion 1 of 1

You must change an unfamiliar feature. What should you read first?

60-second recap

  • Start at the edge, follow one path. Reading the whole tree is not a strategy.
  • The rule lives in the service, the assumptions live in the handler, the truth lives in the tests.
  • Search exact strings from the UI or the logs; use go-to-definition; stop time with a debugger.
  • Check history before deleting weird code. Someone wrote that check for a reason.