Sub topic 2
Start with a specific, testable goal
This chapter assumes you can write and run small programs in one language, run a test suite, and have used a chat or editor-based AI assistant. It covers the first item in a good prompt: the goal.
Why the goal comes first
The assistant only sees the text you send. If the goal is vague, it fills the gaps with guesses, and each guess is a place where the result can differ from what you wanted.
Compare two requests:
Weak prompt:
Write a function to clean up titles."Clean up" could mean trimming spaces, fixing capitalization, removing punctuation, or turning the title into a URL slug. The assistant must pick one, and you will only find out whether it picked correctly after reading the code.
A good goal removes that guess. It says what the code does, in terms you could check.
What makes a goal testable
A goal is testable when you can write down, before seeing any code, how you would decide that it works. Three parts help:
- A named unit of work. Give a function name, a signature, or a file. "A function
slugify(text: str) -> str" is better than "a helper." - A defined input and output. State what goes in and what comes out.
- A pass condition. Describe a behavior you can observe, such as "returns
hello-worldforHello, World!."
Here is the weak goal rewritten:
Write a Python 3.10 function slugify(text: str) -> str that converts an article title into a URL slug.This names the language, the function, the types, and the purpose. You can already imagine several test cases for it. Context and constraints will make it tighter in later chapters. For now, the goal alone is much stronger than "clean up titles."
Words that signal a vague goal
Some words feel like requirements but cannot be checked. Replace them with observable behavior.
| Vague wording | Testable wording |
|---|---|
| "Make it better" | "Reduce the function to one pass over the list" |
| "Clean up the data" | "Drop rows where `email` is empty and strip whitespace from `name`" |
| "Handle errors" | "Raise `ValueError` if `text` is not a string" |
| "Make it fast" | "Finish 100,000 items in under one second" |
| "Fix the bug" | "`parse_date('2024-02-29')` currently raises; it should return a `date`" |
If you cannot rewrite a phrase this way, you probably have not decided what you want yet. That is useful to learn before you spend time on a response.
Write the test idea first
You do not need a full test file at this stage. One or two sentences about how you would check the result are enough. Say them to yourself, or put them in the prompt:
Write a Python 3.10 function slugify(text: str) -> str that converts an article title into a URL slug.
It should return "hello-world" for "Hello, World!" and never return a string that starts or ends with a hyphen.The second sentence turns a description into two checks you can run. Later chapters expand this into full input and output examples and a test file.
One goal per prompt
A prompt that asks for several unrelated things gets a response that does each one less carefully, and it is harder to tell which part failed. Keep one main goal per prompt. If the work has several parts, split it into steps, which a later chapter covers.
Common errors
- Describing the problem area instead of the task. "I'm working on a blog tool" is context, not a goal. Add the task as well.
- Asking for a judgment with no criteria. "Is this code good?" has no pass condition. Ask "List any input for which this function raises an exception."
- Hiding the goal in the middle of a long message. State it first, in one sentence.
- Combining the goal with a hope. "Write a fast, clean, general function" lists qualities the assistant cannot measure. Pick the ones you can check and state them.
Checking your own goal
Before sending a prompt, read your goal and ask three questions:
- Could two developers read this and write code that behaves differently on the same input?
- Can I name at least one input and the output I expect?
- Is there only one main thing being asked for?
If the answer to the first is yes, or to the second or third is no, rewrite the goal before sending it.
Exercise
Rewrite this goal so it is specific and testable: "Make my login code more secure." Name the unit of work and at least one observable pass condition. Assume a Python function check_password(user, password).
Show answer
One acceptable answer: "Change checkpassword(user, password) so it compares the hash of password with user.passwordhash using hmac.compare_digest instead of ==. It should return True for the correct password and False for a wrong one, and it must not raise an exception when password is an empty string." This names the function, the method, and checkable behaviors. Any rewrite that names the function and gives at least one observable condition is correct.
Exercise
Which of these goals is testable, and why? (a) "Write a good parser for config files." (b) "Write parse_config(path: str) -> dict that reads a file of key=value lines and returns a dict; blank lines are ignored."
Show answer
(b). It names the function and signature, defines the input and output, and states behavior you can check, such as a blank line producing no key. (a) uses "good" and "config files" without any format or pass condition.