Explain and Improve Legacy Code
Copilot Chat explains code well when you highlight it first. Cursor can read the full file for context. The three-step structure (explain, improve, verify) prevents the common mistake of 'improving' code while accidentally changing its behavior.
Explain what this code does, then improve it: File: {{file_path}} Function/section: {{code_section}} Step 1 — Explain: - What does this code do? (plain English, not just restating the code) - Why was it likely written this way? (infer from the era, patterns, and constraints visible) - What are the hidden assumptions or gotchas? - What would break if {{specific_scenario}} happened? Step 2 — Improve: - Rewrite using modern {{language}} patterns ({{target_version}} or later) - Replace any deprecated APIs with current equivalents - Add type annotations if they're missing - Simplify without changing behavior (measure complexity: before vs after) - Add error handling for the failure modes identified in Step 1 Step 3 — Verify: - Show test cases that prove the rewrite behaves identically to the original - Note any subtle behavior changes and flag them with [BEHAVIOR CHANGE] Keep the improved version under {{max_lines}} lines. If it can't be simplified that much, explain what's inherently complex.
Variables to customize
Why this prompt works
Copilot Chat explains code well when you highlight it first. Cursor can read the full file for context. The three-step structure (explain, improve, verify) prevents the common mistake of 'improving' code while accidentally changing its behavior.
Save this prompt to your library
Organize, version, and access your best prompts across ChatGPT, Claude, and Cursor.
Related prompts
Get thorough code reviews with actionable feedback tailored to your language, framework, and standards.
Context-Aware Code CompletionProviding the surrounding code and project context lets the model match existing patterns exactly. The constraint against modifying existing code prevents unwanted side effects.
Inline Code SuggestionConstraining suggestions to match existing style and scope produces insertions that feel native to the codebase. The 'no explanation' rule mimics real inline completion behavior.
Code ExplanationThe audience level parameter adjusts complexity automatically. Requiring a usage example ensures the explanation is practical, not just theoretical.