Refactoring

From working code to better code, safely

9 lessons56 minPlus
Lesson 1 is free now. The rest comes with Plus.

What you'll gain

Refactoring is the one job most of us won't hand to an agent, because it never knows when to stop and it can change how your code behaves with the tests still passing. Here, you'll record what the code does before anything moves, and set limits on what the agent's allowed to touch, so you can prove nothing behaves differently. Then the code you've been avoiding for a year is something you can tidy up any week.

  • You'll refactor code locally or in production, verify changes against your guardrails and test suite, and ensure the refactored code is cleaner without breaking anything.

When to use it

  • Code is working but needs restructuring
  • Technical debt is slowing down development
  • Preparing code for a major new feature

You'll reach for it: When code needs restructuring

What you leave with

11
Prompts

Syllabus

9 lessons · 56 min
1
Your Test Suite Won't Save You
A refactor can change how your app behaves and leave every test green. We'll hand one file to an agent with "clean this up", watch the diff spread across four files, and find the broken behaviour by using the app rather than reading the diff.
4 minFree
2
Refactor What's Costing You
The code that looks worst is usually the code nobody opens, so it isn't costing you anything. We'll get the agent to read your git history, rank what's genuinely worth refactoring with a line of evidence for each one, and then put a name against every entry: agent job, your job, or leave it alone.
4 min
3
Record It Before You Break It
You can't tell whether behaviour changed if you never wrote down what it was doing. We'll have the agent write characterisation tests that record exactly what your target does today, wrong bits included, then break the code on purpose to check the net actually fires.
5 min
4
Draw the Agent a Box
An instruction with no limits in it doesn't have any, so everything the agent can see is fair game. We'll write a scope block you keep and reuse on every refactor, run the same prompt with it and without it, and put the two diffs next to each other.
5 min
5
The Incremental Loop
Three changes in one prompt and a red suite at the end tells you nothing except that something's broken. We'll run refactors one small change at a time and get the agent to write a hook that runs the suite whether you remember to or not.
7 min
6
Five Ways It Goes Wrong
Four of these five leave your test suite completely green, which is exactly why they're worth knowing by sight. We'll run every one of them for real, then you'll go and find three of them planted in a diff with no hints from me.
8 min
7
The Tidying You Never Asked For
Most of the agent refactoring you've merged wasn't something you asked for, it came bundled inside feature commits with nothing in the message about it. We'll go through your last twenty commits and find it, then add an AGENTS.md rule and a gate check that keep it out of the next one.
9 min
8
A Net You Can Build in Ten Minutes
Not everything you want to tidy has tests, and you don't always have an afternoon to write them. We'll record what the code produces for a set of real inputs, refactor, diff the two, and then be honest about how much protection that actually buys you.
6 min
9
Give It the Destination First
Ask an agent to restructure your app and you get the generic module layout from forty blog posts, because it had to invent the destination itself. We'll hand it a real one, an article, a reference repo and your own notes on which boundaries matter, then cut the plan into stages that each leave the project working.
8 min

Access

It's part of Plus. Lesson 1 is free to watch for everyone, no account needed.

Every lesson of every course, the full notes, transcripts and prompts, and the Unlearn-built tools. Blog posts and the tools that are public on GitHub stay free for everyone.

It shows as Completed on your profile and counts toward its role. Finishing every step of a role earns the role certificate.

Yes. Team plans can assign this course, or a whole role, and follow progress in Team.

Next step

Next in Product Engineer
Getting Up to Speed on Any Codebase
Workflow · 2 lessons · 11 min
Go to the next step