refactoring skill
Restructures code safely by following a systematic workflow of test verification, dependency mapping, and incremental changes.
- GitHub stars
- 7
- Files
- 2
- Last commit
- May 12, 2026
- License
- MIT
- Allowed tools
- —
What does the refactoring skill do?
refactoring restructures code safely by following a systematic workflow of test verification, dependency mapping, and incremental changes.
Summary generated from the skill's SKILL.md.
About the refactoring skill
Restructuring code while maintaining existing behavior is the primary function of this skill. Developers use it when they need to clean up, reorganize, or improve the structure of a codebase without changing its external functionality. The process relies on a systematic workflow that begins with verifying test coverage using commands like npm test, pytest, or go test.
An agent should load this skill when tasked with addressing code smells such as long methods, duplicate code, or complex conditionals. It produces a safer codebase by following a cycle of making one small change, running tests, and committing only when the tests pass. The skill utilizes dependency mapping through grep to understand how code interacts before any modifications occur. It specifically targets patterns like extracting methods, introducing parameter objects, or replacing switch statements with polymorphism to improve long-term maintainability.
What an agent can do with it
- 1Clean up long methodsAn agent can take a large, complex function and break it down into smaller, more manageable pieces.
- 2Reduce code duplicationA developer can identify repeated logic and consolidate it into a single shared function.
- 3Simplify complex conditionalsYou can replace messy switch statements or nested if-else blocks with polymorphic class structures.
- 4Organize long parameter listsAn agent can group multiple individual arguments into a single parameter object for better clarity.
Written from the skill's SKILL.md. Every use case is checked against a quote from it.
How to install the refactoring skill
Each command copies the skill folder into the client's skills directory. Restart the client or start a new session to load it.Install the refactoring skill in Plugin
/plugin marketplace add AvinashP/AgentsAtlas /plugin install agents-atlas@agents-atlas
Install the refactoring skill in Claude Code
~/.claude/skills/refactoring/SKILL.md
$ npx degit AvinashP/AgentsAtlas/skills/refactoring ~/.claude/skills/refactoringInstall the refactoring skill in Claude Code (project)
.claude/skills/refactoring/SKILL.md
$ npx degit AvinashP/AgentsAtlas/skills/refactoring .claude/skills/refactoringInstall the refactoring skill in Codex
~/.agents/skills/refactoring/SKILL.md
$ npx degit AvinashP/AgentsAtlas/skills/refactoring ~/.agents/skills/refactoringInstall the refactoring skill in Gemini CLI
~/.gemini/skills/refactoring/SKILL.md
$ npx degit AvinashP/AgentsAtlas/skills/refactoring ~/.gemini/skills/refactoringInstall the refactoring skill in Claude.ai
npx degit AvinashP/AgentsAtlas/skills/refactoring refactoring && zip -r refactoring.zip refactoring # claude.ai → Customize → Skills → + → Create skill → Upload a skill → refactoring.zip
SKILL.md
View on GitHubRefactoring Skill
Restructure code safely while preserving behavior.
Workflow
1. Verify Test Coverage
Before refactoring, ensure tests exist:
# Run existing tests
npm test
pytest
go test ./...
If no tests cover the code:
- Write characterization tests first
- These capture current behavior (even if buggy)
- Then refactor with confidence
2. Understand Current Structure
Map the code:
- What are the dependencies?
- Who calls this code?
- What does it depend on?
- What's the data flow?
# Find usages
grep -r "functionName" --include="*.ts"
# Find dependencies
grep -r "import.*from.*module" --include="*.ts"
3. Plan the Refactoring
Identify the target state:
- What's the problem with current code?
- What's the desired structure?
- What's the smallest step toward that?
Choose your approach:
| Situation | Approach |
|---|---|
| Large function | Extract methods |
| Duplicated code | Extract shared function |
| Complex conditionals | Replace with polymorphism |
| Long parameter list | Introduce parameter object |
| Feature envy | Move method to data's class |
| Data clump | Extract class |
4. Execute in Small Steps
The safe refactoring cycle:
1. Make ONE small change
2. Run tests
3. Commit if green
4. Repeat
Never skip steps:
- Don't combine multiple refactorings
- Don't "fix bugs" while refactoring
- Don't add features while refactoring
5. Verify Behavior Preserved
# Run full test suite
npm test
# Check for regressions
npm run test:e2e
# Manual smoke test if needed
Quick Reference
Safe Refactorings (Automated)
Most IDEs can do these automatically:
| Refactoring | Shortcut (VS Code) |
|---|---|
| Rename | F2 |
| Extract function | Ctrl+Shift+R |
| Extract variable | Ctrl+Shift+R |
| Inline variable | Ctrl+Shift+R |
| Move to file | Drag in explorer |
Common Patterns
Extract Function
// Before
function processOrder(order) {
// validate
if (!order.items.length) throw new Error('Empty order');
if (!order.customer) throw new Error('No customer');
// calculate total
let total = 0;
for (const item of order.items) {
total += item.price * item.quantity;
}
// apply discount
if (order.coupon) {
total *= (1 - order.coupon.discount);
}
return { ...order, total };
}
// After
function processOrder(order) {
validateOrder(order);
const total = calculateTotal(order);
return { ...order, total };
}
function validateOrder(order) {
if (!order.items.length) throw new Error('Empty order');
if (!order.customer) throw new Error('No customer');
}
function calculateTotal(order) {
const subtotal = order.items.reduce(
(sum, item) => sum + item.price * item.quantity,
0
);
return order.coupon
? subtotal * (1 - order.coupon.discount)
: subtotal;
}
Replace Conditional with Polymorphism
// Before
function getSpeed(vehicle) {
switch (vehicle.type) {
case 'car': return vehicle.horsepower * 0.5;
case 'bike': return vehicle.gears * 5;
case 'boat': return vehicle.engineSize * 2;
}
}
// After
class Car {
getSpeed() { return this.horsepower * 0.5; }
}
class Bike {
getSpeed() { return this.gears * 5; }
}
class Boat {
getSpeed() { return this.engineSize * 2; }
}
Introduce Parameter Object
// Before
function createUser(name, email, age, country, role) {
// ...
}
// After
function createUser({ name, email, age, country, role }) {
// ...
}
// Or with type
interface CreateUserParams {
name: string;
email: string;
age: number;
country: string;
role: string;
}
function createUser(params: CreateUserParams) {
// ...
}
Code Smells to Address
| Smell | Refactoring |
|---|---|
| Long method | Extract method |
| Large class | Extract class |
| Duplicate code | Extract function |
| Long parameter list | Parameter object |
| Switch statements | Polymorphism |
| Feature envy | Move method |
| Data clump | Extract class |
| Primitive obsession | Value object |
| Comments explaining code | Extract well-named method |
Anti-Patterns
Don't:
-
Refactor and change behavior simultaneously
- Refactoring = same behavior, different structure
- Keep them separate
-
Make big-bang changes
- Small steps with tests passing between each
-
Refactor without tests
- Write characterization tests first
-
Over-abstract too early
- Wait for duplication to appear 3 times
-
Refactor "just in case"
- Refactor for a reason (readability, extensibility needed now)
See Also
- patterns.md - Detailed refactoring patterns with examples
Files in this skill
2refactoring skill questions
How do I install the refactoring skill in Claude Code?
Run npx degit AvinashP/AgentsAtlas/skills/refactoring ~/.claude/skills/refactoring. Claude Code loads skills from ~/.claude/skills (all projects) or .claude/skills (one project) and uses a skill when its description matches the task.
Which clients can use the refactoring skill?
Any client that reads the Agent Skills format: Claude Code, Codex, Gemini CLI and claude.ai (upload as a ZIP). Scripts in the skill run only where the client allows code execution.
What should I do if there are no tests covering the code I need to refactor?
You should write characterization tests first to capture the current behavior, even if it is buggy, before proceeding with the refactor.
What is the recommended cycle for executing a refactor?
The safe cycle involves making one small change, running tests, committing if the tests pass, and then repeating the process.