← Knowledge

Public EcoRouter Knowledge · AI-generated content ·

How do I ask an AI to refactor messy code without breaking existing features?

The question

How do I ask an AI to refactor messy code without breaking existing features?

Balanced

To safely refactor code using an AI, the key is setting strict boundaries around behavior and taking an incremental approach.

Here is a practical strategy and a ready-to-use prompt template.


1. The Strategy: Test First, Refactor Second

If you don't already have unit tests for the code, ask the AI to write them before touching the implementation:

  1. Step 1: Give the AI the messy code and ask: "Generate comprehensive unit tests that cover all edge cases and document the current behavior."
  2. Step 2: Run those tests locally to verify they pass against the original code.
  3. Step 3: Use those tests as a safety net when refactoring.

2. What to Explicitly Tell the AI

When prompting for the refactor, always include these four constraints:

  • Strict behavior preservation: Explicitly forbid changing function signatures, return types, error handling, or side effects.
  • Define what "clean" means: Be specific (e.g., "split into small single-purpose functions," "remove nested conditionals," "apply idiomatic Python typing").
  • Ask for an explanation of changes: Having the AI document what it changed makes reviewing easier.
  • Keep the scope small: Refactor one function, class, or logical module at a time rather than an entire file at once.

3. Prompt Template

You can copy and adapt this template:

Role: You are an expert software engineer refactoring legacy code.

Task: Refactor the following code to improve readability, maintainability, and structure without altering any existing behavior or public APIs.

Constraints:

  1. Zero functional changes: Function names, argument types, return values, exceptions, and side effects must remain 100% backward-compatible.
  2. Edge cases: Preserve all implicit logic, edge cases, and error handling. Do not "fix" bugs or alter business logic unless asked.
  3. Goals:
    • [e.g., Reduce nesting with early returns / guard clauses]
    • [e.g., Break large functions into small, well-named helpers]
    • [e.g., Add clear type hints and docstrings]
  4. No external dependencies: Do not introduce new libraries or dependencies.

Output:

  • The refactored code.
  • A brief bulleted summary of structural changes made and why.

Code to refactor:

code
[paste code here]

4. Reviewing the Result

Before committing the refactored code:

  • Run your test suite.
  • Review the diff rather than just reading the new code — this makes it obvious if an edge case or log statement was accidentally deleted.

Answering this used an estimated 75% less than EcoRouter’s configured frontier baseline

Routed to
Balanced
Tokens
375 in / 1,010 out
Cost
$0.0041
Baseline
$0.02

Figures recorded by EcoRouter when this answer was generated, and fixed at that moment. Cost comparisons are against a configured reference model, not a measurement of electricity, carbon or water.

Ask about this

Ask EcoRouter a follow-up using this Knowledge as context. Nothing becomes public unless you choose to publish it.

Ask a follow-up
1 view

Comments

No comments yet.