Home › Coding › Explain unfamiliar code: how to do it with AI

Explain unfamiliar code: how to do it with AI

Last checked

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.

Best pick

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:

Not a good fit:

Step by step

  1. Copy the function plus its imports and any types or config it uses. One piece at a time beats a whole repo pasted in.
  2. Say what you know: the language, what the code is supposed to do, and what's confusing you.
  3. Ask for the purpose, inputs and outputs, side effects, and edge cases, in that order.
  4. Ask for questions you should ask the original author.
  5. Check its explanation: add a print or log line, or run a quick test, to confirm one claim.
  6. 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 function
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.
Trace one example
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.
Map an unfamiliar repo
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.
Debug with me
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 tests that prove the explanation
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 price

A 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

Which tool to pick, and why

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

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

Related tasks

Report an error on this page