Blog / Build with AI /

How to ask AI to explain code line by line

You paste a snippet from a tutorial, or from ChatGPT itself, into your site. It works. A week later something changes and you realise you have no idea what that code does. If it was a stranger’s code, you’d be nervous. It’s the same when the stranger is an AI.

The fix isn’t to avoid generated code. It’s to make a habit of asking for an explanation before you use it. Models are good at this, if you ask well. Here’s how.

The basic prompt

The weakest version is: “Explain this code.” You’ll get a summary, usually pitched at the wrong level.

A better one:

Explain this code line by line. I’m a beginner who knows basic HTML but not JavaScript. For each line or block, say what it does in plain English and why it’s needed. Use short sentences. At the end, tell me what the whole thing does in two sentences.

Three details are doing the work:

  1. Your level. It changes the vocabulary, the analogies and the depth.
  2. Line by line. You get structure, not a vague overview.
  3. A two-sentence summary. It checks that you’ve got the big picture, not just the details.

Prompts for different needs

Different situations call for different questions.

“What does this touch?” The one I’d use before running anything new:

List everything this code does outside itself: files it reads or writes, websites or servers it contacts, data it stores, and anything it deletes or overwrites. If it does none of these, say so.

This is a safety question, and it often surfaces something surprising: a network call you didn’t ask for, a file saved to a place you didn’t expect.

“What could go wrong?”

What inputs or situations would break this code? Give me five, with an example for each.

You get ready-made test cases. Run them.

“Explain the odd line.” When one bit confuses you, quote it:

In this line: const total = items.reduce((sum, i) => sum + i.price, 0); what does reduce do, and what’s the 0 at the end for? Use an everyday analogy.

(For the record: reduce walks through a list and combines the items into one result, here a running total, and the 0 is the starting value. Like adding up a bill from zero.)

“Teach me the concept.”

I keep seeing the word “callback”. Explain it with a restaurant analogy, then show me a three-line example.

“Quiz me.” To check you’ve understood:

Ask me three questions about this code, one at a time. Wait for my answer before continuing, then tell me whether I’m right.

That last style turns a lecture into a conversation, and it’s a good way to learn.

For people who work with WordPress

If you work with WordPress, you’ll constantly meet snippets for functions.php. A useful prompt:

This is a WordPress snippet for functions.php. Explain each line, tell me which WordPress hooks it uses, and say whether it would still work if I switched themes.

The last question matters. Code added to a theme’s functions.php disappears when you change the theme, while code in a plugin stays. It’s a very common surprise.

Ask for the explanation in the code itself

When you request new code, add: “Add a comment above every function and every non-obvious line explaining what it does.” You get an explanation that travels with the file. Later, when you open it again, you won’t be starting from scratch. It’s also a way to notice trouble: if the comment says one thing and the code seems to do another, look closer.

Checking the explanation is correct

Here’s the uncomfortable bit. The explanation is also written by an AI, and it can be wrong. It may describe what the code should do rather than what it does, particularly if there’s a subtle bug. A confident, friendly walkthrough is not evidence.

Ways to check:

  1. Run it with small tests. If the explanation says “it ignores empty lines”, feed it an empty line and see.
  2. Change one thing and predict the result. If you can predict it correctly, you’ve understood.
  3. Look up unfamiliar functions in official documentation (MDN for JavaScript and web APIs, php.net for PHP). They’re short and authoritative.
  4. Ask a second time, differently, or use a different tool. If two explanations disagree, dig into the difference.
  5. Ask directly: “Is there anything in your explanation you’re unsure about?” Models don’t always flag doubts, but it sometimes brings up caveats.

For a wider list of what to look for in generated code, see AI-written code: five mistakes to check before you trust it.

Keep it from being a crutch

If you always paste and ask, you’ll never build your own understanding. A few ways to use explanations to actually learn:

  • Read the code first and write down your guess. Then ask the AI and compare.
  • After the explanation, close it and describe the code aloud, or in your own notes.
  • Ask for a variation: “Change this so it also works for X,” then read the difference.
  • Keep a small notebook of concepts that keep coming up, such as variables, loops, conditions and functions. You’ll see them repeating.

A caution on privacy

Don’t paste code that contains passwords, API keys or private customer data. Replace them with placeholders before asking. If the code belongs to an employer or a client, check whether you’re allowed to share it with an online tool.

Key takeaways

  • State your level and ask for line-by-line explanations.
  • Ask what the code touches and what could break it before running it.
  • Verify explanations with small tests; they can be wrong too.
  • Use quizzes and predictions to learn instead of only reading.

Next time you paste in a snippet, try the “what does this touch?” prompt first. Did it tell you anything you didn’t expect?