# Legacy Code Explainer & Refactor Planner

Explains what an unfamiliar or legacy piece of code actually does, then proposes a safe, incremental refactor plan instead of a risky full rewrite.

## Prompt

You are a senior engineer who specializes in safely understanding and modernizing legacy code without breaking production.

Language/framework: {{language_framework}}
Legacy code to analyze:
{{legacy_code_block}}
Why I need to understand/change it: {{reason_for_review}}

Do the following:
1. Explain in plain language what this code actually does, step by step, including any non-obvious side effects, global state mutations, or implicit dependencies you can identify.
2. Flag anything that looks risky, outdated, or likely to cause bugs if touched carelessly (e.g., tightly coupled logic, missing error handling, deprecated patterns).
3. Propose an incremental refactor plan broken into small, independently shippable steps, each one safe to deploy on its own without requiring the full refactor to be finished first. Order them from lowest risk to highest risk.
4. For the riskiest identified area, suggest a specific way to add test coverage before refactoring it, so changes can be verified safely.

## Best for

Developers inheriting unfamiliar codebases, working with legacy systems, or picking up old code who need to understand it safely before making changes, without risking a destabilizing full rewrite.

## Compatible tools

- Claude
- ChatGPT

## How to use

- Paste the legacy code along with the language/framework it's written in.
- Explain why you need to understand or change it, since the refactor plan depends on your actual goal.
- Read the plain-language explanation fully before making any changes to avoid missing hidden side effects.
- Follow the incremental refactor steps in order, shipping and verifying each one before moving to the next.

## Customization tips

- If the codebase has zero existing tests, mention that explicitly so the refactor plan front-loads test coverage before any structural changes.
- For very large files, paste the code in logical chunks and ask for the analysis to build on previous chunks.
- If you have a deadline constraint, mention it so the refactor plan can be scoped to only the minimum safe change needed right now.

## Example input

Language: PHP. Legacy code: [a 200-line function handling order processing with nested conditionals and direct database calls mixed with business logic]. Reason: Need to add a new discount rule without breaking existing checkout flow.

## Example output

A plain-language walkthrough of the order processing logic identifying an undocumented side effect where inventory is decremented mid-function rather than at the end, a flag on the tightly coupled database calls making the logic hard to test in isolation, a 4-step incremental refactor plan starting with extracting the discount logic into its own testable function before touching the core flow, and a suggestion to add integration tests around the current checkout behavior before making any changes to lock in existing behavior.
