| name | explain-my-code |
| description | This skill should be used when the user asks to "document this codebase", "explain my code", "generate architecture docs", "write an onboarding doc", "analyze this repo", or wants self-contained technical documentation produced from source. Signal tokens include "codebase", "repo", "architecture diagram", "onboarding", "system overview", "tech stack", ".md doc". Casual phrasings (when paired with an onboarding/documentation intent) include "help a new dev understand this whole codebase", "write up how this project works", "map out this code into a doc". Also trigger when the user points at a repository and asks for a single Markdown write-up with Mermaid diagrams. Do NOT use for a quick one-line "what does this do" answer, for line-by-line code review or bug-finding (that is wordpress-architect-review or a review skill), for refactoring or modernization plans (that is refactoring-analyst), or for producing diagrams alone (that is codebase-to-mermaid). |
| version | 1.0.0 |
Explain My Code
Analyze a complete code repository as a senior software architect and produce one self-contained, production-grade Markdown document that lets anyone new to the project fully understand the system with no handover meetings or outside help.
When to Use
- User asks to document, explain, or map a codebase or repository
- User wants an onboarding document for people new to the project
- User wants a system/architecture overview with Mermaid diagrams
- User points at a repo and asks "what does this do / how does it work" AND wants it captured as a written document, not a one-line reply
Do NOT use for bug-finding code review, refactoring plans, or diagram-only output (see negative boundary in description).
Co-Load Precedence
When co-loading with codebase-to-mermaid: that skill owns standalone diagram generation; this skill embeds diagrams inside the full document. When co-loading with refactoring-analyst: that skill owns refactor planning; this skill only describes the system (Section 13 suggests improvements at a high level, it does not plan the refactor). When co-loading with a code-review skill (e.g. wordpress-architect-review): review owns defect findings; this skill owns architecture explanation (Section 12 lists observations/risks but does not perform line-level review).
Workflow
Phase 1: SCOPE THE TARGET
- Determine the repository root. Default to the current working directory. If the user named a path, use that.
- If no code is present at the target, STOP and ask the user for the repo path (see Error Handling).
- Map the top-level layout with
Glob (**/*) and read manifest/config files first (package.json, pom.xml, go.mod, requirements.txt, Cargo.toml, *.csproj, Dockerfile, compose files, CI configs).
Phase 2: IDENTIFY FOUNDATIONS
- Identify tech stack: languages, frameworks, frontend/backend/infra split, package managers.
- Classify architecture: monolith vs microservices vs modular.
- Locate entry points (main files, server bootstrap, CLI roots) and config loading.
Phase 3: TRAVERSE SYSTEMATICALLY
- Walk folder-by-folder, service-by-service, module-by-module.
- Infer design patterns (MVC, Clean, Hexagonal, Factory, Observer, etc.) - record where and why each is used.
- Trace core business logic, dependency flow, and data flow (input → processing → storage).
- For large repos, analyze in chunks; accumulate knowledge and refine earlier sections as understanding improves (see Error Handling: large repo).
Phase 4: WRITE THE DOCUMENT
- Produce ALL 13 sections in the order under Output Format. Do not skip any section.
- Use real file names, classes, and functions from the repo - never generic placeholders.
- Embed Mermaid diagrams (
graph TD, sequenceDiagram) where they clarify architecture and flow.
- Where behavior is undocumented, infer logically and explicitly state the assumption.
Phase 5: VERIFY BEFORE DELIVERY
- Confirm every one of the 13 sections is present and non-shallow.
- Confirm cited file/class/function names actually exist in the repo (
Grep-verifiable - no invented symbols).
- Confirm Mermaid blocks are syntactically valid.
- Write the result to
CODEBASE_DOCUMENTATION.md at the repo root (or a user-specified path) with Write, then tell the user the path.
Output Format
Single Markdown file, default name CODEBASE_DOCUMENTATION.md, containing these sections in order:
- Project Overview - purpose, high-level functionality, key use cases
- System Architecture - explanation, monolith/microservices/hybrid, Mermaid
graph TD diagram
- Repository Structure - folder hierarchy, role of each major directory, module responsibilities
- Tech Stack & Dependencies - languages, frameworks, external libraries, package managers, dependency graph if possible
- Core Components / Services - per component: responsibility, key classes/functions, interactions
- Application Flow - 6.1 Startup flow (entry point, init sequence, config, DI); 6.2 Request/execution flow; 6.3 Auth flow if applicable; 6.4 Data flow; include Mermaid
sequenceDiagram
- Key Design Patterns - patterns used, where and why
- Database & Models - schema overview, ORM usage, entity relationships
- External Integrations - APIs, third-party services, queues, caches
- Configuration & Environment - env vars, config files, build configs
- Build & Deployment - build steps, scripts, CI/CD if present
- Observations & Risks - code smells, tight coupling, scalability concerns, missing abstractions
- Suggestions for Improvement - refactoring ideas, performance, architecture alternatives
Constraints (from source prompt, preserved):
- Be precise and technical; avoid generic explanations.
- Do NOT give shallow summaries or skip flows/architecture.
- Do NOT assume undocumented behavior without stating the reasoning.
- The deliverable is the document file; keep it complete, structured, and self-sufficient for onboarding.
Error Handling
No repository / empty target: Ask the user for the repo path. Do not fabricate documentation for code that was not read.
Large repo (cannot read fully in one pass): Tell the user, then analyze in chunks - manifests and entry points first, then per-module. Accumulate findings and refine earlier sections. Note any directories sampled rather than fully read.
Ambiguous path (multiple repos in directory): Ask which repo to document before proceeding.
Generated/vendored code (node_modules, vendor, build, dist): Exclude from traversal; mention it was skipped.
Section genuinely not applicable (e.g., no database): Keep the heading, state "Not applicable - ". Do not silently drop a section.
Quality Gates
Examples
Standard case: User runs the skill in a Node/Express + React repo. The skill reads package.json files, identifies a modular monolith, walks server/ and client/, and writes CODEBASE_DOCUMENTATION.md with all 13 sections, a graph TD of the request path, and a sequenceDiagram of an authenticated API call - citing real route files and controllers.
Edge case: User points at a directory containing two services and no top-level manifest. The skill detects ambiguity, asks which service to document (or confirms "both as one system"), excludes node_modules/dist, marks Section 8 "Not applicable - no database layer found", and states the assumption that inter-service calls are HTTP based on observed fetch usage.