StructuredFor DevelopersCode Writing & Debugging

Refactor Legacy Code.

When inheriting old code that works but is unreadable, untestable, or hard to extend.

ChatGPT Β· Claude Β· GeminiΒ·IntermediateΒ·~900 tokens
Curated by the AIPP team
Last updated 14 May 2026 Β· v3
refactor-legacy-code-4.md Β· 900 words
You are a senior {{role}} brought in to help a developer or tech professional complete a {{use_case}} task.

# Context
- Pack: Developers & Tech Professionals
- Category: Code Writing & Debugging
- Use case: Refactor Legacy Code
- Source task:
  - Refactor the following {{language}} code while preserving exact behaviour: {{code}}. Goals: improve readability, reduce duplication (DRY), follow modern {{language}} patterns, reduce cyclomatic complexity where possible, and prepare it for easier unit testing. Show the before and after side by side, and list every change made with a one-line reason.

# Goal
Refactored code with a change log explaining every modification and why it improves the codebase.

# Constraints
- Produce a complete, usable first draft in one response.
- Avoid generic filler, vague advice, and unsupported claims.
- Make the output specific, practical, and ready to use.

# Output
Refactored code with a change log explaining every modification and why it improves the codebase.

The variables to fill in

PlaceholderWhat to put thereExample
{{role}}Rolerefactoring specialist
{{use_case}}Your specific valuerefactor legacy code
{{language}}LanguagePython
{{code}}Codepaste the function or component here

How to customize this prompt

  1. Replace each {{double-curly}} with your real context.
  2. Adjust the constraints section to match your tone β€” formal, casual, blunt.
  3. If the engagement is recurring, change the duration line to mention milestones rather than days.
  4. Run it in your tool of choice. The output should be ready to paste with at most one small edit.

When to use

When inheriting old code that works but is unreadable, untestable, or hard to extend.

PRO TIP

Refactor in phases β€” one concern per session. Trying to fix structure, naming, and performance simultaneously creates risky changes.

Related prompts

Structured

Blog Post Drafting Engine

Write a complete, SEO-optimised blog post on the given topic. Include a compelling headline, an engaging introduction, 4-5 subheadings with detailed body paragraphs, and a strong conclusion with a cal

Structured

Email Newsletter Writer

Write a complete email newsletter including subject line, preview text, opening hook, main body content (3 short sections), and a clear call to action.

Structured

YouTube Video Script Writer

Write a complete YouTube video script including a strong hook (first 30 seconds), structured main content with transitions, and a closing that encourages likes, comments, and subscriptions.

Structured

LinkedIn Article Builder

Write a complete LinkedIn article that establishes professional authority, shares a genuine insight, and encourages professional discussion.

β˜… THIS PROMPT IS IN A PACK

The Developer Toolkit Pack

250 technical prompts for code review, documentation, architecture planning, debugging, test writing, API design, and career growth β€” built by developers for developers.

Browse more prompts β†’