Legacy code

From bdfinst/agentic-dev-team · View interactive version

Legacy code is an AI agent skill from the GitHub repository bdfinst/agentic-dev-team. Safely modify code that lacks tests. Use whenever tasked with changing code without test coverage — apply characterization tests and dependency-breaking techniques before making any behavioral changes.

How to use this skill

Skill file

Legacy Code

Role: worker. This command applies characterization-testing and

dependency-breaking techniques directly to the code under change.

Overview

Techniques for safely modifying code that lacks tests or has poor structure. Based on the principle that legacy code is code without tests (Michael Feathers' definition) — regardless of age. The goal is to get code under test before changing it, then improve structure incrementally.

Constraints

Core Concepts

Legacy Code Definition

Code without tests. A well-structured 6-month-old codebase with no tests is legacy. A messy 10-year-old codebase with comprehensive tests is not. The presence or absence of tests determines whether you can change code safely.

The Legacy Code Change Algorithm

  1. Identify change points — where in the code does the change need to happen?
  2. Find test points — where can you observe the effects of the change?
  3. Break dependencies — make the code testable without changing its behavior
  4. Write tests — characterization tests that lock down existing behavior
  5. Make changes and refactor — now that tests protect you, modify behavior and improve structure

Seams

A seam is a place where behavior can be altered without editing the code under test:

Characterization Tests

Tests that document what the code actually does, not what it should do. They lock down existing behavior so you can detect unintended changes. Characterization tests answer: "If I make a change here, what breaks?"

Patterns

Characterization Test Procedure

  1. Find the code area you need to change
  2. Write a test that calls the code and lets it fail to reveal actual output
  3. Update the test assertion to match observed behavior
  4. Repeat until the change area and its immediate dependencies are covered
  5. Use these tests as a safety net — any behavioral change will now show as a test failure

Finding Test Points (effect reasoning)

Step 2 of the algorithm — where can you observe the effects of the change? — is its own skill in tangled code. Before writing tests, reason about effects and place verification where it has leverage. The full method (effect sketches, interception points, pinch points, and the editing-safety techniques — Lean on the Compiler, Preserve Signatures, Single-Goal Editing) is in CODE0. In brief:

Graph-assisted exploration. Prefer CodeGraph/Repowise over raw Grep when identifying change points, tracing effects outward to interception/pinch points, and mapping the dependencies a seam needs to break — a pre-built call graph surfaces callers, callees, and blast radius faster and more completely than text search. See CODE1 for tool selection and the fallback contract.

Dependency Breaking Techniques

The everyday shortlist below covers most cases. The full 24-technique catalog (globals/singletons, non-OO seams, hard parameters, monster methods, with seam type and risk for each) lives in CODE0 — read it when a break here doesn't present a seam.

| Technique | When to Use | Risk |

| --- | --- | --- |

| Extract Interface | Class has a concrete dependency you need to substitute | Low |

| Extract Method | Long method with embedded logic you need to isolate (safest starting point) | Low |

| Parameterize Constructor | Class creates its own dependencies internally | Low |

| Extract and Override Call / Factory Method | A specific call or new in a constructor blocks the test | Low–Med |

| Subclass and Override Method | Need to neutralize or replace specific behavior in tests | Medium |

| Sprout / Wrap Method or Class | Adding new behavior to untested code (see decision below) | Low–Med |

| Adapt Parameter | Method depends on a type you can't use in tests | Medium |

Sprout vs. Wrap Decision

Strangler Pattern

Incrementally replace legacy components by routing new behavior through new code while old code remains operational:

  1. Identify a bounded area of legacy functionality
  2. Build new implementation alongside the old
  3. Route new requests/features through new code
  4. Gradually migrate existing behavior to new code
  5. Remove old code only when fully replaced and verified

Output

Report the legacy code analysis: identified change points, test points, dependency breaks needed, characterization test targets, and the recommended refactoring sequence. Be concise — bullet list format; skip background explanation.

When to Apply

| Situation | Apply? |

| --- | --- |

| Modifying code without tests | Yes |

| Adding behavior to poorly structured code | Yes |

| Migrating legacy components to new architecture | Yes |

| Greenfield development | No |

| Code already well-tested and well-structured | No |

| Pure deletion of unused code | No |

Guidelines

  1. Never change behavior and structure in the same step. Refactor under green tests only.
  2. Write characterization tests before modifying legacy code. The tests document current behavior, not desired behavior.
  3. Prefer the smallest dependency break that gets code under test. Aggressive refactoring without test coverage creates risk.
  4. Sprout when the new behavior is clearly separable; wrap when existing callers must see the new behavior transparently.
  5. Every dependency break is temporary scaffolding — revisit and clean up once test coverage allows broader refactoring.
  6. If you cannot find a seam, extract method first. Extract Method is the safest starting point for almost any dependency break.
  7. Characterization tests are not a substitute for acceptance tests. They lock behavior; acceptance tests define behavior.

Integration

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.