Lesson 4 of 10 · Own the code AI writes
Ask AI to explain your code, then check the answer
Prompts to get AI to explain code, the ways its explanations go wrong, and three quick checks that what it told you is true.
An AI tool will explain any line of code you show it, patiently, as many times as you ask, at two in the morning. That's a gift for anyone learning.
It's also wrong sometimes, and it never sounds wrong. An explanation that's confident, clear and slightly off is worse than none, because you stop looking. So this lesson is two halves: how to get a good explanation, and how to check it.
Why AI explanations go wrong
They go wrong in a few predictable ways, and knowing them makes them easy to catch:
- It can't see what you haven't shown it. Paste one file into a chat and the AI has to guess what the rest of the project does. It guesses well, and it guesses.
- It explains what the code should do, not what it does. A function called
checkUserIsAdmingets described as checking the user is an admin, even when line 4 has a mistake that lets everyone through. - It knows the versions it learned from. If your project uses a newer version of a library than the AI's training data, it'll describe how the old one worked.
- It agrees with you. Ask "this is safe, right?" and you'll often get a yes. Ask a leading question and you get a led answer.
None of that makes it useless. It makes it a colleague who's quick, well read and occasionally bluffing.
Give it the whole picture
The single biggest improvement is letting the AI see the project, not one pasted piece of it. Tools that work inside your project, such as Claude Code, Cursor or the AI panel in your editor, can open the other files themselves: the route a form sends to, the function a page calls, the version in package.json.
If you're using a chat window such as ChatGPT or Claude, paste more than you think you need: the file, the files it imports from, and the relevant part of package.json.
Prompts to get AI to explain code
Copy these and change the parts in capitals.
Line by line, at your level:
Explain FILE line by line for someone who knows a little JavaScript
but not React. For each line, say what it does and why it's there.
Don't skip the lines you think are obvious.
What it assumes:
What does this function assume about its inputs and about who's
calling it? For each assumption, what happens if it's wrong?
Where it connects:
Which files does FILE depend on, and which files use it?
Trace what happens from the moment a visitor presses the button
to the moment the data is saved. Name each file on the way.
Point at the line:
You said this checks the user is logged in. Quote the exact line
that does that.
That last one is the most useful prompt in this lesson. An explanation that can't point at a line is a guess. When the AI quotes the line, you can read it yourself.
Ask without leading:
Review this code as if you didn't write it. List anything that's
wrong, risky or out of date, with the line for each. If you find
nothing, say so and explain what you checked.
Compare that with "Is this code OK?". The first asks for a search. The second asks for reassurance.
Three ways to check an explanation
1. Run it and look
The fastest check is to make the code tell you what it's doing. console.log prints a value while the code runs:
console.log("status before sending:", status);
Where it prints depends on where the code runs. Code in the browser prints to the browser's console: open the developer tools (F12 on Windows, Cmd+Option+I on a Mac) and choose Console. Code on the server, such as a Next.js Server Component or an API route, prints in the terminal where npm run dev is running. Two wrinkles in Next.js: a Client Component is also drawn once on the server, so its logs can turn up in the terminal as well, and while you develop, logs from Server Components are copied into the browser's console too, marked as coming from the server.
If the AI said status would be "idle" at that point and the console says "error", you've found something the AI got wrong.
Take the console.log lines out when you're done.
2. Read the docs for your version
When the explanation is about how a library works, check the library's own documentation. First find which version you have in package.json, because a library's docs often cover several. A minute on the real docs beats an hour of confident guessing.
3. Ask again, fresh
Start a new conversation, or use a different tool, and ask the same question without the earlier answer in front of it. If the two explanations agree and both point at the same lines, you can lean on them more. If they disagree, you've found exactly the part you need to read yourself.
Make the AI your teacher, not your answer
There's a way of using AI that makes you better and a way that keeps you where you are. The difference is who does the thinking first.
- Write down what you think the code does, then ask. Compare.
- Ask for a hint, not the answer: "Don't fix it. Tell me which line to look at."
- Ask it to quiz you: "Ask me five questions about this file to check I understand it."
- When it explains something new, ask for the one-line version, then put it in your own words in your notes.
If you're learning the trade, this habit will show in an interview and in your first job. Anyone can paste an explanation. Being able to say it yourself is the skill.
If you built something with AI
What to check
- Pick the file in your app that handles payments, logins or personal data. Ask the AI to explain it line by line, then ask it to quote the exact lines that check who the user is. If it can't point to them, find out whether they exist.
- Ask the AI what your app assumes about the people using it. Check each answer against the code.
- Find out which version of your main framework you're on (
next,reactorviteinpackage.json), and ask the AI whether its advice applies to that version. - Turn the map you made in lesson 2 into a
CLAUDE.md(orAGENTS.md, which other tools read), so the AI starts every session knowing the project.
If you're learning the trade
Try it
Take the lines you couldn't explain from the last lesson's exercise.
- Ask an AI to explain each one, using the line-by-line prompt above.
- For each explanation, check it one of the three ways: run it with
console.log, read the docs, or ask again in a fresh session. - Write down any place where the explanation was wrong or vague, and what was actually true.
Then do one more thing: break the component on purpose. Change one line, predict what will happen, and run it. Ask the AI to predict it too, before you run it. Keep score.