Skip to content

skillCode quality

Legacy Code Characterization

Records what untested legacy code actually does by writing characterization tests first, so you can change or refactor it without guessing at behavior.

npx workspacetemplates add legacy-code-characterization --tool claude-code

Writes 1 file to .claude/skills/legacy-code-characterization/

.claude/skills/legacy-code-characterization/SKILL.md28 lines · 1.8 KBFile
---
name: legacy-code-characterization
description: Records what untested legacy code actually does by writing characterization tests first, so you can change or refactor it without guessing at behavior.
---

# Legacy code characterization

Describe what the code does, not what it should do. Do not fix anything in this step.

## Steps

1. Choose the seam: one function, class or endpoint. Read it and list its inputs, outputs, side effects (database, files, network, clock, randomness) and callers.
2. Make it testable without changing behavior. Replace time, randomness and network with test doubles at the edge, and use a temporary database or fixtures. If the code cannot be called without a rewrite, use the smallest wrapper or an extracted-but-identical function, and say so.
3. Collect realistic inputs: from existing fixtures, logs, call sites and sample data. Add boundaries: empty, zero, null, maximum length, unusual encodings and error cases.
4. Write a test per input that asserts the current output. If you do not know the output, run the code, read the result and paste it in as the expected value. Snapshot tests are fine for large outputs.
5. Run the suite three times to catch flaky tests. A test that fails randomly is not a safety net; find the hidden dependency.
6. Check coverage for the target (`--coverage`) and add inputs until each branch runs at least once.
7. Mark suspicious results. When the output looks wrong, keep the test, add a comment `// current behavior, looks like a bug` and list it.

## Rules

- Change no production logic in this step, except the minimal seam from step 2.
- Name tests after the input, not the expected value.
- Never delete a failing characterization test; ask first.

## Report

Tests added, branch coverage reached, the seams introduced, and a list of suspected bugs with their inputs.

What it does

You cannot refactor code if you do not know what it does today, and old code rarely has a spec. A characterization test asks the code itself: it runs real inputs, records the output and fails when that output changes.

This skill builds that safety net around one function or module before any edit. The tests describe current behavior, bugs included, and the bugs are listed separately so you decide what to fix.

Example

you › We need to change calcLateFee() in billing/fees.php but it has no tests. Pin it down first.

agent › Wrapped the clock behind a `$now` parameter (default unchanged). Wrote 23 characterization tests: weekend due dates, leap days, zero balances, negative credit, and the 90-day cap. Branch coverage of the function 100%, stable over 3 runs. Suspected bugs: fee is charged on credit balances (test line 140) and rounds down at exactly 0.5.

Questions

Do these tests mean the behavior is correct?
No, only that it is current. Suspected bugs are listed apart so you can change the expected value on purpose once you decide.
What if the code cannot be tested at all?
The skill makes the smallest behavior-neutral change that opens a seam and reports exactly what it changed before going on.
  • API Deprecation Handling

    SkillCode quality

    Free
  • Code Duplication Removal

    SkillCode quality

    Free
  • Code Smell Detection

    SkillCode quality

    Free
  • Concurrency Bug Analysis

    SkillCode quality

    Free