Recall is small, clean and yours. Real work usually is not. This lesson is about the other
situation, which is most of them.

Why old code is harder for an agent

It cannot see why. The code says what happens. It does not say that the strange rule on
line 40 exists because of a tax change in 2019. That reason is in somebody’s head, and it is
not in the context window.

It copies what it finds. Point an agent at messy code and it writes messy code that fits
in. It is matching the pattern it sees, and the pattern is the mess.

There are usually no tests. No tests means the loop from Lesson 3.2 cannot close, and
you are back to a chat window that types faster.

The approach that works

Step 1: Ask before changing.

Read src/billing.ts. Explain what it does, step by step, in plain English.
Do not change anything. Tell me anything that looks unusual or that you
are unsure about.

That last sentence is valuable. The things it flags as odd are often the things that exist
for a reason nobody remembers, which are exactly the things you must not casually “clean up”.

Step 2: Write a test for what it does now.

Not what it should do. What it does.

Write tests that capture the current behaviour of calculateTotal,
including any behaviour that looks wrong. Do not fix anything.

This feels strange the first time. You are recording bugs as expected results. That is the
point: now you can change the code and immediately see what your change altered. Without
this you are editing blind.

Step 3: Make one small change.

Then run the tests. Anything that goes red is something your change affected. Decide, one at
a time, whether each was intended.

Step 4: Only now, tidy up.

With tests in place, cleaning is safe. Before that, it is guessing.

What not to do

Do not point an agent at a large old file and say “refactor this”. You will get a large
change, to code you do not understand, with no tests, and no way to tell what moved.

That is the single most expensive mistake available to you at this stage.

Try this before the next lesson

  1. Find some old code of yours. Ask an agent to explain it. Was it right?
  2. Ask it to write tests capturing current behaviour. Do any of them encode a bug?
  3. Make one small change. What went red?