Understand Legacy Code
Purpose
Turn an unfamiliar codebase into a map a developer can act on: where things happen, which rules live where, what is dangerous to touch and where to start, while keeping clear what was seen in the code and what is still a guess.
When to use
- A team inherits a system or module without reliable documentation or original authors.
- A change must be made in code whose flow and rules are not understood.
- A modernization, migration or rewrite needs an inventory of current behavior and hidden rules.
When not to use
- A single function or file needs explaining. Use
code-explanation.
- The goal is to improve the code structure step by step. Use
refactoring.
- The goal is a prioritized debt inventory for decision makers. Use
tech-debt-assessment.
Inputs
Required:
- Access to code excerpts: folder tree, entry points or the files around the area of interest.
- The question or change that drives the exploration (for example "how is an invoice created").
Optional, improves quality:
- Build and deployment files, database schema, configuration, logs, commit history highlights, existing tickets, anyone who remembers the system.
If only part of the code is available, map what is visible, mark gaps [NOT SEEN], and ask for the next most informative files one small batch at a time.
Process
- Anchor on the driving question; comprehension without a goal expands forever.
- Identify the tech stack and entry points: main programs, HTTP routes, scheduled jobs, message consumers, UI screens, stored procedures and triggers.
- Sketch the module map: each package or component with a one-line responsibility inferred from names, dependencies and code, and its inbound/outbound dependencies.
- Trace the main flow for the driving question from entry point to persistence and outbound calls; record file and function names at each hop.
- Inventory data: tables or files touched, who writes them, and data that other systems read directly.
- Extract hidden business rules: conditionals on status codes, magic numbers, date cut-offs, special customers, configuration flags; record each with its location.
- Mark risk zones: no tests, high churn or complexity, reflection or dynamic dispatch, shared global state, copy-pasted variants, code that looks dead (confirm with usage search or logs before deleting).
- Identify seams: places where behavior can be observed or substituted for testing (interfaces, configuration, boundaries), and the safest point to make the requested change.
- Propose characterization tests that pin current behavior before changes.
- Label every conclusion as
[SEEN in <file>] or [INFERRED], and list open questions with who or what (logs, database, business owner) can answer them.
- If the goal continues, suggest
refactoring to improve the area safely, business-rules-catalog to document the extracted rules or tech-debt-assessment to prioritize the risks found.
Output format
# Legacy Code Map: <system/module>
Driving question: <...> · Coverage: <what was seen / not seen>
## Entry Points
| Type | Name | Location |
## Module Map
| Module | Responsibility | Depends on | Evidence |
## Main Flow: <question>
1. <hop> — <file:function> — [SEEN/INFERRED]
## Data Touched
| Store/table | Read/Write | By | Shared with |
## Hidden Business Rules
| Rule | Location | Evidence | Confirm with |
## Risk Zones
## Seams and Safe Change Point
## Characterization Tests to Add
## Open Questions
Quality checklist
Common pitfalls
- Reading top-down through every file instead of following one flow from an entry point.
- Trusting names and comments over behavior; legacy names often lie after years of changes.
- Deleting "unused" code that is called via reflection, configuration, scheduled jobs or another system reading the database.
- Missing logic that lives outside the application: stored procedures, triggers, cron scripts, ETL jobs.
Example
Input: "Billing module, folders jobs/, core/, db/procs/. How is an invoice created?"
Excerpt of output:
jobs/NightlyBilling.run() selects contracts with status IN (2,5) [SEEN in NightlyBilling.java]
- Calls stored procedure
sp_create_invoice [SEEN]; its body [NOT SEEN], request db/procs/sp_create_invoice.sql next.
- Hidden rule: contracts with
customer_type = 'K' skip VAT [SEEN in TaxCalc.java:88] — confirm meaning of 'K' with the finance owner.
1---2name: legacy-code-comprehension3description: "Builds a working map of unfamiliar or legacy code: entry points, modules and their responsibilities, main runtime flows, data stores, external integrations, hidden business rules, dead or risky areas and safe places to change, with every conclusion tied to evidence in the code. Use when a developer inherits a system, must change code nobody fully understands, plans a modernization, or asks how an old codebase works."4license: MIT5metadata:6 source: https://skillmd.com/skills/muratdincer/legacy-code-comprehension7 version: "1.0.0"8 language: en9 category: 05-engineering10 role: developer11 area: coding12 title: "Understand legacy code"13 related: "code-explanation, refactoring, tech-debt-assessment, modernization-assessment, business-rules-catalog"14 prompt: "I inherited this billing module with no documentation. Here is the folder structure and the main classes; help me understand how an invoice gets created."15---1617# Understand Legacy Code1819## Purpose20Turn an unfamiliar codebase into a map a developer can act on: where things happen, which rules live where, what is dangerous to touch and where to start, while keeping clear what was seen in the code and what is still a guess.2122## When to use23- A team inherits a system or module without reliable documentation or original authors.24- A change must be made in code whose flow and rules are not understood.25- A modernization, migration or rewrite needs an inventory of current behavior and hidden rules.2627## When not to use28- A single function or file needs explaining. Use `code-explanation`.29- The goal is to improve the code structure step by step. Use `refactoring`.30- The goal is a prioritized debt inventory for decision makers. Use `tech-debt-assessment`.3132## Inputs33Required:34- Access to code excerpts: folder tree, entry points or the files around the area of interest.35- The question or change that drives the exploration (for example "how is an invoice created").3637Optional, improves quality:38- Build and deployment files, database schema, configuration, logs, commit history highlights, existing tickets, anyone who remembers the system.3940If only part of the code is available, map what is visible, mark gaps `[NOT SEEN]`, and ask for the next most informative files one small batch at a time.4142## Process431. Anchor on the driving question; comprehension without a goal expands forever.442. Identify the tech stack and entry points: main programs, HTTP routes, scheduled jobs, message consumers, UI screens, stored procedures and triggers.453. Sketch the module map: each package or component with a one-line responsibility inferred from names, dependencies and code, and its inbound/outbound dependencies.464. Trace the main flow for the driving question from entry point to persistence and outbound calls; record file and function names at each hop.475. Inventory data: tables or files touched, who writes them, and data that other systems read directly.486. Extract hidden business rules: conditionals on status codes, magic numbers, date cut-offs, special customers, configuration flags; record each with its location.497. Mark risk zones: no tests, high churn or complexity, reflection or dynamic dispatch, shared global state, copy-pasted variants, code that looks dead (confirm with usage search or logs before deleting).508. Identify seams: places where behavior can be observed or substituted for testing (interfaces, configuration, boundaries), and the safest point to make the requested change.519. Propose characterization tests that pin current behavior before changes.5210. Label every conclusion as `[SEEN in <file>]` or `[INFERRED]`, and list open questions with who or what (logs, database, business owner) can answer them.5311. If the goal continues, suggest `refactoring` to improve the area safely, `business-rules-catalog` to document the extracted rules or `tech-debt-assessment` to prioritize the risks found.5455## Output format56```markdown57# Legacy Code Map: <system/module>58Driving question: <...> · Coverage: <what was seen / not seen>5960## Entry Points61| Type | Name | Location |6263## Module Map64| Module | Responsibility | Depends on | Evidence |6566## Main Flow: <question>671. <hop> — <file:function> — [SEEN/INFERRED]6869## Data Touched70| Store/table | Read/Write | By | Shared with |7172## Hidden Business Rules73| Rule | Location | Evidence | Confirm with |7475## Risk Zones76## Seams and Safe Change Point77## Characterization Tests to Add78## Open Questions79```8081## Quality checklist82- [ ] The map answers the driving question with a traceable flow.83- [ ] Every statement is tagged `[SEEN in ...]` or `[INFERRED]`; unseen areas are marked `[NOT SEEN]`.84- [ ] Hidden business rules include location and a way to confirm them.85- [ ] "Dead code" is only claimed with evidence or marked as suspected.86- [ ] A concrete safe change point and characterization tests are proposed.87- [ ] All checks pass; if any fails, revise the output and re-run this checklist before answering.8889## Common pitfalls90- Reading top-down through every file instead of following one flow from an entry point.91- Trusting names and comments over behavior; legacy names often lie after years of changes.92- Deleting "unused" code that is called via reflection, configuration, scheduled jobs or another system reading the database.93- Missing logic that lives outside the application: stored procedures, triggers, cron scripts, ETL jobs.9495## Example96Input: "Billing module, folders `jobs/`, `core/`, `db/procs/`. How is an invoice created?"9798Excerpt of output:991. `jobs/NightlyBilling.run()` selects contracts with `status IN (2,5)` `[SEEN in NightlyBilling.java]`1002. Calls stored procedure `sp_create_invoice` `[SEEN]`; its body `[NOT SEEN]`, request `db/procs/sp_create_invoice.sql` next.101- Hidden rule: contracts with `customer_type = 'K'` skip VAT `[SEEN in TaxCalc.java:88]` — confirm meaning of `'K'` with the finance owner.