code-refactoring
Skill35 starsChanged 8 months agoMITView on GitHub
Code refactoring patterns and techniques for improving code quality without changing behavior. Use for cleaning up legacy code, reducing complexity, or improving maintainability.
5 other projects use a version of this, 1 of them edited.
Adopted by
Most recently taken up by
- Ethanon/developer.aiedited, first seen Sep 6, 2026
- joabgonzalez/ai-agents-skillstheir own version, first seen Jun 8, 2026
- autohandai/community-skillstheir own version, first seen Mar 9, 2026
- DauQuangThanh/hanoi-rainbowtheir own version, first seen Jan 22, 2026
- Willhong/claude_skilltheir own version, first seen Nov 17, 2025
Other versions5 projects use a version of this.
Word for word · 0
None found.
Edited · 1
- Ethanon/developer.ai★ 927% same
Wrote their own version · 4
- DauQuangThanh/hanoi-rainbow★ 17same name
- autohandai/community-skills★ 11same name
- joabgonzalez/ai-agents-skills★ 9same name
- Willhong/claude_skill★ 0same 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 repository56 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- templates/CLAUDE.mdCLAUDE.md
- docs/llms.txtllms.txt
- agent-protocolSkill
- context-health-checkSkill
- layered-contextSkill
- muse-commandsSkill
- semantic-searchSkill
- strategic-compactSkill
- using-superpowersSkill
- verification-before-completionSkill
- growth-distributionSkill
- api-design-principlesSkill
- architect-agentSkill
- brainstormingSkill
- build-error-resolverSkill
- changelog-generatorSkill
- code-documentationSkill
- code-reviewer-agentSkill
- coding-standardsSkill
- context7-mcpSkill
- coordinator-modeSkill
- creating-skillsSkill
- deep-diveSkill
- dispatching-parallel-agentsSkill
- doc-coauthoringSkill
- doc-updaterSkill
- e2e-runnerSkill
- executing-plansSkill
- finishing-a-development-branchSkill
- frontend-designSkill
- git-commitSkill
- github-pr-creationSkill
- github-pr-mergeSkill
- github-pr-reviewSkill
- git-security-guardSkill
- iterative-retrievalSkill
- markitdownSkill
- planner-agentSkill
- ralph-wiggumSkill
- receiving-code-reviewSkill
- requesting-code-reviewSkill
- sciomcSkill
- security-reviewSkill
- skill-creatorSkill
- skills-updaterSkill
- subagent-driven-developmentSkill
- systematic-debuggingSkill
- tdd-workflowSkill
- theme-factorySkill
- traceSkill
- ui-ux-pro-maxSkill
- using-git-worktreesSkill
- webapp-testingSkill
- writing-plansSkill
The file
We split this skill into the rules it gives an agent
6 rules. 0 are common, 6 are distinctive, and 0 are very specific to this project.
0
Common
Most of the 5 edited versions we read keep these.
6
Distinctive
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 13
Before adding new features (make change easy, then make easy change)
- Distinctive: line 14
After getting tests passing (red-green-refactor)
- Distinctive: line 15
When you see code smells
- Distinctive: line 22
When you don't understand what the code does
- Distinctive: line 196
Ensure tests exist: - Write tests if they don't
- Distinctive: line 200
Review the diff: - Make sure behavior hasn't changed
“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Code Refactoring
Refactoring Principles
When to Refactor
- Before adding new features (make change easy, then make easy change)
- After getting tests passing (red-green-refactor)
- When you see code smells
- During code review feedback
When NOT to Refactor
- Without tests covering the code
- Under tight deadlines with no safety net
- Code that will be replaced soon
- When you don't understand what the code does
Common Code Smells
Long Methods
// BEFORE: Method doing too much
function processOrder(order: Order) {
// 100 lines of validation, calculation, notification, logging...
}
// AFTER: Extract into focused methods
function processOrder(order: Order) {
validateOrder(order);
const total = calculateTotal(order);
saveOrder(order, total);
notifyCustomer(order);
}Deeply Nested Conditionals
// BEFORE: Arrow code
function getDiscount(user: User, order: Order) {
if (user) {
if (user.isPremium) {
if (order.total > 100) {
if (order.items.length > 5) {
return 0.2;
}
}
}
}
return 0;
}
// AFTER: Early returns (guard clauses)
function getDiscount(user: User, order: Order) {
if (!user) return 0;
if (!user.isPremium) return 0;
if (order.total <= 100) return 0;
if (order.items.length <= 5) return 0;
return 0.2;
}Primitive Obsession
// BEFORE: Primitives everywhere
function createUser(name: string, email: string, phone: string) {
if (!email.includes('@')) throw new Error('Invalid email');
// more validation...
}
// AFTER: Value objects
class Email {
constructor(private value: string) {
if (!value.includes('@')) throw new Error('Invalid email');
}
toString() { return this.value; }
}
function createUser(name: string, email: Email, phone: Phone) {
// Email is already validated
}Feature Envy
// BEFORE: Method uses another object's data extensively
function calculateShipping(order: Order) {
const address = order.customer.address;
const weight = order.items.reduce((sum, i) => sum + i.weight, 0);
const distance = calculateDistance(address.zip);
return weight * distance * 0.01;
}
// AFTER: Move method to where the data is
class Order {
calculateShipping() {
return this.totalWeight * this.customer.shippingDistance * 0.01;
}
}Refactoring Techniques
Extract Method
// Identify a code block that does one thing
// Move it to a new method with a descriptive name
// Replace original code with method call
function printReport(data: ReportData) {
// Extract this block...
const header = `Report: ${data.title}\nDate: ${data.date}\n${'='.repeat(40)}`;
console.log(header);
// ...into a method
printHeader(data);
}Replace Conditional with Polymorphism
// BEFORE: Switch on type
function getArea(shape: Shape) {
switch (shape.type) {
case 'circle': return Math.PI * shape.radius ** 2;
case 'rectangle': return shape.width * shape.height;
case 'triangle': return shape.base * shape.height / 2;
}
}
// AFTER: Polymorphic classes
interface Shape {
getArea(): number;
}
class Circle implements Shape {
constructor(private radius: number) {}
getArea() { return Math.PI * this.radius ** 2; }
}
class Rectangle implements Shape {
constructor(private width: number, private height: number) {}
getArea() { return this.width * this.height; }
}Introduce Parameter Object
// BEFORE: Too many parameters
function searchProducts(
query: string,
minPrice: number,
maxPrice: number,
category: string,
inStock: boolean,
sortBy: string,
sortOrder: string
) { ... }
// AFTER: Parameter object
interface SearchParams {
query: string;
priceRange: { min: number; max: number };
category?: string;
inStock?: boolean;
sort?: { by: string; order: 'asc' | 'desc' };
}
function searchProducts(params: SearchParams) { ... }Replace Magic Numbers with Constants
// BEFORE
if (user.age >= 18 && order.total >= 50) {
applyDiscount(order, 0.1);
}
// AFTER
const MINIMUM_AGE = 18;
const DISCOUNT_THRESHOLD = 50;
const STANDARD_DISCOUNT = 0.1;
if (user.age >= MINIMUM_AGE && order.total >= DISCOUNT_THRESHOLD) {
applyDiscount(order, STANDARD_DISCOUNT);
}Safe Refactoring Process
- Ensure tests exist - Write tests if they don't
- Make small changes - One refactoring at a time
- Run tests after each change - Catch regressions immediately
- Commit frequently - Easy to revert if something breaks
- Review the diff - Make sure behavior hasn't changed
Refactoring Checklist
- Tests pass before starting
- Each change is small and focused
- Tests pass after each change
- No behavior changes (only structure)
- Code is more readable than before
- Commit message explains the refactoring
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
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.

