Legacy Code Comprehension And Bug Hunt
Explains an unfamiliar legacy file and surfaces latent bugs and risky areas before you change it.
Prompt
ROLE: You are an engineer onboarding onto an unfamiliar, undocumented legacy module and assessing its risk before modification. CONTEXT: The file below is from a legacy [LANGUAGE] codebase with little documentation. We need to [PLANNED_CHANGE] and want to understand it and find lurking bugs first. Known behavior: [WHAT_IT_DOES_AT_A_HIGH_LEVEL]. CODE: [PASTE_LEGACY_CODE] TASK: 1. Produce a plain-language explanation of what the module does, its inputs/outputs, and its key responsibilities. 2. Map the control flow and call relationships, noting any global/shared state and side effects. 3. Hunt for latent bugs: dead code, unreachable branches, swallowed errors, resource leaks, broken invariants, and assumptions that may no longer hold. 4. Identify the riskiest areas to touch for the planned change and what could break elsewhere (hidden coupling, implicit contracts). 5. Recommend safety nets to add before modifying: characterization tests, logging, or small refactors that reduce risk. OUTPUT FORMAT: - 'What this module does' (plain English). - 'Control flow / dependencies' (summary or diagram-in-text). - 'Latent bugs and smells' (location | issue | severity). - 'Risk map for the planned change' + 'Safety nets to add first'. CONSTRAINTS: Do not propose the change itself yet; focus on understanding and de-risking. Clearly separate confirmed facts from inferences about intent. Flag anything you cannot determine without seeing additional files.
How to use this prompt
- 1
Copy the prompt above and paste it into ChatGPT, Claude, or Gemini — or open it in the visual Studio to edit each part on a canvas and run it with your own key.
- 2
Replace any bracketed placeholders with your specifics. The more concrete your context and constraints, the sharper the result — see the 5-part prompt structure.
- 3
Run it, then refine. Ask the model to critique and improve its own answer with self-critique prompting.
Techniques in this prompt
Assigns the model an expert persona so it adopts the right vocabulary, depth, and standards for the task.
Learn this techniqueRecommended models
Build on this prompt
Open it in the visual Studio to wire it into a full workflow with your own API key — or learn the craft behind prompts like this.
More in Code Review & Debugging
Pull Request Review With Severity Triage
Reviews a pull request diff and returns issues bucketed by blocking, major, minor, and nit severity with concrete fixes.
Root-Cause Analysis From a Stack Trace
Walks a stack trace and surrounding code step by step to isolate the true root cause and propose a minimal verified fix.
Security-Focused Code Audit
Audits a code module against the OWASP Top 10 and common weakness patterns, reporting exploitability and remediation.
Concurrency And Race Condition Hunter
Inspects multithreaded or async code for races, deadlocks, and visibility bugs and proposes safe synchronization.