The best prompts for coding: 12 templates that save time
Code review, bug hunting, tests, refactoring, SQL, regex: 12 proven prompts for ChatGPT and Claude, with variables and notes on why they work.
A good coding prompt doesn't differ from a bad one by "magic words" — it differs by how much context and how many constraints it gives the model. Below are 12 templates we use ourselves. Each uses variables in the {{name}} or {{name|default}} format: fill in your data and the prompt is ready.
Three rules that matter more than any template
- Give context. Language, version, framework, environment constraints. "Node 22, ESM, no external dependencies" saves three rounds of clarification.
- Specify the answer format. "Diff only", "list the problems first, then code", "no explanations". Otherwise the model picks the format, and it's usually an essay.
- Ask it to check itself. "List your assumptions", "which cases aren't covered". It's a cheap way to catch a hallucination before the code lands in your repo.
Review and understanding
1. Code review
Review this {{language|TypeScript}} snippet.
Context: {{context|Node 22 service, ESM}}.
In order of importance, find: bugs, security issues, races and leaks,
then readability. For each issue: line, why it's a problem, the fix.
Don't comment on style or formatting. If everything is fine, say so.
{{code}}
Why it works: the priority order and the "no style" rule cut the noise. "If everything is fine, say so" reduces the urge to invent a problem.
2. Explain unfamiliar code
Explain what this code does to someone who knows {{language}} but not this project.
Start with one sentence: why it exists. Then walk through it step by step. End with
the non-obvious parts and what breaks if you change them.
{{code}}
3. Complexity and bottlenecks
Estimate the time and space complexity of this function. Say on which inputs it
becomes a bottleneck at {{scale|1M records}}, and suggest one optimization with an
estimate of the gain. Don't rewrite the whole thing.
{{code}}
Bugs and debugging
4. Find the cause of a bug
Expected behavior: {{expected}}.
Actual behavior: {{actual}}.
Environment: {{environment}}.
What I've already checked: {{checked|nothing}}.
Name the 3 most likely causes, most likely first. For each, how to check it in a
minute. Don't write a fix until we've found the cause.
{{code}}
Why it works: banning fixes before diagnosis stops the model from "fixing" the wrong thing. "What I've already checked" filters out the obvious advice.
5. Read a stack trace
Analyze this stack trace. Find the first line from my code (not from libraries),
explain what went wrong, and suggest a minimal fix.
{{stack_trace}}
Tests
6. Tests for a function
Write {{framework|Vitest}} tests for the function below.
Cover: the normal case, boundary values, empty input, invalid input.
One assertion per test; test names describe behavior, not implementation.
Mock only network and time.
{{code}}
7. Find uncovered cases
Here's a function and its tests. List the scenarios the tests don't cover,
riskiest first. Write tests for the top three.
Function:
{{code}}
Tests:
{{tests}}
Refactoring and porting
8. Refactor without changing behavior
Refactor this code: {{goal|remove duplication and simplify branching}}.
Don't change behavior or the public interface. Reply with a unified diff only.
After the diff, list risks if there are any.
{{code}}
9. Port to another language
Rewrite this code from {{from|Python}} to {{to|Go}}, idiomatically for the target
language, not line by line. Keep the behavior, including error handling. Flag places
where behavior necessarily differs.
{{code}}
Data and utilities
10. SQL from a description
Database: {{db|PostgreSQL 17}}. Schema:
{{schema}}
Write a query that: {{task}}.
Explain which indexes it needs to be fast at {{size|10M rows}}.
If the task is ambiguous, ask first.
11. A regex, with tests
Write a regular expression ({{flavor|JavaScript}}) for: {{what_to_match}}.
Give 5 strings that should match and 5 that shouldn't.
Explain the expression piece by piece.
12. A commit message
Write a Conventional Commits message for the diff below.
Subject up to 72 characters, imperative mood. Body explains why, not what.
{{diff}}
Common mistakes in coding prompts
Pasting the whole file when the question is about one function. The model spends attention on irrelevant code, and a long answer is harder to check. Give it the function, its types and its call sites — that's almost always enough.
Leaving out the version. "How to do X in React" and "how to do X in React 19 with server components" are different questions. Same for Python 2 vs 3, Postgres 12 vs 17, Next.js Pages vs App Router.
Asking it to "improve the code". Anything can be improved, endlessly. Name the goal: speed, readability, fewer dependencies, compatibility with an old API.
Accepting the first answer. If it looks plausible, ask one follow-up: "what assumptions did you make?" or "where will this code break?". It takes 20 seconds and often finds a problem.
Mixing tasks in one chat. Forty messages in, the model starts confusing old and new versions of the code. When the task changes, start a new chat and carry over only the outcome.
Turning a good prompt into a template
When a prompt works well, don't leave it buried in history. Replace the specifics with variables: language, framework, the code itself. Give a default to whatever is nearly always the same for you:
Before: Write Vitest tests for the parseDate function.
After: Write {{framework|Vitest}} tests for the function below.
That way one prompt works across all your projects, and you only change what actually differs.
How not to lose good answers
A good prompt is half the job. The other half is finding the answer a month later, when you need that same SQL query or regex again. A few habits:
- Give chats descriptive titles. "Review: auth middleware" is easier to find than "Code help".
- One chat, one task. A mix of five topics can't be found or reused later.
- Keep your templates in one place, not spread across notes, messengers and bookmarks.
Chatsavo's search can filter for code (Pro). The query
email regex has:code role:ai
finds AI answers with code blocks that discussed an email regex — in both ChatGPT and Claude (searching all sites is Pro too). Clicking a result scrolls the chat to that message.
A prompt library in the panel, {{name|default}} variables and quick insertion with "/" in the input box are coming in the next updates. The 40 starter prompts for coding, writing and analysis are already openly available at /prompts.
Chatsavo works on ChatGPT and Claude; Gemini, Grok and Alice are in testing; DeepSeek, Perplexity and GigaChat come later. If you often go back to old answers with code, try 7 days of Pro for free — installation.
Find any old chat in a second
Chatsavo adds folders and full-text search to ChatGPT and Claude. Free, and chat text stays in your browser.