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.
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.
Legacy code cannot be explained the way fresh code can: the names lie, the comments describe what it used to do, and the oddities record incidents nobody remembers. This prompt uses the legacy mode's strategy — reconstruct likely intent from the code itself, identify dated patterns and what they were the standard solution for, hunt hidden assumptions about input shapes and ordering, and flag what will resist change or break silently. Its sharpest rule treats oddities as evidence: a strange special case usually records a real requirement — infer what it might be, and label the inference. A deep-dive depth adds the rule that keeps it honest: where rationale cannot be recovered, say so.
Read behavior, not names
The strategy walks what the code actually does — legacy names and comments routinely describe the past.
Treat oddities as evidence
Strange special cases become labeled hypotheses about real incidents and requirements.
Leave with a maintenance brief
What is safe to touch, what is load-bearing, and what to find out before changing anything.
It forces a hard split between fact and guesswork. The ASSUMPTIONS section requires every claim about intent, history, or rationale to carry an INFERENCE label unless a comment, test, or telling name evidences it, and where behavior can't be read from the source the model must write "cannot be determined from this code" rather than fill the gap with a plausible story.
The DESIGN DECISIONS rule assumes a strange special case usually records a real incident or requirement. The prompt has the model infer what that requirement might have been and label the inference, instead of flagging it as a defect — findings, severities, and fixes are explicitly out of scope under NON-GOALS, so oddities become hypotheses, not verdicts.
Understanding is where it stops. NON-GOALS bar reviewing quality, debugging failures, refactoring, and generating tests — the code-explanation-prompt only produces an explanation you run in your assistant. The closing LEARNING NOTES give a maintenance brief of what's safe to touch and what's load-bearing, which you carry into a separate refactor or debugging prompt.
"Explain this code" gets line-by-line narration. The understanding contract gets strategy, audience-fit, design decisions, and inference labeled as inference.
Explain the idea before the implementation: numbered steps with their contribution, honest best/worst-case complexity, and the alternatives each decision point rejected.
Explain a system's shape, not its statements: layers and their owners, boundary contracts, dependency direction, and one request traced through every handoff.
"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.
Recover a legacy system's architecture before you change it — package the codebase context, explain the current structure, surface the risks and seams, then document the recovered architecture.
Update old, risky code you didn't write — safely — by understanding and pinning its behavior in tests before you change a single line.
Type "refactor this" on code you don't fully understand and the AI — which understands it no better — rewrites a load-bearing line. Here's how to run an explanation pass first: what the code does, what it assumes, and which lines are risky to touch, before you change anything.