Home › Coding › Explain unfamiliar code: how to do it with AI
Explain unfamiliar code: how to do it with AI
From official vendor pages; selected tasks also include dated logged-out free-plan trials. Report an error.
Beginner Risk: Low 💻 Coding
Stuck on code someone else wrote? An AI can walk you through it line by line and point out likely bugs. It works best on one function or file at a time, with enough context to see what feeds into it.
Who it’s for: Developers joining a project, students, analysts reading scripts, and anyone who inherited code they didn't write.
Why low risk: You can check any explanation by running the code. The risks are explanations that sound right but are wrong, and secrets left in pasted code.
ChatGPT has the highest score on our published rubric (94/100) of the 4 tools listed for this task. Free plan includes limited Codex access (verified on its pricing page), and the chat can explain pasted code.
When it fits, and when it doesn’t
Good fit:
- Reading one function or file you didn't write.
- Getting a map of an unfamiliar repository before you change it.
- Debugging with a clear actual-vs-expected example.
Not a good fit:
- Code containing secrets or customer data you can't remove.
- Security or licensing decisions based only on the explanation.
- Very large codebases pasted into a chat (use an editor agent that reads files instead).
Step by step
- Copy the function plus its imports and any types or config it uses. One piece at a time beats a whole repo pasted in.
- Say what you know: the language, what the code is supposed to do, and what's confusing you.
- Ask for the purpose, inputs and outputs, side effects, and edge cases, in that order.
- Ask for questions you should ask the original author.
- Check its explanation: add a print or log line, or run a quick test, to confirm one claim.
- For a whole project, use an editor-based agent that can read the files, such as Cursor or GitHub Copilot.
AI prompts to explain unfamiliar code
Replace the [brackets] with your details. See all prompt examples
Explain this [language] code to someone who knows basic programming but not this codebase: [paste]. Cover: what it's for, inputs and outputs, side effects (files, network, database), edge cases, and anything that looks like a bug. Refer to line numbers.
Walk through this code with the input [example input], step by step, showing the value of each variable as it changes. Tell me the final output and any point where it could crash.
I've opened a new project. Read the main entry point and folder structure, then give me a one-page map: what each folder does, where requests or data come in, and the 5 files I should read first.
This [language] code gives [actual result] but I expected [expected result] for input [input]. Here is the code and the error or output: [paste]. List the 3 most likely causes in order, how to test each one, and the smallest fix. Don't rewrite unrelated code.
Write [pytest/Jest/...] tests for this function covering: a normal case, an empty or missing input, a boundary value, and the edge cases you found. Explain what each test proves.
Illustrative example
You paste
def apply_discount(price, code):
if code in DISCOUNTS:
return price * (1 - DISCOUNTS[code])
return priceA good answer looks like
Purpose: returns the price after a discount code.
Inputs: price (number), code (string). Output: number.
Edge cases: unknown code returns the full price silently; a discount above 1.0 would give a negative price; code is case-sensitive ("save10" ≠ "SAVE10").
To confirm: call apply_discount(100, 'SAVE10') and print the result.Illustrative example written for this guide, not output captured from a specific tool.
Check before you rely on it
- Run the code with one example and compare with the explanation.
- Add a test for each edge case the AI names; keep the ones that fail as bug reports.
- Check library behaviour in the official docs for the version you use, not the AI's memory.
Which tool to pick, and why
- Claude (incl. Claude Code): Free plan verified for chat with files and code execution. Claude Code (paid) can read a whole repository. Visit official website ↗
- ChatGPT (incl. Work & Codex): Free plan includes limited Codex access (verified on its pricing page), and the chat can explain pasted code. Free plan can do this Visit official website ↗
- GitHub Copilot: Free plan verified: 2,000 completions a month plus limited chat and agent usage, inside VS Code, Visual Studio and other editors. Free plan can do this Visit official website ↗
- Cursor: An AI code editor whose agent can read your whole project. Free plan verified with a limited allowance. Free plan can do this Visit official website ↗
Reasons come from the capabilities, free-plan details and prices we've checked on official pages (separate from any dated trial above). Sources: claude.com/pricing · chatgpt.com/pricing · github.com/features/copilot/plans · cursor.com/pricing
Common mistakes
- Pasting code that contains API keys, passwords or customer data.
- Pasting one line without the context it depends on.
- Believing an explanation without testing even one claim.
- Letting the AI "fix" code you don't yet understand.
- Asking about a framework version it may not know. Name the version.
- Asking "why is this broken?" without the error message and input.
- Accepting a rewrite of the whole file when you asked for an explanation.
Privacy tip
Never paste secrets: remove API keys, tokens and connection strings first. For private code, Cursor's Privacy Mode keeps your code out of training, and GitHub lets individual Copilot users opt out of training at github.com/settings/copilot/features.
Cursor setting · GitHub Copilot setting · Claude (incl. Claude Code) setting · Sources: cursor.com/help/security-and-privacy/privacy · github.com/features/copilot/plans