Sub topic 1
Start with a specific question
Before you paste code into an assistant, decide what you want to know. A vague request gets a vague answer. A specific question gets an answer you can check.
Prerequisites
This chapter assumes you can:
- Read code in at least one programming language.
- Use a chat-based assistant, or an assistant in your terminal or editor.
- Run small code snippets on your own machine.
Why "explain this code" is not enough
"Explain this code" leaves the assistant to guess what you already know, what you need, and how much detail you want. It usually guesses with a generic summary. That summary may restate what you can already see and skip the one line that confused you.
A specific question narrows the answer to your actual gap. It also gives you something to test the answer against. If you ask "why is this decorator here?", you can tell whether the reply addresses the decorator.
Find the gap
Read the code once before you ask anything. Mark what you recognize and what you don't. Then name the gap in one sentence.
Consider this snippet from a data-processing project:
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 follow the recursion. The unfamiliar part is @lru_cache(maxsize=None). Your gap is not "what does this code do?" It is closer to "what does this decorator do to the function, and why would someone add it here?"
Choose the kind of question
Most questions about unfamiliar code fall into a few types. Pick one before you write the prompt.
| Question type | Example | What you get back |
|---|---|---|
| Summary | What does this function do overall? | A short description of purpose and result |
| Walkthrough | What happens, step by step, when I call `fib(5)`? | A traced sequence of calls and values |
| Rationale | Why is `lru_cache` used here instead of a plain dictionary? | The reasoning behind a design choice |
| Term or API | What does `maxsize=None` mean? | A definition tied to documented behavior |
You can combine types, but keep the list short. Two or three focused questions work better than one long request that tries to cover everything.
Turn the gap into a question
Use this pattern. State what you know, name what you don't, and say what form of answer you want.
- Say where the code comes from and what you are trying to do with it.
- Name the exact construct or behavior you don't understand.
- State the type of answer you want: summary, walkthrough, or rationale.
Applied to the example:
- Weak: "Explain this code."
- Better: "What does
@lru_cache(maxsize=None)do tofib?" - Best: "I'm reading a Python utility and I don't know
@lru_cache. What does it do tofib, and why is it there? Walk throughfib(5)."
The last version tells the assistant your situation, the gap, and the shape of the answer. The next chapters cover how to supply context and structure the full prompt. For now, the goal is only the question.
Write down what would count as an answer
Add one more step. Before you send the question, decide how you would know the answer was right. For the fib example, a good test is the cache itself. If the assistant says results are stored, you can run the code and look at the cache statistics:
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())If the claim "each value is computed once" is true, the output should show 31 misses (one for each of 0 through 30) and 28 hits. Having a check in mind keeps you from accepting a fluent but wrong answer. A later chapter covers verification in detail.
Common mistakes
- Asking about the whole file when one line is the problem.
- Asking several unrelated questions in one message.
- Leaving out the type of answer you want, so you get a summary when you needed a walkthrough.
- Skipping the step where you decide how you will check the answer.
Exercise
You find @lru_cache(maxsize=None) above a function in a project you inherited. You already understand the function body. Write one specific question about it, and name the question type from the table.
Show answer
One good answer: "Why does this function use @lru_cache(maxsize=None), and what happens to memory if it is called with many different arguments?" This is a rationale question, and it also touches a term or API question (maxsize=None). A weak answer such as "Explain this function" fails because it doesn't name the gap.
Exercise
Before asking an assistant about fib, you want a way to check its claim that results are cached. What would you run, and what output would support the claim for fib(30)?
Show answer
Run fib(30) and then print fib.cache_info(). Supporting output shows 31 misses and 28 hits, which means each value from 0 to 30 was computed once and reused afterward.