Coding
AI Prompt for Understanding Legacy Code
Decode legacy code with this AI prompt: reverse-engineer intent, map the architecture, and get a safe plan for changing old undocumented code.
The prompt
Role: You are a software archaeologist who reverse-engineers undocumented legacy systems and explains them clearly. Context: Language: [LANGUAGE]. This code is roughly [AGE] old, written for [ORIGINAL_CONTEXT if known]. My goal: [YOUR_GOAL, e.g. add a feature, fix a bug, migrate it]. Legacy code: [CODE_SNIPPET] Task: 1) Reconstruct what this code is trying to accomplish — its intent, not just its mechanics. 2) Map the structure: what are the main parts and how do they connect? 3) Identify the risky areas: hidden assumptions, magic values, global state, things that look wrong but might be load-bearing. 4) Tell me what NOT to touch and why. 5) Give me a safe, step-by-step plan to achieve [YOUR_GOAL], starting with how to build a safety net (tests/characterization) first. Format: Intent summary first, then the structure map, then risks (flag each as "probably safe" or "do not touch blindly"), then the step-by-step plan. Constraints: Distinguish confident conclusions from educated guesses — label guesses clearly. Do not suggest a full rewrite unless the code is truly unsalvageable; I need to work with it.
Variables to fill in
Replace each [variable] in the prompt with your own details:
[LANGUAGE] [AGE] [ORIGINAL_CONTEXT] [YOUR_GOAL] [CODE_SNIPPET]
Example input
LANGUAGE=Java, CONTEXT=2008-era payroll module, 3 maintainers in 15 years, GOAL=add a new tax rule without breaking existing calculations
How to use this prompt
State your goal — the analysis focuses on what you need to change, not the whole system. Mention the code's era; old idioms get misread by modern assumptions.
Tips for the best results
- Ask it to write characterization tests first — they document what the code actually does today.
- Check its guesses against git blame/history when possible; humans leave clues.
- Have it list the magic numbers and ask what each one probably means.
Frequently asked questions
Can AI really understand code nobody documented?
It reconstructs intent from structure and names, but verify its guesses against tests or git history before changing behavior.
The codebase is huge. Where do I start?
Use it on one module at a time, starting with the entry points — breadth-first understanding beats depth-first for legacy code.