refactor

Surgical code refactoring to improve maintainability without changing behavior. Covers extracting functions, renaming variables, breaking down god functions, improving type safety, eliminating code smells, and applying design patterns. Less drastic than repo-rebuilder; use for gradual improvements.

By github · 21,692 installs

npx skills add github/awesome-copilot --skill refactor

Source repository · Upstream listing

Refactor Overview Improve code structure and readability without changing external behavior. Refactoring is gradual evolution, not revolution. Use this for improving existing code, not rewriting from scratch. When to Use Use this skill when: Code is hard to understand or maintain Functions/classes are too large Code smells need addressing Adding features is difficult due to code structure User asks "clean up this code", "refactor this", "improve this" Refactoring Principles The Golden Rules 1. Behavior is preserved Refactoring doesn't change what the code does, only how 2. Small steps Make tiny changes, test after each 3. Version control is your friend Commit before and after each safe state 4. Tests are essential Without tests, you're not refactoring, you're editing 5. One thing at a time Don't mix refactoring with feature changes When NOT to Refactor Common Code Smells & Fixes 1. Long Method/Function 2. Duplicated Code 3. Large Class/Module 4. Long Parameter List 5. Feature Envy 6. Primitive Obsession 7. Magic Numbers/Strings 8. Nested Conditionals 9. Dead Code 10. Inappropriate Intimacy Extract Method Refactoring Before and After Introducing Type Safety From Untyped to Typed Design Patterns for Refactoring Strategy Pattern Chain of Responsibility Refactoring Steps Safe Refactoring Process Refactoring Checklist Code Quality [ ] Functions are small (< 50 lines) [ ] Functions do one thing [ ] No duplicated code [ ] Descriptive names (variables, functions, classes) [ ] No magic numbers/strings [ ] Dead code removed Structure [ ] Related code is together [ ] Clear module boundaries [ ] Dependencies flow in one direction [ ] No circular dependencies Type Safety [ ] Types defined for all public APIs [ ] No any types without justification [ ] Nullable types explicitly marked Testing [ ] Refactored code is tested [ ] Tests cover edge cases [ ] All tests pass Common Refactoring Operations Operation Description Extract Method Turn code fragment into method Extract Class Move behavior to new class Extract Interface Create interface from implementation Inline Method Move method body back to caller Inline Class Move class behavior to caller Pull Up Method Move method to superclass Push Down Method Move method to subclass Rename Method/Variable Improve clarity Introduce Parameter Object Group related parameters Replace Conditional with Polymorphism Use polymorphism instead of switch/if Replace Magic Number with Constant Named constants Decompose Conditional Break complex conditions Consolidate Conditional Combine duplicate conditions Replace Nested Conditional with Guard Clauses Early returns Introduce Null Object Eliminate null checks Replace Type Code with Class/Enum Strong typing Replace Inheritance with Delegation Composition over inheritance