Explain legacy code
Professional structure: role, context, objective, details, steps, format, criteria and self-check. Designed for better results, not guaranteed ones: always review the answer.
Never use real personal data. If you need to, run it through the data cleaner.
## Role Act as a software architect who documents legacy systems. ## Context I have to change code I did not write and that has no documentation. ## Objective Understand what it does, how it flows and what may break when I change it. ## Details (fill in what is in square brackets) - [code] - [language] - [change I want to make] ## Step-by-step instructions 1. Summarise the purpose of the code in three sentences. 2. Describe the main flow step by step. 3. List dependencies, side effects and global variables. 4. Flag the risks of the change I want to make. ## Output format Summary, numbered flow, dependency table and list of risks with suggested tests to write first. ## Quality criteria - It separates what is certain from what is assumed. - It uses the real names in the code. - It does not rewrite the code unless asked. ## Self-check Before answering, check every quality criterion. If information is missing, ask me instead of making it up, and at the end state what you have assumed.
Objective
Understand what it does, how it flows and what may break when I change it.
When to use it
This prompt is invaluable when you have inherited a 'black box' system and need to make updates without breaking existing functionality. It is perfect for those daunting moments where you are staring at thousands of lines of undocumented, tangled logic. However, it is not a substitute for a full code audit or a replacement for proper unit testing. If the codebase relies heavily on binary blobs or highly obfuscated machine code, the AI may struggle to provide an accurate dependency analysis.
Worked example: input (fictional data)
Worked example: full response
Variants
Quick Impact Summary
Instead of a full breakdown, focus strictly on the immediate impact. Summarise the purpose in one sentence, then list only the critical side effects and potential breaking changes resulting from my proposed edit. Keep the output concise and bulleted for rapid reading before I commit these specific changes to the production environment.
Junior Developer Guide
Explain this code as if I am a junior developer who is new to the team. Use simple, non-technical analogies to describe the logic flow. Highlight why the code is written in this specific way and explain the risks in plain English, avoiding complex architectural jargon where possible to ensure full understanding.
Security-First Analysis
Prioritise a security-first review of the code. Identify any potential vulnerabilities, unsafe practices, or insecure data handling patterns. Before explaining the flow, list any security risks associated with the existing code structure and highlight how my proposed change might introduce new vulnerabilities or expose sensitive data to potential exploitation.
Common mistakes
- Refactoring everything before understanding it.
- Pasting code that contains credentials.
- Ignoring warnings about global variables used elsewhere in the codebase. How to avoid it: Always check if the global variable is shared by searching the entire directory structure.
- Assuming the AI understands the entire system context from a single file. How to avoid it: Provide context about surrounding modules or related files to get a more accurate picture.
- Failing to verify the suggested tests against your actual development environment. How to avoid it: Run the AI-suggested tests in a sandboxed staging environment before applying changes to production.
- Overlooking deprecated library calls within old legacy syntax. How to avoid it: Ask the AI to specifically flag any functions or libraries that are no longer supported.
Related guide
General and assistant-specific tips
Tips by assistant
- Claude: it handles long documents; paste the full text instead of loose fragments.
- Claude: separate your data with tags such as <document>…</document>; Anthropic recommends this so it can tell instructions from content.
Getting the most out of it
- Paste the prompt as is and only replace what is in [brackets].
- If the answer does not fit, do not start over: ask for “shorter”, “more formal” or “as a table”.
- Always review the result before using it: AI can get facts, dates or names wrong.
Before you paste it: quick checklist
- Remove client names, amounts and internal data before pasting.
- Say who the text is for and which tone you want.
- Ask for a specific length: “120 words max” or “three bullet points”.
- Read the result aloud and fix anything you wouldn’t say yourself.
How to adapt it to your assistant
The template works the same in ChatGPT, Gemini, Copilot or Claude because it only uses plain language. If your assistant supports fixed instructions (custom instructions, Gems or projects), you can store your preferred tone and format there and paste only the variable part. On a free plan, split long requests into two or three shorter messages.