Sub topic 10
Explaining the Code Back in Your Own Words
This chapter assumes you can write small Python functions and run them, and that you have already read generated code critically and checked it with your own tests. Here you add one more check before you accept code: can you explain it without looking at it?
Why explaining works
Reading code feels like understanding. The code is on the screen, each line looks reasonable, and you nod along. That feeling is called the illusion of fluency, and it is a poor guide to what you could do alone.
Explaining is a form of retrieval practice. You pull the idea out of memory and put it into words, and gaps show up when you cannot finish a sentence. Researchers call this self-explanation, and it is linked to better transfer to new problems than rereading.
A useful rule is this: if you cannot explain a piece of AI-generated code, you do not own it yet.
Three levels of explanation
Explain the same code at three levels. Each level exposes a different kind of gap.
| Level | Question it answers | Example for `median` |
|---|---|---|
| Purpose | What problem does this solve? | It finds the middle value of a list of numbers. |
| Steps | What happens, in order? | It sorts a copy, finds the middle index, and picks one value or averages two. |
| Reasons | Why was it written this way? | It uses `sorted` so the caller's list is not changed. It raises an error on empty input because no middle exists. |
Most learners stop at the steps. The reasons level is where you find the decisions you would need to make yourself next time.
A worked example
Take the median function.
def median(values):
if not values:
raise ValueError("median() requires at least one value")
ordered = sorted(values)
mid = len(ordered) // 2
if len(ordered) % 2 == 1:
return ordered[mid]
return (ordered[mid - 1] + ordered[mid]) / 2Close the editor or cover the code. Write an explanation from memory in plain sentences:
- The function takes a list of numbers. If the list is empty, it raises an error, because there is no middle.
- It sorts the numbers into a new list, so the original stays unchanged.
- It computes
mid, the index halfway through the sorted list. - If the length is odd, one value sits exactly in the middle, so it returns that value.
- If the length is even, two values share the middle, so it returns their average.
Now compare your explanation with the code. Suppose you wrote "mid is the middle" but could not say why the even case uses mid - 1. That is a gap. Trace a case by hand: for [1, 2, 3, 4], the length is 4, so mid is 2. The two middle values are at indexes 1 and 2, which is mid - 1 and mid. Writing this trace closes the gap with your own reasoning.
Using the AI to check your explanation
Do not ask the AI to explain the code first. Write your own explanation, then ask it to review that. This keeps the AI in the reviewer role.
Here is a function and my explanation of it.
<paste the code>
My explanation:
<paste your explanation>
Do not rewrite my explanation. Tell me which statements are wrong or vague, and ask me one question about any part I skipped.Check the AI's feedback against the code and your tests. It can be wrong, and it may agree with a confident but incorrect explanation.
Common errors
- Paraphrasing the syntax. "It loops over each character" repeats the code. Say what the loop is for.
- Explaining only the happy path. Ask what happens with empty input, one item, or bad types.
- Explaining while looking at the code. You are reading, not retrieving. Hide it first.
- Accepting a vague phrase such as "it handles the edge cases". Name the cases.
Exercise
Exercise
You ask an AI for a function and it gives you count_vowels. You read it and it looks fine. Describe how you would use explaining back to decide whether to accept it, and name one gap that this method can reveal but reading alone often does not.
Show answer
Hide the code and write from memory what it does, step by step, and why. For count_vowels, that includes why text.lower() is called and why the check is ch in "aeiou". Then compare with the code and trace an example by hand, such as "Learning with AI", which gives 6. A typical hidden gap is not knowing why lower() is needed, which means uppercase vowels like the "A" and "I" would be missed without it. Reading makes this look obvious. Explaining from memory shows whether you could have written it.