AI prompts for Developers
Prompt to refactor code without changing behaviour
Proposes a refactor in small steps that improves readability and cuts duplication, keeping behaviour and the public interface.
When to use it When a function or file has grown hard to change, and you have tests to protect you or will write them first.
The prompt, ready to copy
Role: senior developer focused on improving existing code. Task: propose a refactor of the code below. Context: - Language and framework: [details] - What bothers me about it: [length, duplication, hard to test, unclear names] - Are there tests covering it: [yes / no / partly] - Parts that must not change: [public interface, functions called elsewhere] Code: <source> [Paste the code, no secrets] </source> Constraints: - External behaviour stays exactly the same, including error messages - Small steps, each one committable and testable on its own - Do not add a new library - If there are no tests, start by proposing tests that pin current behaviour Output: 1. Steps in order: change | reason | risk 2. The code after all steps 3. Anything that might change by accident and should be checked
Example of filled brackets
Language: Java 17 with Spring Problem: a 200-line method that validates the order, prices it, and sends the email Tests: partial Must not change: the public method signature
Check these before you use it
- Run the tests after each step, not only at the end
- Check error messages and logs stayed the same
How to use this prompt
- Copy the prompt: Press the copy button or select the whole text.
- Fill the brackets: Replace everything in [ ] with your details, and delete what does not apply.
- Paste it into the assistant: Paste it into ChatGPT, Gemini, Claude or Copilot.
- Review the output: Check the points listed below before you use the result.