EXPLANATION OBJECTIVE
Walk through this function line by line, explaining what each line does and why.
Explanation mode: function walkthrough — inputs, outputs, execution flow, decisions, and side effects.
The goal is understanding, not judgment: explain what this code does and why it is built this way — and clearly separate what the code shows from what you infer.
CODE CONTEXT
System context: A small utility used across the front end to limit how often a handler runs.
Code to explain:
```
function debounce(fn, wait) {
let t;
return function (...args) {
clearTimeout(t);
t = setTimeout(() => fn.apply(this, args), wait);
};
}
```
AUDIENCE PROFILE
Audience: a junior developer — fluent in basic syntax, still building a pattern vocabulary.
- Assume: basic language syntax is known; design patterns, idioms, and domain terms are not.
- Define every term of art on first use, in one sentence, in plain words.
- Prefer concrete examples over abstract descriptions — show a real value flowing through the code.
EXPLANATION STRATEGY
1. State the contract first: inputs with their expected shapes, outputs with their meaning, errors with their triggers.
2. Walk the execution flow in order, pausing at every decision point to explain both branches.
3. Surface every side effect — writes, calls to other systems, mutations of shared state — and when each happens.
4. Distinguish the happy path from the edge paths; name the conditions that route execution off the happy path.
- Depth: detailed explanation — thorough coverage of behavior and structure.
- Cover every section of this contract with real content — no skipped or token sections.
- Go as deep as the code requires, but no deeper: detail serves understanding, not completeness theater.
KEY CONCEPTS
- Explain each non-obvious variable, structure, and helper the walkthrough touches — what it holds and why it exists.
- Order concepts by how load-bearing they are for understanding this code — not by where they appear in the file.
EXECUTION FLOW
- Present the flow as a numbered sequence; at each branch, state the condition in plain language and what each outcome means.
- Where the flow depends on state or configuration, say which, and what each setting changes.
DESIGN DECISIONS
- Where the implementation makes a visible choice — an early return, a special case, a particular ordering — explain what the choice achieves.
- Discuss only decisions actually visible in the code; rationale you cannot evidence belongs under ASSUMPTIONS, labeled as inference.
ASSUMPTIONS
- Separate what the code DOES (verifiable from the source) from why it might be that way (inference).
- Label every statement about intent, history, or rationale as an INFERENCE unless the code itself evidences it — a comment, a test, a telling name.
- Where behavior cannot be determined from the provided code, say "cannot be determined from this code" — never fill the gap with a plausible story.
TRADEOFFS
- Identify the tradeoffs this code embodies: what it optimizes for, and what it sacrifices for that.
- Tie each tradeoff to its visible consequence in the code.
LEARNING NOTES
- End with the contract restated in two sentences: what you give it, what you get back, and what else changes.
NON-GOALS
- Do not review code quality — no findings, no severities, no verdicts.
- Do not debug failures — explain the behavior that exists without diagnosing it.
- Do not refactor the code or propose rewrites.
- Do not generate tests.
- Do not invent requirements or unstated intentions.
- If something looks defective, note it as a question for the team — investigating or fixing it is out of scope.
- Clearly separate facts from assumptions throughout.
OUTPUT REQUIREMENTS
- Structure the explanation with headings that mirror the sections above — a reader should be able to navigate it without reading all of it.
- Anchor every claim about behavior in the code: name the function, branch, or line it comes from when precision matters.
- Calibrate every section to the stated audience — too basic wastes their time, too advanced loses them.
- End with the open questions: what could not be determined from the provided code, and who or what could answer it.