Prompting for Code: Debugging, Refactoring & Tests
Learn how to prompt AI coding assistants effectively: write bug reports they can act on, refactor without changing behaviour, and get tests that actually catch bugs.
On this page 7 sections
Introduction
AI assistants can explain code, find bugs, tidy up messy functions and write tests in seconds. They can also confidently suggest a fix for a problem you do not have, or rewrite working code into broken code. The quality of the help depends heavily on the prompt — and on you checking the result. This lesson applies everything from earlier lessons to programming tasks.
- What a debugging prompt needs so the AI can find the real cause.
- How to ask for a refactor that keeps behaviour unchanged.
- How to get tests that would actually catch bugs.
- What to check before you trust AI-written code.
Debugging: Give the Full Picture
“My code doesn’t work, fix it” forces the assistant to guess. A good debugging prompt contains the same things a good bug report does:
- The relevant code — the function involved, not your entire project.
- The exact error message and stack trace, copied rather than paraphrased.
- What you expected to happen and what actually happened, with the input that triggers it.
- Your environment: language and version, framework, and how you run the code.
- What you have already tried, so it does not suggest the same thing again.
This Python 3.12 function should return the average of a list,but it crashes for one of my inputs.
def average(nums): return sum(nums) / len(nums)
Input: average([])Error: ZeroDivisionError: division by zeroExpected: 0 for an empty list.
Explain why it fails, then give a minimal fix.Asking it to explain the cause before fixing it is a useful habit: if the explanation is wrong, you know not to trust the fix, and if it is right, you learn something.
What is the most important thing missing from this debugging prompt?
My login function isn't working. Here's the code: [40 lines of JavaScript] Please fix it.
Refactoring Without Breaking Things
Refactoring means improving the structure of code without changing what it does. Assistants tend to “improve” more than you asked — renaming functions other code depends on, changing error handling, or swapping libraries. State the boundaries explicitly:
Refactor this function to be easier to read.Rules:- Do not change its name, parameters or return value.- Behaviour must stay exactly the same for all inputs.- No new libraries.Show the new version, then list each change and why it is safe.Asking for a list of changes makes review easier: you can check each change instead of comparing two versions line by line. Run your existing tests before and after, and for anything important, refactor in small steps rather than one big rewrite.
Writing Tests That Catch Bugs
Asked simply to “write tests”, assistants often produce tests for the obvious happy path only — or tests that just repeat what the code currently does, bugs included. Ask for the behaviours that matter: normal cases, edge cases (empty input, zero, very large values, unexpected types) and error cases. Name your test framework so the code runs in your project.
A useful trick is to describe the intended behaviour in your own words and ask for tests of that description, rather than of the code. Tests generated from the code alone will happily confirm a bug as “expected”; tests generated from what the function is supposed to do can catch it. When a bug is reported, ask for a test that reproduces it first, watch it fail, then fix the code — the test then guards against the bug coming back.
function average(nums) { return nums.reduce((a, b) => a + b) / nums.length;}You asked an AI for edge-case tests, and it wrote: expect(average([])).toBe(0). What happens when this test runs against the function above?
Reviewing AI-Written Code
- Run it. Code that looks right can still fail; tests and a quick manual run catch most problems.
- Check that every library, function and option it uses really exists — assistants sometimes invent plausible-looking APIs.
- Look for security issues: unvalidated input, secrets in code, unsafe SQL built from strings.
- Make sure you understand it well enough to maintain it. If you cannot explain a line, ask the assistant to explain it before keeping it.
- Never paste passwords, API keys or private customer data into an assistant you do not control.
Key Takeaways
- Debugging prompts need the code, exact error, expected vs actual behaviour and environment.
- Ask for an explanation of the cause before the fix.
- Refactoring prompts must say what must not change; ask for a list of changes.
- Ask for edge-case and error-case tests — and run everything before you trust it.
Summary
Good coding prompts look like good bug reports and good code-review requests. In the final lesson of this module you will learn where AI goes wrong more broadly — hallucinations, unverified facts and prompt injection — and how to protect yourself.