
Refactor
Improve maintainability of existing code through small, behavior-preserving refactors when structure blocks features or reviews surface smells.
Overview
Refactor is an agent skill most often used in Ship (also Build) that guides behavior-preserving structural improvements through small, tested steps.
Install
npx skills add https://github.com/github/awesome-copilot --skill refactorWhat is this skill?
- Five golden rules: preserve behavior, small steps, version control, tests, one concern at a time
- Explicit when-NOT-to-refactor guardrails (no tests, deadlines, rewrite temptation)
- Covers extract function, rename, break god functions, type safety, and design patterns
- Gradual evolution positioning—less drastic than full repo rebuilds
- Separates refactoring from feature work to avoid risky mixed commits
- 5 golden rules for safe refactoring
- Explicit when-NOT-to-refactor guardrail list
Adoption & trust: 17.8k installs on skills.sh; 34.6k GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
Your codebase works but is hard to read, extend, or review, and every small feature fights god functions and code smells.
Who is it for?
Indie devs with tests (or time to add them) who want incremental cleanup before shipping the next feature or merging a messy PR.
Skip if: Full rewrites, production-critical code with zero tests, tight-deadline crunch, or cosmetic churn on code that will never change again.
When should I use this skill?
User asks to clean up, refactor, or improve code structure; code is hard to maintain; functions or classes are too large; or smells block new features.
What do I get? / Deliverables
You get a disciplined refactor plan and edits that improve structure and maintainability while external behavior stays unchanged, with commits safe to bisect.
- Incremental refactor steps preserving external behavior
- Improved naming, smaller units, and reduced code smells
Recommended Skills
Journey fit
Spans multiple journey phases - primary shelf plus alternate fits below.
Canonical shelf is Ship because refactoring is safest when tied to review discipline, tests, and pre-release quality—not greenfield invention. Review subphase matches surgical cleanup, smell removal, and readability passes before merge or release.
Where it fits
Extract a 200-line Vue component method into composables so the next UI feature does not duplicate logic.
Rename opaque API handler variables and split validation from persistence before adding a new endpoint.
Address review feedback on nesting depth and missing types without slipping behavior changes into the PR.
Tighten a hot-path module after an incident postmortem identified readability risks during on-call edits.
How it compares
Gradual, test-backed refactoring workflow—not a ground-up repo rebuild or a feature-development skill.
Common Questions / FAQ
Who is refactor for?
Solo builders and small teams maintaining real codebases who need agent help restructuring modules without changing what users see.
When should I use refactor?
Use it in Ship (review) before merge or release cleanup; also in Build when adding features is blocked by structure, smells, or oversized functions—always with small steps and tests.
Is refactor safe to install?
The skill encourages safe practice but cannot prevent bad edits; review Security Audits on this page, keep version control, and require tests before trusting large automated refactors.
SKILL.md
READMESKILL.md - Refactor
# Refactor ## Overview Improve code structure and readability without changing external behavior. Refactoring is gradual evolution, not revolution. Use this for improving existing code, not rewriting from scratch. ## When to Use Use this skill when: - Code is hard to understand or maintain - Functions/classes are too large - Code smells need addressing - Adding features is difficult due to code structure - User asks "clean up this code", "refactor this", "improve this" --- ## Refactoring Principles ### The Golden Rules 1. **Behavior is preserved** - Refactoring doesn't change what the code does, only how 2. **Small steps** - Make tiny changes, test after each 3. **Version control is your friend** - Commit before and after each safe state 4. **Tests are essential** - Without tests, you're not refactoring, you're editing 5. **One thing at a time** - Don't mix refactoring with feature changes ### When NOT to Refactor ``` - Code that works and won't change again (if it ain't broke...) - Critical production code without tests (add tests first) - When you're under a tight deadline - "Just because" - need a clear purpose ``` --- ## Common Code Smells & Fixes ### 1. Long Method/Function ```diff # BAD: 200-line function that does everything - async function processOrder(orderId) { - // 50 lines: fetch order - // 30 lines: validate order - // 40 lines: calculate pricing - // 30 lines: update inventory - // 20 lines: create shipment - // 30 lines: send notifications - } # GOOD: Broken into focused functions + async function processOrder(orderId) { + const order = await fetchOrder(orderId); + validateOrder(order); + const pricing = calculatePricing(order); + await updateInventory(order); + const shipment = await createShipment(order); + await sendNotifications(order, pricing, shipment); + return { order, pricing, shipment }; + } ``` ### 2. Duplicated Code ```diff # BAD: Same logic in multiple places - function calculateUserDiscount(user) { - if (user.membership === 'gold') return user.total * 0.2; - if (user.membership === 'silver') return user.total * 0.1; - return 0; - } - - function calculateOrderDiscount(order) { - if (order.user.membership === 'gold') return order.total * 0.2; - if (order.user.membership === 'silver') return order.total * 0.1; - return 0; - } # GOOD: Extract common logic + function getMembershipDiscountRate(membership) { + const rates = { gold: 0.2, silver: 0.1 }; + return rates[membership] || 0; + } + + function calculateUserDiscount(user) { + return user.total * getMembershipDiscountRate(user.membership); + } + + function calculateOrderDiscount(order) { + return order.total * getMembershipDiscountRate(order.user.membership); + } ``` ### 3. Large Class/Module ```diff # BAD: God object that knows too much - class UserManager { - createUser() { /* ... */ } - updateUser() { /* ... */ } - deleteUser() { /* ... */ } - sendEmail() { /* ... */ } - generateReport() { /* ... */ } - handlePayment() { /* ... */ } - validateAddress() { /* ... */ } - // 50 more methods... - } # GOOD: Single responsibility per class + class UserService { + create(data) { /* ... */ } + update(id, data) { /* ... */ } + delete(id) { /* ... */ } + } + + class EmailService { + send(to, subject, body) { /* ... */ } + } + + class ReportService { + generate(type, params) { /* ... */ } + } + + class PaymentService { + process(amount, method) { /* ... */ } + } ``` ### 4. Long Parameter List ```diff # BAD: Too many parameters - function createUser(email, password, name, age, address, city, country, phone) { - /* .