Most advice about AI coding assistants covers one direction: describe what you want and get code. Developers also face the reverse problem all the time. You have working code and need a prompt that explains it well enough for an assistant to review it, test it, convert it to another language or rebuild it from scratch. Pasting the code with the words “fix this” usually yields a confident answer to a question you did not ask. This guide shows how to write the prompt that gets the right answer.
What a good code prompt contains
Code on its own hides most of what an assistant needs to know. A strong prompt states the things the code only implies:
- Context: language, version, framework, and where this code runs.
- Purpose: what the code is meant to do, in a sentence a new teammate would understand.
- Inputs and outputs: types, shapes and examples of real values.
- Constraints: performance targets, compatibility, style rules, libraries you must or must not use.
- The task: exactly one of recreate, explain, refactor, test, convert or review.
- The output format: a diff, a full file, a list of issues, or a test suite.
One task per prompt
Assistants perform best when the request is singular. “Explain, refactor and add tests” in one message tends to do all three superficially. Chain them instead: first ask for an explanation and check that the assistant understood the intent, then ask for the refactor, then for tests against the refactored version. The explanation step doubles as a cheap correctness check, because a wrong explanation reveals a wrong understanding before any code changes.
A real-world example: refactoring a pricing function
A developer inherits a function that calculates order totals. It is a long block of nested conditions: discounts for loyalty members, a bulk discount, a regional tax rate and a free-shipping threshold, all intertwined. She wants it refactored without changing behaviour.
Her first prompt, make this cleaner, returns a pretty version that quietly changes how discounts stack. She rewrites the prompt:
Language: PHP 8.2, no framework. The function calculateTotal() computes an order total. Behaviour that must not change: loyalty discount applies before the bulk discount; tax is calculated on the discounted subtotal; shipping is free above 75.00 after discounts. Task: refactor into small single-purpose functions with type declarations. Do not change any rounding. Output: the full refactored file, then a list of any behaviour you were unsure about. Then write PHPUnit tests that cover each rule and the boundary at 75.00.
Two parts matter most. Stating the behaviour that must not change tells the assistant what the tests must protect. Asking for a list of uncertainties makes it surface ambiguity instead of guessing. She runs the generated tests against the original function first, to confirm that they describe real behaviour, and only then against the refactor. This is a safe workflow for any AI-assisted change: tests anchor the old behaviour, then the code moves.
Protect secrets and private code
Before you paste code anywhere, remove API keys, passwords, tokens, personal data and anything covered by a confidentiality agreement. Check your employer’s policy on sharing source code with third-party services. Replace real values with placeholders such as API_KEY_HERE.
Verify everything
AI-generated code can be wrong in ways that look right. Read it, run it, test the edge cases, and review security-sensitive parts (input handling, authentication, file access, database queries) with extra care. A prompt improves your odds; it does not replace review.