Explain Code
Explain what the given code does, in clear prose with a short summary.
Explains what a file, function, class, or line range does, read-only. It reads the actual source and its callers, traces purpose, inputs, outputs, control flow, and dependencies, then returns a short summary, an execution-order walkthrough, an inputs and outputs table, and the edge cases, with file and line references.
Install to ~/.claude/commands/explain-code.md
npx agentscamp add commands/explain-codeExplain the code identified by $ARGUMENTS. The argument may be a file path, a function or class name, a module, or a line range. Produce an explanation that a teammate could read once and understand without opening the source themselves.
If $ARGUMENTS is empty, ask which file or symbol to explain.
When to use
- You are about to change, review, or debug code you did not write and need an accurate picture of one file, function, or class first.
- Not for following one value across many modules: use /trace-data-flow. Not for writing the explanation into the source as comments: use /add-docstrings.
This command is read-only. It uses Read, Grep, and Glob, and does not run the code, so describe runtime behavior as what the source says will happen, not as something you observed.
Locate the target
Resolve $ARGUMENTS before explaining anything.
- If it is a path (e.g.
src/auth/session.ts), read that file. - If it is a symbol (e.g.
validateTokenorclass UserStore), search the codebase to find its definition, then read the surrounding context. - If it is a path with a range (e.g.
parser.go:40-120), read those lines plus enough above and below to understand the scope. - If it is ambiguous or matches multiple results, list the candidates and ask which one is meant. Do not guess.
- For a symbol, also Grep for its call sites and read one or two. How code is called often explains why it is written the way it is.
- For a whole file over a few hundred lines, explain its structure and main entry points first, then offer to go deeper on a named part instead of narrating every function.
NOTE
Read the actual source before writing a single word of explanation. Never describe code from the name alone.
Understand before you write
Trace the behavior, not just the syntax. Before drafting, work out:
- The purpose: what problem this code solves and who calls it.
- The inputs: parameters, arguments, environment, or global state it reads.
- The outputs: return values, mutations, writes, network calls, or thrown errors.
- The control flow: branches, loops, early returns, and the happy path versus edge cases.
- The dependencies: other functions, modules, or services it relies on.
- Any non-obvious details: concurrency, caching, side effects, or subtle correctness concerns.
Output format
Structure the response with these sections.
Summary
Two or three sentences in plain language stating what the code does and why it exists. A reader should be able to stop here and have the gist.
How it works
Walk through the logic in execution order. Use prose for the narrative and a short list for distinct steps. Quote only the small, load-bearing fragments that matter — do not paste the whole file back. Anchor each step to its location as path:line so the reader can jump to it.
1. Receives <input> and validates <condition>.
2. Transforms it via <step>.
3. Returns <output> or raises <error> when <edge case>.Inputs and outputs
Be precise about types and contracts.
| Aspect | Detail |
|---|---|
| Inputs | parameters, types, expected shape |
| Returns | return type and meaning |
| Side effects | I/O, mutations, network, logging |
| Errors | what is thrown or returned on failure |
Edge cases and gotchas
Call out anything surprising: silent failure modes, off-by-one risks, assumptions about input, thread safety, or behavior that contradicts the function name.
Guidelines
- Match the explanation's depth to the code's complexity. A three-line helper needs a sentence; a state machine needs the full breakdown.
- Use the project's own terminology — variable and domain names as they appear in the source.
- Prefer correctness over completeness. If you are unsure how something behaves, say so explicitly rather than inventing an explanation.
- When behavior depends on code you did not open (a library call, a config value, a remote service), name it as an assumption instead of describing it as fact.
WARNING
Do not modify the code. This command only explains. If you spot a bug while reading, mention it in the gotchas section but make no edits.
Filed under
Related
- AgentDocumentation EngineerUse this agent to write and maintain technical docs that stay true to the code — READMEs, how-to guides, API references, and runbooks. Examples — updating a stale README after a refactor, documenting a new public API from its signatures, writing an on-call runbook for a service.
- SkillOnboarding Guide WriterWrite a developer onboarding guide that gets a new contributor from clone to first merged change fast — a verified golden path, a quick architecture map, the real workflow conventions, and the gotchas that live only in senior engineers' heads. Use when a repo has no onboarding doc, when new hires keep asking the same setup questions, or when the README is a marketing page instead of a contributor guide.
- GuideSkills vs Agents vs CommandsHow Claude Code's two extension mechanisms — subagents and skills — differ across three invocation patterns, with a decision table for choosing the right one.
- SkillArchitecture Diagram GeneratorGenerate accurate architecture diagrams as Mermaid — straight from the codebase, not from imagination — by first choosing which view answers the question (container/component, sequence, ER, or state) and then reading the real entry points, module boundaries, service calls, and schema. Use when onboarding to an unfamiliar repo, documenting a system, or visualizing one complex flow.
- CommandTrace Data FlowTrace how a value, field, or variable flows through the codebase from source to sink.
- Guide9 Best Claude Skills for Onboarding to a New CodebaseClaude skills and commands for getting productive in an unfamiliar repo: settings audits, dev containers, CLAUDE.md, diagrams, code explanations, data-flow traces, and git history.
- CommandAdd DocstringsAdd or improve docstrings for the public API of a file or symbol.
- CommandUpdate READMEUpdate the README to reflect the current scripts, structure, and features of the repo.