AI Prompt to Explain Code Line by Line
A ready AI prompt for understanding unfamiliar code: it gives a summary, walks through it in order and ends with questions to test yourself.
Published · 2 min read
Reading code written by someone else is a skill of its own. The syntax may be familiar, but the intent behind it is not, and guessing wrong costs time. This prompt asks for a summary first, then a walk through the code in the order it runs, and finally a few questions so you can check whether you really understood it instead of just nodding along.
The prompt
Explain the [programming language] code below to a [beginner or intermediate developer] who already knows [what you know, for example variables, loops and functions]. First give a two-sentence summary of what the code does. Then walk through it in the order it runs, block by block, saying what each part does and why it is there. Point out anything that looks risky, slow or confusing, and name the concept behind each unfamiliar term. Finish with three questions I can use to test my own understanding. Code: [paste the code here]
Replace the highlighted brackets with your own details before you send it.
When this prompt helps
It suits code from a tutorial, an open-source project, a colleague who has left, or an old project of your own that you no longer remember. It is especially useful for code that mixes several ideas, such as asynchronous calls, regular expressions or recursion, where a single line can hide a lot of behaviour.
How to use it
- Name the language and your level honestly. A beginner level gets definitions of terms, while an intermediate level skips them.
- Paste a focused piece of code, such as one function or one file, rather than a whole project.
- After reading the walkthrough, try to answer the three questions without looking back. Then ask the assistant to check your answers.
- Run the code yourself with a small input and compare what happens with the explanation.
A filled-in example
Explain the JavaScript code below to a beginner who already knows variables, loops and functions. (Followed by a twelve-line function that removes duplicate values from an array using a Set.)
A good answer states that the function returns a new array without repeated values, explains that a Set stores each value only once, and notes that the original array is not changed. If the explanation skips a part of your code, ask about that part by name.
Tips for better results
- Include the error or the behaviour that confused you. It tells the assistant where to slow down.
- Ask for the explanation in smaller chunks if the code is long, then connect the chunks yourself.
- Never paste passwords, API keys or private customer data. Replace them with placeholders first.
- Treat the explanation as a guide, not a guarantee. Assistants can misread unusual code, so confirm important points by running it.
Variations to try
- Add "use an analogy from everyday life" for concepts that feel abstract.
- Ask for a version of the explanation with comments added to the code itself.
- Request a list of the three safest changes you could make to improve the code without changing its behaviour.