Refactoring & Modernization
Compatible with Cursor, Claude Code, Cline, Roo Code, Antigravity, GitHub Copilot

Safe Legacy Code Modernization with Golden Master Tests

Safely modernize fragile legacy codebases using Martin Fowler refactoring patterns and characterization tests.

RefactoringClean CodeDesign PatternsLegacy Code
Skill Objective

Refactor legacy, untested, or deeply nested code using characterization tests (Golden Master) to guarantee zero behavioral regressions.

Execution Workflow Procedure

Ordered steps the coding agent executes when performing this skill.

1

Build Characterization Test Safety Net

Write characterization tests that capture the current exact output behavior across wide input matrices before modifying production code.

2

De-Nest and Apply Guard Clauses

Flatten deep conditional nesting with early returns and guard clauses without changing execution semantics.

3

Apply Micro-Refactorings

Execute small atomic refactorings (Extract Method, Parameter Object, Strategy Pattern) running characterization tests after each step.

4

Introduce Strict Types and Pure Functions

Replace magic numbers and mutable variables with strict types, constants, and pure deterministic functions.

Agent Safety Guardrails & Anti-Hallucination Rules

  • DO NOT alter business behavior or fix unrelated bugs in the same refactoring pull request.
  • DO NOT modify legacy code without a passing characterization test harness.

Expected Output Format

Characterization tests and step-by-step refactored code.

🛡️ Characterization Test Suite: Golden Master test cases capturing legacy behavior.
🛠️ Modernized Clean Code: Decoupled, strongly typed implementation.

Domain Verification Checks

Golden Master Safety: Ensures 100% output equivalence before and after changes.
Flattened Guard Clauses: Eliminates deeply nested arrow anti-patterns.
Pure Function Isolation: Extracts business logic into testable pure functions.

Example Agent Invocations

"Safely refactor this 300-line nested report generation function into modern TypeScript with characterization tests."
"Modernize this legacy Java billing calculation method using the Strategy pattern."

Install / Configure This Skill

# Place in .cursorrules or create .cursor/rules/safe-legacy-code-refactor.mdc
# Trigger: Whenever you perform safe legacy code modernization with golden master tests

---
name: safe-legacy-code-refactor
description: Modernize legacy code safely using characterization tests and atomic Martin Fowler refactoring steps.
version: 1.2.0
compatibility: [Cursor, Claude Code, Cline, Roo Code, Antigravity]
---

# Safe Legacy Code Modernization with Golden Master Tests

## Objective
Refactor complex legacy code without introducing regressions by creating characterization tests first and applying atomic refactoring steps.

## Workflow
1. **Characterization Tests**: Write Golden Master tests capturing current outputs across diverse inputs.
2. **Flatten & Guard**: Eliminate nested if-statements using early return guard clauses.
3. **Atomic Refactoring**: Extract methods and introduce design patterns (Strategy, Factory) while keeping tests green.
Complementary Agent Prompt

Need a quick one-time prompt for chat?

Try the Legacy Code Safe Refactoring with Characterization Tests — a lightweight version designed for 1-click copying directly into Claude, ChatGPT, or Cursor chat.

View Prompt

SprintKit Workflow Integrations

Complementary tools and checklists to pair with this agent workflow.

Related Agent Skills

Explore related procedural workflows in this discipline.

All Skills

God Class & Monolithic Method Decomposition (SOLID)

Decompose monolithic 1,000+ line classes into Single Responsibility components using SOLID design patterns.

Design PatternsSOLIDOOP+2
3 workflow stepsView skill

Comprehensive Unit Test Suite Generation (AAA Pattern)

Generate mutation-tested unit tests with Arrange-Act-Assert structure, boundary cases, and proper mock boundaries.

TestingJUnitVitest+3
3 workflow stepsView skill

Strict TypeScript Type Safety & Soundness Audit

Audit TypeScript code for type safety, type narrowing, generics, mutation traps, and runtime boundary validation.

TypeScriptNode.jsClean Code+1
4 workflow stepsView skill