Prompt: Explain Unfamiliar Code

A prompt for a clear, layered explanation of unfamiliar code — what it does, how data flows, and what's risky — before you change it.

Updated Sep 2026 PIS score 74
The prompt
Open in Studio
You are a senior engineer helping a colleague understand code they didn't write before they modify it.

Objective: Explain what the code below does, written in {{LANGUAGE}}.

Context: Why I need to understand it: {{REASON — e.g. about to fix a bug, adding a feature, reviewing a PR}}.

Constraints:
- Start with a one-paragraph summary of what the code does overall, in plain language
- Then walk through it in logical sections (not line by line), explaining what each section does and why it likely exists
- Explicitly flag anything that looks risky, unusual, or likely to break if changed carelessly
- Do not guess at business intent you can't infer from the code — say "unclear from the code" instead

Output format: Return a "Summary" section, a "Walkthrough" section with subheadings, and a "Risks and unknowns" section.

Before finishing, check that every claim about what the code does is traceable to a specific line or block, not assumed.

Code:
```
{{PASTE CODE HERE}}
```

Example

Input

LANGUAGE: Python
REASON — e.g. about to fix a bug, adding a feature, reviewing a PR: About to add a new discount type without breaking existing pricing logic
PASTE CODE HERE:
  def calculate_price(base_price, user, promo_code=None):
      price = base_price
      if user.is_premium:
          price *= 0.9
      if promo_code:
          discount = get_discount(promo_code)
          if discount and not discount.expired:
              price -= discount.amount
      return max(price, 0)

Output

## Summary
This function calculates a final price by applying a premium-user discount and an optional promo code discount to a base price, ensuring the result never goes below zero.

## Walkthrough
**Premium discount**: If the user is marked premium, the price is multiplied by 0.9 — a flat 10% reduction applied before any promo code.

**Promo code discount**: If a promo code is supplied, it looks up a discount object. If that discount exists and hasn't expired, its `amount` is subtracted from the price as a flat value, not a percentage — so it stacks additively with the premium discount rather than multiplicatively.
Show more
**Floor**: `max(price, 0)` prevents the final price going negative if a large flat discount is applied to a small base price.

## Risks and unknowns
- The premium discount and promo discount combine in a specific order (percentage first, then flat amount) — changing that order would change final prices. Unclear from the code whether that order is a deliberate business rule.
- `get_discount` and `discount.expired` are not shown here — their behaviour on an invalid promo code is unclear from this code.

Why it works

  • Separating a plain-language summary from the detailed walkthrough gives a fast orientation before the reader commits to the full detail.
  • Explicitly flagging risky or unusual logic surfaces exactly the parts most likely to break silently when a new feature is added.
  • Banning guessed business intent keeps the explanation honest about what the code actually shows versus what's assumed.

Why the basic version fails

“Explain this code” on an unfamiliar function usually produces a line-by-line restatement of syntax the reader can already see, padded out with confident-sounding guesses about business intent the code doesn’t actually reveal — which is misleading precisely when the reader most needs to trust the explanation.

What this prompt fixes

Separating a plain-language summary from the detailed walkthrough gives a fast orientation before committing to reading every section closely. Explicitly flagging risky or unusual logic — like the specific order two discounts combine in — surfaces exactly the part most likely to break if a new feature is bolted on carelessly. Banning guessed business intent keeps the explanation limited to what the code actually demonstrates, with genuine unknowns marked as such rather than filled in with a plausible-sounding guess.

Tips per model

  • Claude tends to be disciplined about the “unclear from the code” rule, which is exactly the behaviour you want before touching code you didn’t write.
  • ChatGPT is fast at spotting the overall shape of a function, useful for a quick first-pass orientation before a deeper read.
  • Paste the whole function or module, not a snippet — a risk flagged out of context (like the discount ordering here) often depends on code just outside the pasted section.