explain-code
Explain Code
Workflow
- Start with an analogy — compare the code to something from everyday life
- Pick the right C4 level — match diagram depth to the question; default Level 2 (Container)
- Draw the diagram — use the
c4-diagramskill (level selector, DSL rules, rendering all live there) - Walk through the code — trace inputs to outputs in narrative form: what enters, which functions/modules touch it in order, where state changes, what exits. Name the data, not just the steps.
- Highlight a gotcha — what's a common mistake or misconception?
Keep explanations conversational. For complex concepts, use multiple analogies.
Example output shapes
Single function
Analogy:
debounceis like a snooze button — it delays acting until you've stopped poking it.Walkthrough: Call starts the timer (
setTimeout). Another call before the timer fires clears it and restarts (clearTimeout). Timer fires → callback executes once.Gotcha: The returned function captures
timervia closure — each call site needs its owndebounce(fn, ms)instance, or they'll share the same timer.
Multi-class system
Analogy: This auth service is like a bouncer at a club — it checks your ID (token) before letting you into any room (endpoint).
(C4 Container diagram here — generated via
c4-diagramskill)Walkthrough: Request arrives at the HTTP adapter →
AuthMiddlewarevalidates the JWT → decoded claims passed toUserService.resolve()→ user record returned orUnauthorizedErrorthrown.Gotcha:
AuthMiddlewareis stateless but relies onTokenCache— if cache is cold, every request hits the DB.
Multi-service architecture
Analogy: This checkout pipeline is like a relay race — each service passes a baton (order event) to the next leg before it can proceed.
(C4 Context diagram here — generated via
c4-diagramskill)Walkthrough:
OrderServiceemitsorder.createdto the message bus →InventoryServicereserves stock and emitsstock.reserved→PaymentServicecharges and emitspayment.captured→NotificationServicesends confirmation email. Failure at any step emits a compensating event to roll back upstream.Gotcha: Each service is independently deployable but the saga has no central coordinator — debugging a failed order requires tracing correlation IDs across 4 service logs.