refactor
Original:🇺🇸 English
Translated
Code refactoring best practices based on Martin Fowler's catalog and Clean Code principles (formerly refactoring). This skill should be used when refactoring existing code, improving code structure, reducing complexity, eliminating code smells, or reviewing code for maintainability. Triggers on tasks involving extract method, rename, decompose conditional, reduce coupling, or improve readability.
11installs
Sourcepproenca/dot-skills
Added on
NPX Install
npx skill4agent add pproenca/dot-skills refactorTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Fowler/Martin Code Refactoring Best Practices
Comprehensive code refactoring guide based on Martin Fowler's catalog and Clean Code principles, designed for AI agents and LLMs. Contains 43 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
When to Apply
Reference these guidelines when:
- Refactoring existing code to improve maintainability
- Decomposing long methods or large classes
- Reducing coupling between components
- Simplifying complex conditional logic
- Reviewing code for code smells and anti-patterns
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Structure & Decomposition | CRITICAL | |
| 2 | Coupling & Dependencies | CRITICAL | |
| 3 | Naming & Clarity | HIGH | |
| 4 | Conditional Logic | HIGH | |
| 5 | Abstraction & Patterns | MEDIUM-HIGH | |
| 6 | Data Organization | MEDIUM | |
| 7 | Error Handling | MEDIUM | |
| 8 | Micro-Refactoring | LOW | |
Quick Reference
1. Structure & Decomposition (CRITICAL)
- - Extract Method for Long Functions
struct-extract-method - - Apply Single Responsibility Principle
struct-single-responsibility - - Extract Class from Large Class
struct-extract-class - - Compose Method for Readable Flow
struct-compose-method - - Keep Functions Under 20 Lines
struct-function-length - - Replace Method with Method Object
struct-replace-method-with-object - - Introduce Parameter Object
struct-parameter-object
2. Coupling & Dependencies (CRITICAL)
- - Use Dependency Injection
couple-dependency-injection - - Hide Delegate to Reduce Coupling
couple-hide-delegate - - Remove Middle Man When Excessive
couple-remove-middle-man - - Fix Feature Envy by Moving Methods
couple-feature-envy - - Apply Interface Segregation Principle
couple-interface-segregation - - Preserve Whole Object Instead of Fields
couple-preserve-whole-object
3. Naming & Clarity (HIGH)
- - Use Intention-Revealing Names
name-intention-revealing - - Avoid Abbreviations and Acronyms
name-avoid-abbreviations - - Use Consistent Vocabulary
name-consistent-vocabulary - - Use Searchable Names
name-searchable-names - - Avoid Type Encodings in Names
name-avoid-encodings
4. Conditional Logic (HIGH)
- - Replace Nested Conditionals with Guard Clauses
cond-guard-clauses - - Replace Conditional with Polymorphism
cond-polymorphism - - Decompose Complex Conditionals
cond-decompose - - Consolidate Duplicate Conditional Fragments
cond-consolidate - - Introduce Special Case Object
cond-special-case - - Replace Conditional with Lookup Table
cond-lookup-table
5. Abstraction & Patterns (MEDIUM-HIGH)
- - Extract Strategy for Algorithm Variants
pattern-strategy - - Use Template Method for Shared Skeleton
pattern-template-method - - Use Factory for Complex Object Creation
pattern-factory - - Apply Open-Closed Principle
pattern-open-closed - - Prefer Composition Over Inheritance
pattern-composition-over-inheritance - - Extract Superclass for Common Behavior
pattern-extract-superclass
6. Data Organization (MEDIUM)
- - Encapsulate Collection
data-encapsulate-collection - - Replace Primitive with Object
data-replace-primitive - - Encapsulate Record into Class
data-encapsulate-record - - Split Variable with Multiple Assignments
data-split-variable - - Replace Temp with Query
data-replace-temp-with-query
7. Error Handling (MEDIUM)
- - Use Exceptions Instead of Error Codes
error-exceptions-over-codes - - Create Domain-Specific Exception Types
error-custom-exceptions - - Fail Fast with Preconditions
error-fail-fast - - Separate Error Handling from Business Logic
error-separate-concerns
8. Micro-Refactoring (LOW)
- - Remove Dead Code
micro-remove-dead-code - - Inline Trivial Variables
micro-inline-variable - - Simplify Boolean Expressions
micro-simplify-expressions - - Rename for Clarity
micro-rename-for-clarity
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
- Individual rules:
references/{prefix}-{slug}.md
Full Compiled Document
For the complete guide with all rules expanded:
AGENTS.md