refactoring-patterns
Skill1 starsChanged 9 months agoApache-2.0View on GitHub
Code refactoring patterns with before/after diffs for extract method, extract class, inline, move, rename, dead code removal, dependency injection, and code smell detection.
6 other projects use a version of this.
Adopted by
Most recently taken up by
- Benkapner/claude-code-basecamptheir own version, first seen Sep 14, 2026
- irahardianto/awesome-agvtheir own version, first seen Aug 21, 2026
- 2867203296/hermes-agenttheir own version, first seen Aug 19, 2026
- wondelai/skillstheir own version, first seen Jul 16, 2026
- vanara-agents/skillstheir own version, first seen Jul 14, 2026
- roanbrasil/engineer-grade-agent-skillstheir own version, first seen Jun 17, 2026
Other versions6 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 6
- wondelai/skills★ 2.3ksame name
- irahardianto/awesome-agv★ 157same name
- Benkapner/claude-code-basecamp★ 17same name
- vanara-agents/skills★ 9same name
- roanbrasil/engineer-grade-agent-skills★ 8same name
- 2867203296/hermes-agent★ 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 repository73 other files it gives its agents.
- accessibility-wcagSkill
- api-designSkill
- api-testingSkill
- asset-optimizationSkill
- audio-systemsSkill
- aws-essentialsSkill
- ci-cd-pipelinesSkill
- codebase-analysisSkill
- code-reviewerSkill
- container-securitySkill
- cost-optimizationSkill
- database-designSkill
- data-visualizationSkill
- doc-generationSkill
- docker-kubernetesSkill
- docxSkill
- email-notificationsSkill
- env-configSkill
- flutter-mobileSkill
- framer-motionSkill
- frontend-code-reviewSkill
- frontend-designSkill
- fullstack-developerSkill
- game-enginesSkill
- gameplay-designSkill
- game-programming-languagesSkill
- game-serversSkill
- git-workflowSkill
- godot-4Skill
- golang-proSkill
- go-linter-configurationSkill
- graphics-renderingSkill
- i18n-localizationSkill
- incident-responseSkill
- level-designSkill
- llm-integrationSkill
- local-securitySkill
- memory-managementSkill
- monetization-systemsSkill
- monitoring-observabilitySkill
- multiplayer-netcodeSkill
- nextjs-16Skill
- notebooklm-researchSkill
- npm-packageSkill
- oauth-authSkill
- particle-systemsSkill
- performance-optimizationSkill
- playwright-testingSkill
- postgres-advancedSkill
- programming-architectureSkill
- project-migrationSkill
- publishing-platformsSkill
- python-best-practicesSkill
- react-nativeSkill
- redis-patternsSkill
- remotion-best-practicesSkill
- rust-best-practicesSkill
- scraping-automationSkill
- security-reviewSkill
- seo-metaSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
3 rules. 0 are common, 3 are distinctive, and 0 are very specific to this project.
0
Common
Most of the 5 edited versions we read keep these.
3
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 227
Write tests if none exist.
- Distinctive: line 229
Run tests after every change.
- Distinctive: line 230
Use IDE refactoring tools.
“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
Extract Method
When: A code block does one logical thing within a function that does more.
BEFORE:
function processOrder(order: Order) {
// Validate order
if (!order.items || order.items.length === 0) {
throw new Error('Order must have items');
}
if (order.total <= 0) {
throw new Error('Order total must be positive');
}
// Calculate tax
let tax = 0;
for (const item of order.items) {
tax += item.price * item.quantity * 0.08;
}
// Save to database
db.orders.insert({ ...order, tax });
}AFTER:
function processOrder(order: Order) {
validateOrder(order);
const tax = calculateTax(order.items);
saveOrder(order, tax);
}
function validateOrder(order: Order) {
if (!order.items || order.items.length === 0) {
throw new Error('Order must have items');
}
if (order.total <= 0) {
throw new Error('Order total must be positive');
}
}
function calculateTax(items: OrderItem[]): number {
return items.reduce((sum, item) => sum + item.price * item.quantity * 0.08, 0);
}
function saveOrder(order: Order, tax: number) {
db.orders.insert({ ...order, tax });
}Why: Each function has a single responsibility. Method names replace comments.
Extract Class
When: A class has multiple responsibilities or a group of fields are always used together.
BEFORE:
class User {
id: number;
name: string;
email: string;
street: string;
city: string;
zipCode: string;
country: string;
getFullAddress(): string {
return `${this.street}, ${this.city}, ${this.zipCode}, ${this.country}`;
}
}AFTER:
class Address {
constructor(
public street: string,
public city: string,
public zipCode: string,
public country: string
) {}
getFullAddress(): string {
return `${this.street}, ${this.city}, ${this.zipCode}, ${this.country}`;
}
}
class User {
id: number;
name: string;
email: string;
address: Address;
}Why: Address is a cohesive concept. Extracting it makes User simpler and Address reusable.
Inline Method
When: A method's body is as clear as its name. The indirection adds no value.
BEFORE:
function getDiscountedPrice(price: number, discount: number): number {
return applyDiscount(price, discount);
}
function applyDiscount(price: number, discount: number): number {
return price * (1 - discount);
}AFTER:
function getDiscountedPrice(price: number, discount: number): number {
return price * (1 - discount);
}Replace Conditional with Polymorphism
When: A switch or if/else chain selects behavior based on type.
BEFORE:
class Order {
type: 'standard' | 'express' | 'overnight';
calculateShipping(): number {
if (this.type === 'standard') return 5;
else if (this.type === 'express') return 15;
else if (this.type === 'overnight') return 30;
}
}AFTER:
interface Order {
calculateShipping(): number;
}
class StandardOrder implements Order {
calculateShipping(): number { return 5; }
}
class ExpressOrder implements Order {
calculateShipping(): number { return 15; }
}
class OvernightOrder implements Order {
calculateShipping(): number { return 30; }
}Why: Adding new order types requires no changes to existing code. Open/closed principle.
Dependency Injection
When: A class creates its dependencies internally. Hard to test and couples implementation.
BEFORE:
class OrderService {
private db = new Database();
private emailer = new EmailService();
async createOrder(order: Order) {
await this.db.orders.insert(order);
await this.emailer.send(order.email, 'Order confirmed');
}
}AFTER:
class OrderService {
constructor(
private db: Database,
private emailer: EmailService
) {}
async createOrder(order: Order) {
await this.db.orders.insert(order);
await this.emailer.send(order.email, 'Order confirmed');
}
}
// Testing
const testService = new OrderService(mockDb, mockEmailer);Why: Dependencies are explicit. Testing with mocks is trivial.
Code Smells
Catalog of common code smells: Long Method, Large Class, Feature Envy, Data Clumps, Primitive Obsession, Divergent Change, Shotgun Surgery, and more. Each with detection criteria and fix patterns.
See references/code-smells.md for the full catalog with BAD/GOOD examples.
Technical Debt Prioritization
Score = (Frequency x Blast Radius) / Effort. Prioritize highest-score items first. Includes scoring table, priority tiers (P0-P3), and a tracking template.
See references/tech-debt-prioritization.md for the full scoring system, workflow, and register template.
Refactoring Workflow
- Write tests if none exist. Refactoring without tests is gambling.
- Make one change per commit. Small commits are reviewable and revertable.
- Run tests after every change. Failing tests mean behavior changed.
- Use IDE refactoring tools. They are less error-prone than manual edits.
- Review the diff before committing. Automated tools sometimes surprise.
- Explain intent in pull request descriptions. Clarify why structure changed.
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.

