Sub topic 1
Start with a specific question
Most unhelpful explanations of code come from vague questions. "Explain this code" gives an assistant no way to know what you are missing, so it explains everything at an average level. This chapter shows how to decide what you need to understand before you write any prompt. It covers the common kinds of questions, how to narrow a broad one, and how to tell when you have a question worth asking.
Prerequisites
This chapter assumes you have the following:
- Basic ability to read code in at least one programming language.
- Experience using a chat-based AI assistant, or a terminal or editor assistant.
- Comfort running small code snippets locally.
You do not need to know Python well. The examples use Python, but the method applies to any language.
Why the question comes first
An assistant answers the question you ask. If the question is broad, the answer is broad. You then have to read a long explanation to find the one part you needed, and you may not recognize that part when you see it.
Compare two requests about the same snippet:
- "Explain this code."
- "I don't know what
@lru_cachedoes. Why is it on this function?"
The first gets a line-by-line tour of things you probably already know. The second gets an answer about the one thing you came for.
Writing the question also helps you. Often you find that you understand more than you thought, and the gap is one word or one line.
The running example
Suppose you are reading a Python utility in a data-processing project. You find this function:
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)You can read most of it. It is a recursive Fibonacci function. The unfamiliar part is the line starting with @. That is the gap, and the gap is what you should ask about.
Find the gap
Before prompting, locate exactly where your understanding stops. Read the code once and mark each part as one of three things:
- Understood. You could explain it to someone else.
- Guessed. You have a theory but are not sure.
- Unknown. You do not recognize the syntax, name, or idea.
For the snippet above, the marking might look like this:
| Part | Status | Note |
|---|---|---|
| `def fib(n)` | Understood | Ordinary function |
| `n if n < 2 else ...` | Understood | Conditional expression |
| `fib(n - 1) + fib(n - 2)` | Understood | Recursion |
| `@lru_cache(...)` | Unknown | Never seen this |
| `maxsize=None` | Guessed | Maybe "no limit"? |
The "Guessed" rows are useful. A guess lets you ask a sharper question: "I think maxsize=None means the cache has no limit. Is that right, and what happens with a limit?" The assistant can confirm or correct your guess instead of starting from nothing.
Kinds of questions
Questions about unfamiliar code usually fall into a few types. Naming the type tells you what to ask for.
| Type | What you want | Example |
|---|---|---|
| What | The meaning of a construct | "What does `@lru_cache` do?" |
| How | The step-by-step behavior | "Walk through `fib(5)`. What gets called, and in what order?" |
| Why | The reason the code was written this way | "Why is a cache on a recursive function?" |
| When | The conditions and edge cases | "What happens if `n` is negative, or not an integer?" |
| What if | The effect of a change | "What would change if I removed the decorator?" |
A single prompt can contain more than one type, but each should be explicit. If you only ask "what", the assistant will not volunteer a walkthrough or the history behind a design choice.
Narrow a broad question
Sometimes your first question is too large. "How does this module work?" is a real need, but it is not a question an assistant can answer well in one reply. Break it down.
Start from the outside
Ask about the purpose of the whole piece first, in one or two sentences. Then pick the part that is least clear and ask about that. You are narrowing in steps, and each answer tells you where to aim the next question.
Replace vague words
Vague words hide the gap. Replace them with specifics:
| Vague | Specific |
|---|---|
| "How does this work?" | "How does the second call to `fib(30)` avoid recomputing values?" |
| "What is this doing?" | "Which line modifies the shared list, and when?" |
| "Is this good?" | "What breaks if two threads call this function at once?" |
| "I don't get it." | "I follow up to the `yield`, but I don't see when the function resumes." |
Say what you already know
State what you understand and where you got lost. This does two things. It stops the assistant from repeating basics, and it lets the assistant spot a wrong assumption in your mental model.
I understand that fib is recursive and that it returns n for n < 2.
I don't understand how @lru_cache(maxsize=None) changes the behavior.Decide what a good answer looks like
Before you send the question, picture the answer you want. Ask yourself:
- Do I want a short definition, or a walkthrough with values?
- Do I need to make a decision from this, such as whether to keep, change, or delete the code?
- Will I have to explain this to someone else?
The decision shapes the question. If you must decide whether it is safe to delete the decorator, "what does it do?" is not enough. You need "what would change in behavior and in performance if I removed it?"
A question you can verify is better than one you cannot. "Does fib(30) call the function body 31 times or about 1.3 million times?" has an answer you can check by running the code. "Is this elegant?" does not.
A worked example
Here is a weak prompt and a stronger one for the same snippet.
Weak:
Explain this code.
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)Stronger:
I'm reading a Python utility and I don't know the @lru_cache decorator.
I understand the recursion in fib. What I don't understand is what the
decorator changes about how the function runs, and why maxsize=None is used.
Walk through fib(5) and show which calls are served from the cache.The stronger prompt states the situation, names the gap, says what is already understood, and asks for a specific kind of answer. Later chapters cover how to add context and structure. For now the point is that the question is already clear before any of that.
Check your question against the code
You can often test whether a question is well formed by seeing whether you could verify the answer. Run this version of the snippet:
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
print(fib(30))
print(fib.cache_info())The output is:
832040
CacheInfo(hits=28, misses=31, maxsize=None, currsize=31)Thirty-one misses means the function body ran once for each value from 0 to 30. Twenty-eight hits means repeated calls were answered from the cache. If you had asked "does the decorator avoid recomputation?", this output answers it. A question that leads to a check like this is a good question.
Common mistakes
- Asking for everything. "Explain the whole file" produces a shallow answer. Choose one function or one concept.
- Hiding the real question. If you want to know whether code is safe to change, say so. Do not ask only what it does.
- Skipping your own reading. If you have not read the code first, you cannot say what you do not understand.
- Asking about code you have not shown. Questions like "why does this fail?" need the code and the error. A question about unseen code gets a guess.
- Accepting the first framing. If the answer addresses a different question than the one you had, rewrite the question and ask again.
How this fits the rest of the process
Defining the question is the first step. The next steps build on it: gather the context the assistant needs, protect anything sensitive, structure the prompt, read the answer critically, ask follow-ups, and verify.
The loop matters. If the answer does not address your gap, the usual fix is not a longer prompt. It is a sharper question.
Summary of the method
- Read the code yourself first.
- Mark each part as understood, guessed, or unknown.
- Pick one gap. Write it as a single question.
- Choose the question type: what, how, why, when, or what if.
- State what you already know.
- Imagine an answer you could check by running or reading something.
Exercise
You find this line in a project: sorted(items, key=lambda x: x[1], reverse=True). You know sorted and reverse. You are unsure about key=lambda x: x[1]. Write a specific question for an assistant, and name its type (what, how, why, when, or what if).
Show answer
A good answer names the gap and states what you know, for example: "I know sorted and reverse=True. What does key=lambda x: x[1] do, and if items is a list of tuples, which tuple element is used for ordering?" This is a "what" question with a "how" element. A weak answer such as "Explain this line" does not locate the gap.
Exercise
Mark each part of this snippet as understood, guessed, or unknown from the point of view of a reader who has never used decorators, then write one question about the biggest gap. The snippet is the fib function with @lru_cache(maxsize=None). Finally, name one thing you could run to check the answer.
Show answer
Sample marking: def fib(n), the conditional, and the recursion are understood. @lru_cache is unknown. maxsize=None is a guess. Sample question: "What does the @lrucache decorator change about how fib runs, and what does maxsize=None mean?" To check the answer, run fib(30) and print fib.cacheinfo(). The output shows 31 misses and 28 hits, which confirms that results are cached.