code-refactoring
Refactor Code with Confidence
This skill helps developers simplify complex code while preserving behavior. It provides step-by-step refactoring patterns like Extract Method, DRY principle, and SOLID design to improve code clarity and reduce technical debt.
Download the skill ZIP
Upload in Claude
Go to Settings → Capabilities → Skills → Upload skill
Toggle on and start using
Test it
Using "code-refactoring". Refactor this function: function processOrder(order) { /* 100 lines of mixed validation, calculation, inventory check, and order creation */ }
Expected outcome:
## Refactoring Plan
### Original Function: processOrder (100 lines)
### Changes Made:
1. **Extracted validateOrder()** - Handles input validation
2. **Extracted calculateTotal()** - Computes order total with tax and shipping
3. **Extracted checkInventory()** - Validates stock availability
4. **Extracted createOrder()** - Creates the order in database
### New Structure:
```typescript
async function processOrder(order: Order) {
validateOrder(order);
const total = calculateTotal(order);
await checkInventory(order);
return await createOrder(order, total);
}
```
### Behavior Preserved:
- [x] Same validation rules
- [x] Same total calculation
- [x] Same inventory checking
- [x] Same order creation
Using "code-refactoring". These two functions have duplicated logic: getActiveUsers() and getActivePremiumUsers()
Expected outcome:
## Duplication Found
### Before (Duplicated):
```typescript
async function getActiveUsers() {
return await db.user.findMany({
where: { status: 'active', deletedAt: null },
select: { id: true, name: true, email: true }
});
}
async function getActivePremiumUsers() {
return await db.user.findMany({
where: { status: 'active', deletedAt: null, plan: 'premium' },
select: { id: true, name: true, email: true }
});
}
```
### After (DRY):
```typescript
async function getActiveUsers(filter: UserFilter = {}) {
return await db.user.findMany({
where: { status: 'active', deletedAt: null, ...filter },
select: { id: true, name: true, email: true }
});
}
// Usage: getActiveUsers() or getActiveUsers({ plan: 'premium' })
```
Using "code-refactoring". This class violates Single Responsibility. How should I refactor it?
Expected outcome:
## SOLID Analysis
### Original: User class with 3 responsibilities
- Data storage (save to DB)
- Email sending
- Report generation
### Refactored Structure:
| Class | Responsibility |
|-------|----------------|
| User | Data container |
| UserRepository | Database operations |
| EmailService | Communication |
| UserReportGenerator | Reporting |
### Benefits:
- Each class has one reason to change
- Easier to test in isolation
- Flexible to swap implementations
- Better collaboration in teams
Security Audit
SafeStatic analysis flagged 48 potential issues including external_commands, network, filesystem, and cryptographic patterns. Manual review confirms all findings are false positives. The skill is documentation-only (SKILL.md contains code examples for teaching refactoring techniques). No actual shell execution, network requests, filesystem operations, or cryptographic code exists in this skill. Risk level: SAFE.
Medium Risk Issues (1)
Low Risk Issues (3)
Quality Score
What You Can Build
Simplify Long Functions
A developer has a 200-line function that does too much. Use Extract Method to break it into smaller, focused functions with clear names.
Remove Duplicated Logic
A codebase has the same validation logic repeated in multiple places. Apply DRY principle to extract common logic into reusable functions.
Improve Object-Oriented Design
A class has multiple responsibilities causing tight coupling. Apply SOLID principles to separate concerns into well-defined classes.
Try These Prompts
Refactor this function to be more maintainable. Apply the Extract Method pattern to break it into smaller, focused functions:
Find and remove duplicated code in this codebase. Apply the DRY principle by extracting common logic into reusable functions:
Review this class and suggest how to apply SOLID principles. Identify which principles are violated and how to fix them:
I want to refactor this code. First, help me understand its current behavior (inputs, outputs, side effects). Then propose refactoring changes that preserve behavior, and tell me how to validate the changes:
Best Practices
- Write tests before refactoring to establish behavior baseline
- Make small, incremental changes - one refactoring at a time
- Run tests after each change to verify behavior is preserved
- Use clear, descriptive names for extracted functions and variables
- Commit after each successful refactoring for easy rollback
Avoid
- Refactoring and adding new features simultaneously - separates concerns properly
- Skipping tests because 'the change is simple' - always verify behavior
- Creating abstraction layers that add complexity without clear benefit
- Refactoring code you do not understand - analyze behavior first
- Ignoring performance implications of abstraction - profile when needed
Frequently Asked Questions
What is Extract Method refactoring?
How do I refactor without breaking functionality?
When should I apply SOLID principles?
What is the DRY principle?
How do I know if my refactoring is complete?
Can this skill automatically refactor my code?
Developer Details
Author
supercent-ioLicense
MIT
Repository
https://github.com/supercent-io/skills-template/tree/main/.agent-skills/code-refactoring/Ref
main
File structure
📄 SKILL.md