refactoring-guide
Skill3 starsChanged 7 months agoMITView on GitHub
Safe refactoring patterns and techniques. Use when the user wants to improve code structure, reduce duplication, or make code more maintainable without changing behavior. Emphasizes incremental changes with test coverage.
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 3 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 26
Before adding new features to messy code
- Distinctive: line 32
Never refactor without tests: - Ensure behavior is preserved
- Distinctive: line 249
No test coverage exists (write tests first)
≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Refactoring Guide
Improve code structure safely without changing behavior.
When to Use
- User asks to "clean up" or "refactor" code
- Code smells detected during review
- Before adding new features to messy code
- Reducing duplication
- Improving readability
Golden Rules
- Never refactor without tests - Ensure behavior is preserved
- Small steps - One change at a time, run tests after each
- No behavior changes - Refactoring ≠ fixing bugs or adding features
- Commit often - Easy to revert if something breaks
Refactoring Process
1. Ensure tests exist and pass
↓
2. Identify the code smell
↓
3. Choose appropriate refactoring technique
↓
4. Make small change
↓
5. Run tests
↓
6. Commit if green, revert if red
↓
7. Repeat until doneCommon Code Smells
Long Function
Symptom: Function > 20-30 lines, multiple responsibilities
Refactoring: Extract Method
// Before
function processOrder(order: Order) {
// Validate order (10 lines)
// Calculate totals (15 lines)
// Apply discounts (12 lines)
// Update inventory (8 lines)
// Send confirmation (10 lines)
}
// After
function processOrder(order: Order) {
validateOrder(order);
const totals = calculateTotals(order);
const finalTotal = applyDiscounts(totals, order.discounts);
updateInventory(order.items);
sendConfirmation(order, finalTotal);
}Duplicate Code
Symptom: Similar code in multiple places
Refactoring: Extract to shared function
// Before
function getUserName(user: User) {
return `${user.firstName} ${user.lastName}`.trim();
}
function getCustomerName(customer: Customer) {
return `${customer.firstName} ${customer.lastName}`.trim();
}
// After
function formatFullName(entity: { firstName: string; lastName: string }) {
return `${entity.firstName} ${entity.lastName}`.trim();
}Long Parameter List
Symptom: Function with > 3-4 parameters
Refactoring: Introduce Parameter Object
// Before
function createUser(
name: string,
email: string,
age: number,
address: string,
phone: string,
role: string
) { ... }
// After
interface CreateUserInput {
name: string;
email: string;
age: number;
address: string;
phone: string;
role: string;
}
function createUser(input: CreateUserInput) { ... }Nested Conditionals
Symptom: Deep if/else nesting
Refactoring: Guard Clauses / Early Returns
// Before
function getDiscount(user: User) {
if (user) {
if (user.isActive) {
if (user.isPremium) {
return 0.2;
} else {
return 0.1;
}
}
}
return 0;
}
// After
function getDiscount(user: User) {
if (!user) return 0;
if (!user.isActive) return 0;
if (user.isPremium) return 0.2;
return 0.1;
}Magic Numbers/Strings
Symptom: Unexplained literal values in code
Refactoring: Extract Constant
// Before
if (user.age >= 21) { ... }
if (status === 'PNDG') { ... }
// After
const MINIMUM_AGE = 21;
const ORDER_STATUS = {
PENDING: 'PNDG',
COMPLETE: 'CMPL',
} as const;
if (user.age >= MINIMUM_AGE) { ... }
if (status === ORDER_STATUS.PENDING) { ... }Feature Envy
Symptom: Method uses data from another class more than its own
Refactoring: Move Method
// Before
class Order {
getCustomerDiscount() {
if (this.customer.loyaltyYears > 5) {
return this.customer.baseDiscount * 1.5;
}
return this.customer.baseDiscount;
}
}
// After
class Customer {
getDiscount() {
if (this.loyaltyYears > 5) {
return this.baseDiscount * 1.5;
}
return this.baseDiscount;
}
}Refactoring Techniques
| Smell | Technique |
| Long function | Extract Method |
| Duplicate code | Extract Function/Class |
| Long parameter list | Parameter Object |
| Nested conditionals | Guard Clauses |
| Magic values | Extract Constant |
| Feature envy | Move Method |
| Large class | Extract Class |
| Primitive obsession | Value Object |
| Switch statements | Replace with Polymorphism |
Safety Checklist
Before refactoring:
- Tests exist and pass
- Understand current behavior
- Identify specific smell to address
During refactoring:
- One change at a time
- Run tests after each change
- Commit when green
After refactoring:
- All tests still pass
- Code is more readable
- No behavior changed
- Review the diff
When NOT to Refactor
- No test coverage exists (write tests first)
- Under tight deadline (technical debt is okay temporarily)
- Code is being deleted soon
- You don't understand the code fully
Other versions2 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 2
- JayRHa/agent-skills★ 12same name
- JayRHa/AgentSkills★ 4same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository72 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- llms.txtllms.txt
- api-designerSkill
- backend-patternsSkill
- code-reviewerSkill
- debugging-assistantSkill
- golang-testingSkill
- project-guidelines-exampleSkill
- python-testingSkill
- spec-driven-developmentSkill
- tdd-workflowSkill
- verification-loopSkill
- update-docsSkill
- api-docsSkill
- changelog-writerSkill
- readme-generatorSkill
- django-patternsSkill
- django-securitySkill
- django-tddSkill
- django-verificationSkill
- fastapi-patternsSkill
- nextjs-patternsSkill
- react-patternsSkill
- springboot-patternsSkill
- springboot-securitySkill
- springboot-tddSkill
- branch-strategySkill
- changelogSkill
- commit-helperSkill
- pr-creatorSkill
- prSkill
- evolveSkill
- instinct-exportSkill
- instinct-importSkill
- instinct-statusSkill
- cleanupSkill
- lint-fixSkill
- refactor-cleanSkill
- update-depsSkill
- continuous-learningSkill
- continuous-learning-v2Skill
- eval-harnessSkill
- iterative-retrievalSkill
- strategic-compactSkill
- golang-patternsSkill
- python-patternsSkill
- rust-patternsSkill
- typescript-patternsSkill
- accessibility-checkerSkill
- build-fixSkill
- code-reviewSkill
- e2eSkill
- performance-reviewerSkill
- security-auditorSkill
- security-reviewSkill
- test-coverageSkill
- checkpointSkill
- deploySkill
- evalSkill
- The first 60 shown.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

