Skip to content
Back to skills

Refactor Safely

ASecurity

Change the structure of code without changing what it does, by pinning behaviour with tests first and moving in small verified steps. Use for "refactor this module", "clean up this code", "extract this into", "remove the duplication", "split this file".

  • 11 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 5, 2026
ai-agentsgorefactoringperformancedocumentation

Security analysis

A100/100

Scanned December 5, 2026

npx -y skills add kinzarra/vibetrack --skill refactor-safely --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Refactor Safely?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Refactor Safely
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/kinzarra-refactor-safely/badge)](https://www.skillsdirectory.com/skills/kinzarra-refactor-safely)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: refactor-safely
description: Change the structure of code without changing what it does, by pinning behaviour with tests first and moving in small verified steps. Use for "refactor this module", "clean up this code", "extract this into", "remove the duplication", "split this file".
---

# Refactor safely

The result is code with a better structure and exactly the same observable behaviour, proven by tests that passed before the first step and after every step.

## 1. Define the goal and the boundary

- Write the goal as one sentence with a measurable end: "the pricing rules live in one module and the three copies are gone", not "improve the code".
- Name the reason from the task: a feature that is hard to add, a bug source, duplication that drifted. A refactoring without a reason is not started.
- List what counts as behaviour and must not change: return values, errors and their types, side effects and their order, stored data and formats, public names and signatures, log lines and metrics that anything parses, timing where callers depend on it.
- Find every caller of what will change, including dynamic ones: reflection, string lookups, configuration files, templates, other services, documentation examples. Search by name and by string.

## 2. Pin behaviour first

- Run the project's full test command and record the result. Failures that already exist are written down before anything is touched.
- Check that the code to be changed is really exercised. Use the coverage report if the project has one, or break a line on purpose and see whether a test notices; restore the line.
- Where coverage is missing, add characterization tests first: feed real or representative inputs, record what the code does now, and assert exactly that, including behaviour that looks wrong. A suspected bug is recorded and reported, not fixed here.
- Pin through the public entry points, so that the tests survive the restructuring.
- For output-heavy code (reports, generated files, serialized payloads), save the current output for a set of inputs and compare byte for byte after each step.

## 3. Move in small steps

One step is one kind of change, and the tests run after each:

1. Rename (with the project's tooling when it has one).
2. Extract a function, a class or a module, with no change to its logic.
3. Move code to its new place and leave a thin forwarder where callers still point to the old one.
4. Switch the callers to the new place, a few at a time.
5. Remove the forwarder and the dead code once nothing refers to them.
6. Only then simplify the internals.

- After each step: build, lint, type check and tests pass. If a step fails and the cause is not clear within a few minutes, undo the step and take a smaller one.
- Keep each step small enough to describe in one line. Keep the list of steps; it goes into the report.
- Replace an implementation by the parallel route when it is risky: build the new one beside the old, compare the results of both on the same inputs, then switch.

## 4. What does not belong in the change

- No new features and no bug fixes. A bug found on the way is reported with file, line and input. If it must be fixed, that is a separate change.
- No changes to the public interface, the stored data format, the schema, the dependencies or the configuration, unless the task says so.
- No formatting sweep of files the goal does not touch, and no mass renames outside the boundary.
- No edits to existing tests to make them pass. A test changes only when it referred to a moved or renamed internal; its assertions stay the same.
- No performance changes mixed in. If the refactoring could change speed or memory on a hot path, measure before and after.

## 5. When to stop

- The goal sentence is true. Stop there, even if more could be improved; list the rest as follow-ups.
- The change passes about 400 changed lines or 15 files: finish the current step, hand in what is complete and describe the remaining steps.
- The next step needs a behaviour change, an interface change or a decision about design: stop as blocked with the question.
- The tests cannot pin the behaviour and adding them needs changes that are themselves risky: report this and propose the order (make it testable first).

## 6. Report

1. **Goal** and whether it was reached.
2. **Steps taken**, one line each, in order.
3. **Proof of unchanged behaviour**: the test command and its results before and after, the characterization tests added, the output comparisons made.
4. **What moved where**: old location to new location, for reviewers and for anyone with open work on those files.
5. **Bugs and oddities found** and left as they were, with file and line.
6. **Follow-ups**: remaining steps and things worth doing next.

Rules: work only in the task's worktree. Do not commit, push or merge unless the task says so; if the task allows commits, one commit per step. When blocked, state exactly what is needed.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.