# Can ChatGPT write code you can trust?

Date: 2022-12-04 · Author: Robert Scales · https://guidetoartificialintelligence.com/en/learn/chatgpt-write-code-trust/

**Answer:** ChatGPT can write working code for many small, well-described tasks, but you cannot trust it on sight, because it predicts likely-looking code without running it. Treat every snippet as a draft from a fast junior colleague: read it, test it, and only then use it.

In its first days online, ChatGPT has become a favorite toy for programmers. The Guardian reports people using it to solve coding puzzles in obscure languages within seconds. The Verge collected posts from testers who called its answers to coding questions perfect, next to a post from someone who called one of those same answers garbage. Both reactions can be true at once, and understanding why is the key to using it well.

ChatGPT is a language model: a program trained to predict the next piece of text. When you ask for a function, it writes the kind of code that usually follows a request like yours. It does not compile that code, run it or check the result before showing it to you. So the code can look tidy, come with a confident explanation, and still be wrong.

## Why good-looking code can still be wrong

OpenAI is frank about this. Its launch post says the model sometimes produces answers that sound plausible but are incorrect, and that it usually guesses what you meant when a request is ambiguous instead of asking. In code, those two habits show up as:

- **Edge cases left out.** The function works for the obvious input and breaks on an empty list, a negative number or a blank line.
- **Silent assumptions.** You meant inches and it assumed centimeters; you meant to keep duplicates and it removed them.
- **Invented details.** A function or option name that sounds real but does not exist in the library you use.

The good news is that code, unlike an essay, can be checked mechanically. A test either passes or fails. That makes programming one of the safer places to use ChatGPT, as long as you actually run the tests.

OpenAI's own launch examples point in the same direction. In one sample, a user pastes some code and says it does not work, and ChatGPT first asks what the code is supposed to do before suggesting a likely problem. A good conversation about code is a back-and-forth, not a single request.

## A worked example: request, tests, fix

Here is a small task in Python. The replies described below are illustrative, showing the kind of answer you are likely to get, not a transcript.

**Step 1: ask for the function, with the rules spelled out.**

```prompt
Write a Python function median(numbers) that returns the median of a list of numbers.
Rules:
- Do not change the list that is passed in.
- For an even number of items, return the average of the two middle values.
- For an empty list, raise a ValueError with a clear message.
Add a short docstring.
```

Suppose the first version sorts the list in place with `numbers.sort()`. It returns the right median, but it quietly reorders the caller's list, breaking your first rule. Nothing in the explanation would warn you.

**Step 2: ask for tests, separately.**

```prompt
Now write pytest tests for median(). Cover: an odd-length list, an even-length list, a single item, negative numbers, an empty list (expect ValueError), and a check that the original list is unchanged after the call.
```

**Step 3: run them.** Save the function in `stats.py`, the tests in `test_stats.py`, install pytest if needed (`pip install pytest`) and run `pytest` in that folder. In our illustrative case, five tests pass and the "list is unchanged" test fails.

**Step 4: send the failure back.**

```prompt
The test test_does_not_modify_input fails: the list is reordered after calling median(). Fix the function without changing the tests.
```

The usual fix is to work on a sorted copy, `values = sorted(numbers)`, instead of sorting in place. Run the tests again. When all six pass, you have something you can trust for the cases you thought of.

| Step | What you do | What it catches |
|---|---|---|
| Clear request | List the rules and edge cases | Vague guesses |
| Separate tests | Ask for tests that match your rules | Missing behavior |
| Run them yourself | Execute the code on your machine | Bugs the explanation hid |
| Paste the failure back | Quote the exact error | Wandering "fixes" |

> **Try it:** Pick a function you already wrote and understand. Ask ChatGPT to write its own version, then ask it for tests, and run its tests against both versions. Note where the two disagree.

## How do you test the code it gives you?

Start by writing down, in plain words, what the code must do and what it must never do. Those sentences become your test cases. Ask ChatGPT to turn them into tests, but read each test before running it: a test with the wrong expected answer will happily approve a wrong function. If you can, write one or two tests yourself first, for the cases you care most about.

Then run everything on your own machine or in a safe sandbox, never on live data. For a script that deletes, moves or sends anything, test on copies first. Watch for library names you have never seen; look them up in the official documentation before installing anything a chatbot suggests.

Finally, keep the conversation focused. Paste the exact error message or the failing test, and ask for the smallest change that fixes it. If the answers start going in circles, start a fresh chat with the current code and your test list. Many expect tools like this to become a normal part of programming, and the habit that will matter most is the oldest one in the trade: do not ship what you have not tested.

## Sources

- ChatGPT: Optimizing Language Models for Dialogue: https://openai.com/blog/chatgpt/
- OpenAI’s new chatbot can explain code and write sitcom scripts but is still easily tricked: https://www.theverge.com/23488017/openai-chatbot-chatgpt-ai-examples-web-demo
- AI bot ChatGPT stuns academics with essay-writing skills and usability: https://www.theguardian.com/technology/2022/dec/04/ai-bot-chatgpt-stuns-academics-with-essay-writing-skills-and-usability
