# What does a good prompt look like for a developer?

Date: 2023-09-11 · Author: Robert Scales · https://guidetoartificialintelligence.com/en/learn/good-prompt-look-like-developer/

**Answer:** A good coding prompt reads like a small ticket: the language and version, the surrounding code, the exact interface, the constraints, the test cases that define "done", and a request to explain the choices and list any assumptions. The more of your project's reality you put in, the less generic code you get back.

"Write a function that parses durations" will get you a function. It may be in the wrong language version, pull in a library you don't use, handle formats you never see and miss the ones you do. The model is not being careless; it simply fills every gap you leave with the most common choice it has seen. A good developer prompt closes those gaps the way a well-written ticket does.

*A Beginner's Guide to ChatGPT* makes the point with a simple progression. "Help me fix this code" gets little useful help. Naming the language and the kind of problem gets a focused answer. Asking for a review of specific qualities, with the reasoning behind each suggestion, gets the most useful reply. The same progression applies to writing new code.

## What a developer prompt should contain

1. **Environment.** Language, version, runtime and framework: "Node.js 18, plain JavaScript, ES modules."
2. **Context.** The code around the change, or at least the types and function signatures it must fit.
3. **The interface.** Function name, parameters, return value and how errors are reported.
4. **Constraints.** No new dependencies, performance limits, browser support, house style.
5. **Acceptance tests.** Concrete inputs and expected outputs that you write yourself.
6. **Explanation.** Ask for a short note on design choices and a list of assumptions.

The last two parts do the most work. Your test cases turn a vague goal into a precise one, and a list of assumptions shows you where the model guessed.

## A worked example: a duration parser

Suppose your app lets users type video lengths like `1h30m` and you need seconds.

```prompt
You are a senior JavaScript developer reviewing and writing production code.

Environment: Node.js 18, plain JavaScript (no TypeScript), ES modules. No new npm dependencies.

Task: write an exported function parseDuration(text) in a file duration.js.
- Accepts strings made of hours, minutes and seconds in that order, each optional but at least one present: "2h", "45m", "1h30m", "1h5m20s". Allow surrounding spaces and upper or lower case.
- Returns the total number of seconds as an integer.
- Throws a RangeError with a clear message for anything else, including "", "1.5h", "30m1h" and "-5m".

Acceptance tests (these must pass):
parseDuration("1h30m") === 5400
parseDuration(" 45M ") === 2700
parseDuration("1h5m20s") === 3920
parseDuration("30m1h") throws RangeError

After the code, write the same tests using Node's built-in test runner (node:test and node:assert). Then list any assumptions you made and explain your parsing approach in three sentences.
```

Notice what the prompt rules out. Naming Node 18 and its built-in test runner prevents a reply that installs a test framework you don't use. Listing invalid inputs forces a decision about decimals and order instead of leaving it to chance. And asking for assumptions might surface something you hadn't decided, such as whether `0h` is valid.

## Two more developer tasks, two more prompts

**Refactoring without changing behavior.** Paste the function and its tests, then add: "Refactor for readability. Keep the public interface and every test passing. Do not change behavior, even for edge cases. Show the diff and explain each change in one line." The tests act as a lock on behavior.

**Reviewing your own code.** Paste a function and ask for specific checks rather than general advice: "Review this for input validation, error handling and performance on lists of 100,000 items. For each issue, quote the line, explain the risk and suggest a fix. Say 'no issue found' for any category that is fine." Naming the categories keeps the review on what matters to you.

> **Try it:** Take a small function you wrote recently. Write the prompt as if you were asking for it from scratch, including the environment, interface, constraints and four test cases. Compare the model's version with yours, and note which of its assumptions you would have answered differently.

## How do you get code you can trust?

Treat every answer as a pull request from a colleague you have just met: useful, often good, and never merged without review. The earlier article on whether ChatGPT can write trustworthy code walks through the test-and-fix loop; the habits below come before and after it.

**Own the tests.** Write the acceptance tests yourself, from your requirements, before you read the generated code. Tests the model writes for its own code can simply confirm what it already did, mistakes included.

**Run the code somewhere real.** ChatGPT Plus subscribers can use Code Interpreter, which OpenAI rolled out in July and now calls Advanced Data Analysis. It lets ChatGPT actually run code and show the result, which catches some mistakes early. It is a sandbox, though, not your project: for anything that depends on your versions and configuration, run the tests in your own environment.

**Check every name against the docs.** Methods, options and packages can look right and not exist, or exist only in another version. Look up anything unfamiliar in the official documentation for the version you named.

**Keep changes small.** Ask for one function or one change at a time, so each diff is short enough to read line by line.

**Mind what you paste.** Remove API keys, passwords, customer data and anything your employer treats as confidential before you share code. Read your company's policy on AI tools; some teams use business plans with different data terms, and others don't allow pasting code at all.

## Sources

- A Beginner's Guide to ChatGPT: https://www.amazon.com/dp/B0BZRRPXZW
- ChatGPT — Release Notes: https://help.openai.com/en/articles/6825453-chatgpt-release-notes
- Introducing ChatGPT Enterprise: https://openai.com/index/introducing-chatgpt-enterprise/
