Coding Prompts
Ready-made prompts that speed up reading code, finding bugs and writing tests, for beginners and experienced developers.
Ready-made prompt templates
- Explain code line by line: Explain the following code line by line, as if teaching a beginner:
[paste your code here]
Sample case: Code: const total = items.reduce((sum, item) => sum + item.price * item.qty, 0);
1. `items.reduce(...)`: Walks through each item and reduces them to a single value; here that value is the total amount.
2. `(sum, item) => ...`: On each step `sum` holds the total so far and `item` the current product.
3. `sum + item.price * item.qty`: Multiplies the item's price by its quantity and adds it to the total.
4. `, 0`: The starting value; the total begins at 0. If the list is empty the result is 0.
5. `const total`: Stores the result in a variable that cannot be reassigned.
This sample output was produced with Claude; your result may differ. - Find the bug: There's a bug in this function but I can't find it. Review the code and list possible issues:
[paste your code here]
Sample case: Code: function average(arr) { let sum = 0; for (let i = 0; i <= arr.length; i++) { sum += arr[i]; } return sum / arr.length; }
Likely problems:
1. The loop condition `i <= arr.length` is wrong: on the last turn `arr[arr.length]` is undefined, so the sum becomes NaN. It should be `i < arr.length`.
2. If the array is empty, `sum / arr.length` is 0/0 and returns NaN; handle that case separately.
3. If the array contains a non-number the result breaks; validate the input.
This sample output was produced with Claude; your result may differ. - Improve the code: Suggest improvements to make this code more readable and performant, without changing its logic:
[paste your code here]
Sample case: Code (Python): squares = [] for n in numbers: if n % 2 == 0: squares.append(n * n)
Suggestion: the list can be built with a single expression.
squares = [n * n for n in numbers if n % 2 == 0]
The logic is the same: only the squares of even numbers are taken. It reads better and removes the repeated `append` calls. If the condition gets complicated, prefer the original loop for readability.
This sample output was produced with Claude; your result may differ. - Code review: Review the following code snippet, list possible bugs and improvement points, then suggest a cleaner version:
[paste your code here]
Sample case: Code: var x = 5 function check(a) { if (a == '5') { return true } else { return false } }
Likely problems:
1. Use `let` or `const` instead of `var`; it avoids scope issues.
2. `==` performs type coercion; `5 == '5'` is true. Use `===` if that is not intended.
3. Returning true/false through `if/else` is unnecessary; return the condition directly.
Cleaner version:
const x = 5;
function check(a) { return a === '5'; }
This sample output was produced with Claude; your result may differ. - Explain an error message: Explain the following error message and list its likely causes in plain language: [paste error message]. Language/framework I'm using: [language/framework].
Sample case: Error: TypeError: Cannot read properties of undefined (reading 'name'). Language: JavaScript (React).
Meaning: Your code tried to read the `name` property but the object before it was `undefined`.
Likely causes:
1. The data has not loaded yet (the component rendered before the API response arrived).
2. A wrong property path, for example `user.profile.name` when `profile` does not exist.
3. Accessing an index that is not in the array (`users[3].name`).
Ways to fix: use optional chaining like `user?.name` or show a loading state until the data arrives. Check the stack trace in the console to see which line caused it.
This sample output was produced with Claude; your result may differ. - Fix the error: Look at the following error message and code. Explain the likely cause and show the corrected code:
Error: [error]
Code:
[code]
Sample case: Error: IndexError: list index out of range Code: def last(items): return items[len(items)]
Why: In Python indexes start at 0; the last item's index is `len(items) - 1`. `items[len(items)]` points outside the list.
Fixed code:
def last(items):
if not items:
return None
return items[-1]
`items[-1]` gives the last item. It also returns `None` for an empty list so no second error occurs.
This sample output was produced with Claude; your result may differ. - Write unit tests: Write unit tests for the following function. Cover normal cases, boundary values and invalid input:
[function]
Sample case: Function (Python): def divide(a, b): if b == 0: raise ValueError('b cannot be zero') return a / b
import pytest
from mymodule import divide
def test_normal():
assert divide(10, 2) == 5
def test_negative():
assert divide(-9, 3) == -3
def test_float():
assert divide(1, 4) == 0.25
def test_zero_numerator():
assert divide(0, 5) == 0
def test_zero_divisor():
with pytest.raises(ValueError):
divide(5, 0)
Coverage: normal case, negative number, fractional result, a boundary value (zero numerator) and invalid input (division by zero). Change the module name to match your file.
This sample output was produced with Claude; your result may differ. - Write a SQL query: Write a SQL query that does this. Tables and columns: [schema]. Desired result: [result]. Explain the query line by line.
Sample case: Tables: customers(id, name), orders(id, customer_id, total, created_at). Wanted: the top 5 customers by spending in 2026.
SELECT c.name, SUM(o.total) AS total_spent
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.created_at >= '2026-01-01' AND o.created_at < '2027-01-01'
GROUP BY c.id, c.name
ORDER BY total_spent DESC
LIMIT 5;
Explanation:
1. JOIN matches customers with their orders.
2. WHERE keeps only 2026 orders.
3. GROUP BY collects each customer's orders into one row.
4. SUM computes total spending.
5. ORDER BY ... DESC sorts from highest, LIMIT 5 takes the first five.
Note: LIMIT syntax varies by database (TOP 5 in SQL Server).
This sample output was produced with Claude; your result may differ. - Write a regex: Write a regular expression for this pattern. Should match: [examples]. Should not match: [examples]. Explain each part of the expression.
Sample case: Should match: 0532 123 45 67, 05321234567. Should not match: 0532 123 45, 1234.
^05\d{2} ?\d{3} ?\d{2} ?\d{2}$
Parts:
^ and $: start and end of the text; the pattern must fit the whole text.
05: starts with a fixed 05.
\d{2}: two digits (rest of the operator code).
?: an optional space.
\d{3}, \d{2}, \d{2}: the remaining digit groups.
This pattern accepts 11-digit numbers with or without spaces; it does not cover the international prefix (+90).
This sample output was produced with Claude; your result may differ.
Coding Prompts – how to use them
The most effective approach in coding prompts is to give complete context: the language and version, the full error message, and the expected versus actual behavior. Instead of “my code does not work”, give the relevant code, the error and what you are trying to do, and the AI finds the cause far more accurately.
Read and test code the AI gives you before running it; it may use a function that does not exist or contain a security hole. Never paste passwords, API keys or confidential customer data into a prompt. If you work with company code, follow your organization’s AI use rules.
- Include the language, version and the full error message.
- Describe expected and actual behavior separately.
- Test the code before running it for real.
- Never paste passwords or API keys.
Example: a weak prompt and a strong prompt
Weak prompt
My code does not work.
Strong prompt
Review the error message and code below. Explain the likely cause and show corrected code: Error: TypeError: cannot read properties of undefined (reading ‘length’). Code: function average(list){ return list.reduce((a,b)=>a+b,0)/list.length } average()
Sample output generated with Claude
Cause: you call the function with no argument (average()), so list is undefined and list.reduce or list.length fails.
Fix:
function average(list = []){
if(list.length === 0) return 0;
return list.reduce((a,b)=>a+b,0)/list.length;
}
average([4, 8, 6]); // 6
I added a default parameter and an empty-list check. You may prefer to throw an error for an empty list instead of returning 0; that is a design decision for you.
This output is an example from running the strong prompt above with Claude. Results can differ in ChatGPT, other tools or at other times; check the output yourself before using it.
Frequently asked questions
Can I use AI-generated code directly?
Read and test it first. It may use a function that does not exist, outdated syntax or contain a security hole.
Is it safe to paste my private code?
Never paste passwords, API keys or customer data. For company code, check your organization’s AI use rules.
Which languages does it support?
ChatGPT and Claude work well with most popular languages. They can slip on very rare libraries or brand-new versions; verify against the official docs.
Other prompt categories
- The 20 Most Popular Prompts
- Planning & Productivity Prompts
- Business Writing Prompts
- Editing, Summary & Translation Prompts
- Learning & Study Prompts
- Communication & Hard Conversation Prompts
- Resume & Interview Prompts
- Decision-Making & Problem-Solving Prompts
- Everyday Life & Shopping Prompts
- Content Creation Prompts
- Social Media Prompts
- Marketing Prompts
- SEO & Blog Writing Prompts
- E-Commerce Prompts
- Summarizing Prompts
- Data Analysis Prompts
- Image & Design Prompts
- Presentation Prompts
- Travel Planning Prompts
- Prompts for Teachers
- YouTube and Video Prompts
- Startup and Small Business Prompts
- Petition and Official Letter Prompts
- HR and Recruiting Prompts
- Creative Writing Prompts