refactoring
Refactoring
Improve the design of working code without changing what the system does.
Context
Refactoring keeps implementation quality from decaying between releases. It is not cleanup for its own sake. In Prodcraft, refactoring is justified when reviews, tech-debt evidence, or repeated implementation friction show that the current structure is increasing future cost or risk.
The constraint is strict: externally observable behavior must stay stable. If behavior changes, the work is feature development or defect fixing, not refactoring.
Inputs
- source-code -- The current implementation that needs structural improvement.
- test-suite -- The safety net that proves behavior has not changed.
- review-report -- Concrete findings from review that identify structural problems worth fixing.
- tech-debt-registry -- Evidence that the refactor addresses recurring cost rather than personal preference.
Process
Step 1: Choose One Structural Problem
Pick a focused target such as duplication, oversized functions, unstable seams, hidden dependencies, or naming that obscures intent. Define the invariant that must not change.
Avoid combining unrelated cleanup goals into one refactor.
Step 2: Strengthen the Behavioral Safety Net
Before moving code, verify the tests protect the behavior that matters. Add characterization or regression coverage first if the current suite would allow a silent behavior change.
Step 3: Refactor in Reversible Steps
Make the change as a sequence of small transformations:
- rename for clarity
- extract or inline with tests green
- move responsibilities to cleaner seams
- remove duplication after the new shape is proven
Run the relevant tests after every step.
Step 4: Prove the Design Actually Improved
Before review, state what got better: smaller surface area, clearer boundaries, lower duplication, simpler control flow, or safer extension points. If you cannot name the gain, the change is probably unnecessary.
Outputs
- source-code -- Behaviorally equivalent code with improved structure and lower ongoing change cost.
Quality Gate
- Tests prove behavior stayed stable
- The refactor addresses a concrete structural problem
- The change is narrow, reviewable, and reversible
- Complexity, coupling, or duplication is measurably lower
- No opportunistic feature work is mixed into the refactor
Anti-Patterns
- Refactor by intuition -- changing structure because it "feels cleaner" without evidence of real cost.
- Behavior change in disguise -- sneaking in requirement changes under the label of cleanup.
- Unsafe large-step rewrite -- moving too much code at once to reason about regressions.
- Testless cleanup -- improving structure without a safety net strong enough to catch breakage.
Related Skills
- feature-development -- produces the working slice that may reveal structural friction
- tdd -- provides the behavioral safety net
- code-review -- validates the refactor stayed behavior-preserving and worthwhile
- tech-debt-management -- prioritizes larger or recurring refactor targets
Distribution
- Public install surface:
skills/.curated - Canonical authoring source:
skills/04-implementation/refactoring/SKILL.md - This package is exported for
npx skills add/updatecompatibility. - Packaging stability:
beta - Capability readiness:
beta
More from yknothing/prodcraft
system-design
Use when reviewed requirements or specifications are ready and the team must decide high-level architecture, component boundaries, integration seams, or brownfield coexistence strategy before API design, technology selection, or task planning.
6ci-cd
Use when a reviewed implementation slice needs an automated build, test, and deployment pipeline, especially when brownfield rollback, release-boundary checks, contract/integration gates, and staged delivery must be explicit before shipping.
6intake
The mandatory gateway for all new engineering work. Triage and route new products, apps, features, migrations, tech-debt, or any 'not sure where to start' request to the correct lifecycle path. Use before starting design or implementation. Do not use for ongoing tasks, specific debugging, or PR reviews.
6feature-development
Use when a reviewed task slice has tests or acceptance targets and the team must turn it into a small, mergeable implementation increment without expanding scope, breaking contracts, or hiding release-boundary risk.
6monitoring-observability
Use when a live service or newly delivered release needs actionable telemetry, dashboards, and alerts that expose real user-impactful boundaries, especially when brownfield coexistence rules, unsupported-flow safety, rollback health, or queue/backfill behavior must be visible before incidents escalate.
6incident-response
Use when a live production issue needs coordinated containment, severity triage, stakeholder communication, and evidence capture, especially when a recent release, brownfield coexistence rules, rollback decisions, or unresolved contract boundaries must be handled before root-cause work.
6