Behavior-Preserving Refactor Planner
Break a risky cleanup into verifiable steps that preserve user-visible behavior while improving an AI-built codebase.
Vibe coders cleaning up rapidly generated code while protecting current behavior and avoiding a full AI rewrite.
You are a refactoring strategist who specializes in improving maintainability without unintended product behavior changes.
Inputs:
1. Refactoring target and maintainability problem: {{refactor_target}}
2. Current code, dependencies, and public interfaces: {{current_implementation}}
3. Behavior that must remain unchanged: {{behavior_contract}}
4. Existing tests and known fragile areas: {{test_context}}
5. Timeline, deployment, and scope constraints: {{constraints}}
Do the following:
1. Describe the code smell or maintenance cost using concrete evidence and distinguish it from unrelated style preferences.
2. Map public behavior, side effects, data contracts, timing assumptions, accessibility behavior, and integration points that the refactor must preserve.
3. Recommend a sequence of small, independently testable transformations with checkpoints, avoiding simultaneous feature changes and broad rewrites.
4. Define characterization tests or snapshots needed before editing, plus invariant checks, performance guardrails, and rollback boundaries for each step.
5. Produce a refactor plan, dependency map, step-by-step prompts for the coding assistant, validation commands, risk register, and completion criteria. If the current behavior appears defective, document it separately rather than silently changing it during the refactor.How to use
- State the maintenance problem with examples.
- Define all behavior that must not change.
- Add characterization tests before extraction.
- Keep feature work out of the refactor sequence.
Example input
Target: A 920-line React booking page containing data fetching, pricing calculations, form state, validation, and modal markup. Problem: small edits cause regressions and duplicated price logic. Contract: same URLs, field order, keyboard behavior, totals, analytics events, and API payload. Tests: two Playwright happy paths; refund pricing and keyboard focus are fragile. Constraints: no visual redesign and no API changes in this sprint.
Example output
The plan first adds characterization tests for pricing variants, payloads, analytics, and focus restoration. It then extracts pure pricing functions, request adapters, validation, and presentational sections in separate commits, keeping the existing page as orchestrator. Each extraction has a narrow prompt and rollback point. A suspected refund-rounding bug is documented as a separate future change rather than corrected during the behavior-preserving refactor.
Customization tips
- — Use small commits with one transformation each.
- — Extract pure logic before stateful orchestration.
- — Include accessibility and analytics behavior in the contract.
- — Track known defects separately from preserved behavior.
Tags
FAQ
What is this prompt for?
How should I customize it?
Are there any limitations?
How is it different from a basic prompt?
Related Prompts
All Vibe Coding prompts →Accessibility Acceptance Criteria Builder
Turn a feature brief into concrete accessibility behavior, acceptance criteria, and tests that an AI coding assistant can implement and verify.
AI Coding Context Pack Builder
Create a compact, reliable context pack that helps an AI coding assistant understand an existing app before making changes.
App Performance Budget Planner
Set measurable performance budgets for an AI-built app and turn them into architecture choices, tests, and regression gates.
Related Skills
Code Refactor Advisor
Reviews a code snippet and returns a prioritized refactor plan — readability, structure, and performance issues, each with a concrete fix.
Engineering Judgment Coach
Makes an AI coding agent pause and reason like a senior engineer before writing code — right layer, reuse over rebuild, the right pattern, and honest refactor timing.
Code Review & Refactor Assistant
Reviews a pasted code snippet for bugs, readability, and performance issues, then delivers a prioritized refactor with explanations for each change.
Related Articles
How do I use AI tools like Lovable or Cursor to build an app?
AI app builders work best when users provide a scoped feature request, data model, user flow, UI constraints, acceptance criteria, and test cases.
What is vibe coding, and where does it break?
Vibe coding can help users prototype quickly with natural language, but it breaks when requirements, security, data model, testing, and maintainability are ignored.