Explain Code Prompt — the Understanding Contract
"Explain this code" gets line-by-line narration. The understanding contract gets strategy, audience-fit, design decisions, and inference labeled as inference.
What actually happens when a client calls POST /orders: the contract, every decision point, every side effect, and the conditions that route off the happy path.
Endpoint behavior lives in many places — validation, services, queues — and explanations that stay inside one function miss the flow. This prompt uses the function-walkthrough mode on an end-to-end objective: the contract first (inputs with shapes, outputs with meaning, errors with triggers), the execution flow walked in order with both branches explained at every decision point, and every side effect surfaced — writes, calls to other systems, queued work — with when each happens. The happy path gets distinguished from the edge paths by the conditions that separate them. The result reads like the sequence diagram nobody drew.
Contract first
Inputs with expected shapes, outputs with meaning, errors with their triggers — before any flow.
Walk every decision
Each branch explained in plain language: the condition, and what each outcome means.
Surface the side effects
Writes, external calls, queued jobs — and exactly when in the flow each one happens.
It won't. The NON-GOALS block forbids review entirely — "no findings, no severities, no verdicts" — and anything that looks defective gets noted "as a question for the team" instead. The code-explanation-prompt outputs an explanation prompt; if you actually need bug-finding, notFor points you to the Debugging Prompt Generator instead.
The ASSUMPTIONS section requires every statement about intent, history, or rationale to be labeled INFERENCE unless the code evidences it — a comment, a test, a telling name. Where behavior can't be established it must write "cannot be determined from this code" rather than fill the gap with a plausible story. You still verify the walkthrough against the real code.
Because narration skips error paths. The EXPLANATION STRATEGY walks the execution flow in order, pausing at each decision to state the condition in plain language and what each outcome means, and surfaces every side effect — writes, calls to other systems, queued work — with when each happens. The stated CODE CONTEXT covers validate, reserve inventory, charge payment, and queue fulfillment, not just the happy path.
"Explain this code" gets line-by-line narration. The understanding contract gets strategy, audience-fit, design decisions, and inference labeled as inference.
For code whose authors are gone: reconstruct likely intent as labeled inference, hunt undocumented assumptions, and end with a maintenance brief — what's safe to touch.
Explain the idea before the implementation: numbered steps with their contribution, honest best/worst-case complexity, and the alternatives each decision point rejected.
"Review this code" gets shallow comments. The review contract gets findings with severities, a checklist, and a verdict.
"Refactor this code" invites silent behavior changes. The refactoring contract preserves business rules, outputs, and side effects — and flags uncertainty instead of deciding it.
"Fix this error" gets guesses. The investigation contract gets a ten-stage diagnosis: facts separated from assumptions, alternatives weighed, fixes justified.
Build code explanation prompts — mode, audience, and depth turn into an understanding contract that teaches, not narrates.
An endpoint exists but its docs don't, so "document this API" from code alone invents parameters, status codes, and an auth flow that were never real. Here's how to write API documentation with AI from the facts you provide, with unknowns flagged and checked against the real API.