Lesson 34 of 1455
Refactoring Safely With AI
Refactoring means changing code without changing behavior. That used to be scary. With tests and AI together, it becomes routine.
Builders · AI-Assisted Coding · ~21 min read
Change Shape, Keep Behavior
Refactoring means improving the structure of code without changing what it does. Renaming for clarity, splitting one big function into small ones, moving code to a better file. AI is very good at these mechanical tasks because they rarely require new ideas.
The safety net first
- 1Make sure you have tests that pass before touching anything
- 2If no tests exist, ask the AI to help write a few
- 3Commit your changes to git so you can roll back
- 4Only then start refactoring
Three refactors AI does well
Extract-function is the most common refactor. AI handles it cleanly when you ask for it.
// Before: one long function doing too much function processOrder(order) { if (!order.email.includes("@")) throw new Error("bad email"); const tax = order.total * 0.08; const shipping = order.total > 50 ? 0 : 9.99; const final = order.total + tax + shipping; db.save({ order, final }); sendEmail(order.email, `Total: ${final}`); } // After: small, named functions a human can read function validateOrder(order) { if (!order.email.includes("@")) throw new Error("bad email"); } function calculateTotal(order) { const tax = order.total * 0.08; const shipping = order.total > 50 ? 0 : 9.99; return order.total + tax + shipping; } function processOrder(order) { validateOrder(order); const final = calculateTotal(order); db.save({ order, final }); sendEmail(order.email, `Total: ${final}`); }Refactor prompts that work
- Rename `x`, `y`, `z` to descriptive names based on how they are used
- Extract the validation block into a function called `validateOrder`
- Move these helper functions into a new file called `utils/pricing.ts`
- Replace the switch statement with a lookup map
When to stop
Every refactor has a diminishing return. Rename until names are clear. Extract until functions fit on one screen. Stop when the next change would be a matter of taste, not clarity.
“Refactoring is a surgical act. AI hands you the scalpel, but you still choose where to cut.”
Key terms in this lesson
The big idea: refactoring is safe when behavior is pinned down by tests and changes are small. AI is ideal for mechanical restructuring, but you own the safety net.
End-of-lesson quiz
Check what stuck
8 questions · Score saves to your progress.
Lesson help
Questions are best handled with a grown-up here.
For this age range, Tendril keeps freeform AI chat paused until parent/guardian consent and child-safe moderation are fully verified. Use the quiz, notes, and related lessons below, or ask a parent, guardian, teacher, or librarian to work through the question with you.
Progress saved locally in this browser. Sign in to sync across devices.
Related lessons
Keep going
Builders · 25 min
What Does AI-Assisted Coding Even Mean?
AI-assisted coding is not magic and not cheating. It is a new way of working where a model drafts, you decide. Let's draw a map before we start building.
Builders · 30 min
Your First Copilot-Style Completion
Let's actually feel what autocomplete is like. Write a comment, pause, and watch a full function appear. Then learn what to do next.
Builders · 30 min
Prompting for Code Is Different From Prompting for Prose
A prompt that writes a poem is not the same as a prompt that ships working code. Code has hidden standards. You need to make them explicit.
