Article30 mins

Writing Prompts for Refactoring

Refactoring is one of the highest-value applications of vibe coding. A well-crafted refactoring prompt can accomplish in 30 seconds what would take an hour of manual work — and often produces cleaner results because the AI isn't biased by familiarity with the existing code.

Why AI Excels at Refactoring

Refactoring is the sweet spot for AI assistance because:

  • It's pattern-based: Most refactoring follows well-known patterns (extract method, introduce parameter object, replace conditional with polymorphism).
  • The input and output are both code: The AI can see what exists and produce what should exist.
  • Correctness is verifiable: Run the tests before and after — if they pass, the refactoring preserved behavior.
  • It's tedious for humans: Renaming 50 variables, restructuring 10 files, or converting class components to hooks is mechanical — but error-prone when done manually.

The Anatomy of a Refactoring Prompt

Every effective refactoring prompt has four components:

  • THE CODE: Paste the exact code to refactor. Don't summarize — paste it verbatim.
  • THE PROBLEM: What's wrong with the current code? "This function is 300 lines long", "There's duplicated logic in these 3 functions", "The naming is inconsistent."
  • THE GOAL: What should the refactored code look like? "Extract into 5–7 focused functions", "Deduplicate into a shared utility", "Rename everything to camelCase."
  • THE CONSTRAINTS: What must NOT change? "Keep the public API identical", "Don't change the database queries", "Preserve all existing error messages."

💡 Note

The constraints section is the most commonly forgotten — and the most important. Without constraints, the AI will "helpfully" change things you didn't ask it to change, breaking callers and introducing subtle behavior changes.

Refactoring Pattern Catalog

Here are prompts for the most common refactoring patterns. Adapt them to your specific code:

Pattern 1: Extract Method

"This function is too long. Extract the [describe logic block] on lines X–Y into a separate function called `[name]`. The extracted function should accept [parameters] and return [type]. Keep the original function's signature unchanged."

Pattern 2: Simplify Conditionals

"Replace the nested if-else blocks in this function with early returns (guard clauses). Each guard should handle one error condition and return early. The happy path should be the unindented, final code path."

Pattern 3: Deduplicate Logic

"These [N] functions share duplicated logic for [describe what's duplicated]. Extract the common logic into a shared utility function and update all callers to use it. Preserve the unique behavior of each original function."

Pattern 4: Convert Class to Functional

"Convert this React class component to a functional component using hooks. Map lifecycle methods to appropriate hooks (componentDidMount → useEffect, state → useState). Preserve all existing props and behavior. Keep the same component name and export."

Pattern 5: Introduce Types

"Add TypeScript types to this JavaScript file. Create interfaces for all function parameters that are objects. Use proper union types instead of 'any'. Add return type annotations to all functions. Create a types.ts file if needed."

Progressive Refactoring Strategy

For large refactoring tasks, don't try to do everything in one prompt. Use a progressive strategy:

  • Step 1: Ask the AI to ANALYZE the code and list all refactoring opportunities. Don't change anything yet.
  • Step 2: Prioritize the list. Which changes have the highest impact with lowest risk?
  • Step 3: Refactor one thing at a time. Each prompt should make one focused change.
  • Step 4: Test after each change. Verify the behavior is preserved.
  • Step 5: Move to the next refactoring opportunity.

This approach is slower per-step but dramatically more reliable. Each change is reviewable, testable, and reversible.

The Four-Eye Review

After any AI refactoring, review the output with these specific checks:

  • API preservation: Are all public function signatures identical to before?
  • Behavior preservation: Does the refactored code handle the same edge cases?
  • Naming conventions: Did the AI use your codebase's naming style, or its own?
  • Import changes: Did the AI add new dependencies you don't want?
  • Error handling: Did the AI accidentally swallow errors or change error messages?
  • Performance: Did the AI introduce unnecessary loops, copies, or allocations?

💡 Note

The most common review catches: renamed variables that don't match codebase conventions, added null checks that change behavior (returning undefined instead of throwing), and changed string interpolation style.

Key Takeaways

  • Always include: the code, the problem, the goal, and the constraints.
  • Constraints (what NOT to change) are the most important part of a refactoring prompt.
  • Use progressive refactoring for large tasks — one change per prompt.
  • Test after every refactoring step.
  • Review AI output for naming, API changes, and subtle behavior differences.
Back to Course