Error in user YAML: (<unknown>): could not find expected ':' while scanning a simple key at line 3 column 1
---
name: clean-refactor
description: Post-feature cleanup guide for identifying code smells and applying refactoring techniques.
Use after implementing features to eliminate bloaters, fix coupling issues, simplify conditionals,
and improve code organization through systematic refactoring patterns.
---
WHEN TO APPLY: After implementing each feature, review code against these rules and refactor accordingly.
- Long Method - Method >10 lines? Extract smaller focused methods
- Large Class - Class doing too much? Split responsibilities into separate classes
- Primitive Obsession - Using primitives for domain concepts? Create value objects
- Long Parameter List - >3 params? Use parameter object or preserve whole object
- Data Clumps - Same vars appearing together? Extract into class
- Switch Statements - Type-checking or status switches? Replace with polymorphism
- Temporary Field - Field used conditionally? Extract class or remove
- Refused Bequest - Subclass not using parent methods? Use composition instead
- Alternative Classes Different Interfaces - Similar classes? Unify interfaces
- Divergent Change - One class changed for multiple reasons? Extract class per reason
- Shotgun Surgery - Small change requires many file edits? Move related code together
- Parallel Inheritance Hierarchies - Adding subclass requires another? Merge hierarchies
- Comments - Excessive comments? Make code self-documenting via Extract Method/Rename
- Duplicate Code - Repeated logic? Extract method/class
- Lazy Class - Class doing minimal work? Inline or remove
- Data Class - Only fields, no behavior? Move behavior from other classes here
- Dead Code - Unused code? Delete immediately
- Speculative Generality - Abstract code for future use? Remove until actually needed
- Feature Envy - Method using another class's data heavily? Move to that class
- Inappropriate Intimacy - Classes knowing each other's internals? Hide delegate or extract class
- Message Chains -
a.b().c().d()? Hide delegate at intermediate points - Middle Man - Class mostly delegating? Remove middle man
- Incomplete Library Class - Missing functionality? Introduce foreign/local extension
- Extract Method - Long/complex method → smaller focused methods
- Inline Method - Trivial method body → inline at call site
- Extract Variable - Complex expression → named variable
- Inline Temp - Variable used once for simple expression → inline expression
- Replace Temp with Query - Temp holding calculation result → extract to method
- Split Temporary Variable - Temp assigned multiple times → separate variable per use
- Remove Assignments to Parameters - Modifying params → use local variable
- Replace Method with Method Object - Long method with local state → extract to class
- Substitute Algorithm - Better algorithm exists → replace entire method body
- Move Method - Method using another class more → move it there
- Move Field - Field used by another class more → move it there
- Extract Class - Class doing work of two → create new class
- Inline Class - Class doing almost nothing → merge into caller
- Hide Delegate - Client calling delegate through server → add method on server
- Remove Middle Man - Simple delegation only → client calls delegate directly
- Introduce Foreign Method - Need method on class you can't change → add to client
- Introduce Local Extension - Need several methods on external class → create wrapper/subclass
- Self Encapsulate Field - Direct field access → use getters/setters
- Replace Data Value with Object - Data item needs behavior → turn into object
- Change Value to Reference - Many identical instances → use single reference object
- Change Reference to Value - Small immutable object → make value object
- Replace Array with Object - Array elements mean different things → use object with named fields
- Duplicate Observed Data - Domain data in GUI → separate domain layer with observer
- Change Unidirectional to Bidirectional - Classes need bidirectional link → add back-pointers
- Change Bidirectional to Unidirectional - Bidirectional link not needed → remove back-pointer
- Replace Magic Number with Symbolic Constant - Literal with special meaning → named constant
- Encapsulate Field - Public field → make private with accessors
- Encapsulate Collection - Getter returns collection → return readonly/copy, proper modifiers
- Replace Type Code with Class - Numeric type code affecting behavior → create class
- Replace Type Code with Subclasses - Type code with conditional → use subclasses
- Replace Type Code with State/Strategy - Type code changing during lifetime → use state pattern
- Replace Subclass with Fields - Subclasses differ only in returned values → use fields in parent
- Decompose Conditional - Complex conditional → extract to methods with clear names
- Consolidate Conditional Expression - Multiple checks same result → combine into single check
- Consolidate Duplicate Conditional Fragments - Identical code in all branches → move outside
- Remove Control Flag - Variable acting as control flag → use break/return/continue
- Replace Nested Conditional with Guard Clauses - Non-normal cases obscuring normal → guard clauses
- Replace Conditional with Polymorphism - Conditional based on object type → polymorphic method
- Introduce Null Object - Null checks everywhere → null object with default behavior
- Introduce Assertion - Assumption about code state → assertion making it explicit
- Rename Method - Name doesn't reveal purpose → rename
- Add Parameter - Method needs more info → add parameter
- Remove Parameter - Parameter not used → remove it
- Separate Query from Modifier - Method returns value AND modifies state → split into two
- Parameterize Method - Similar methods differ only by value → single method with parameter
- Replace Parameter with Explicit Methods - Method with parameter selecting behavior → separate methods
- Preserve Whole Object - Getting values from object to pass as params → pass object itself
- Replace Parameter with Method Call - Calling method to get param value → let method call it
- Introduce Parameter Object - Group of params naturally go together → replace with object
- Remove Setting Method - Field set at creation never changed → remove setter, make final
- Hide Method - Method not used by other classes → make private
- Replace Constructor with Factory Method - Complex construction logic → factory method
- Replace Error Code with Exception - Returning error codes → throw exception
- Replace Exception with Test - Exception for condition caller could check → provide test method
- Pull Up Field - Subclasses have same field → move to superclass
- Pull Up Method - Subclasses have same method → move to superclass
- Pull Up Constructor Body - Subclasses have similar constructors → call super in subclass
- Push Down Method - Method relevant only to some subclasses → move to those subclasses
- Push Down Field - Field used only by some subclasses → move to those subclasses
- Extract Subclass - Class features used only sometimes → create subclass for special case
- Extract Superclass - Two classes with similar features → create superclass
- Extract Interface - Multiple clients use same subset of class → extract to interface
- Collapse Hierarchy - Subclass and superclass barely different → merge them
- Form Template Method - Subclasses do similar steps in different order → template method
- Replace Inheritance with Delegation - Subclass uses only part of superclass → use field
- Replace Delegation with Inheritance - Using many simple delegations to all methods → inherit
- Identify smells - Scan code for patterns above
- Choose technique - Select applicable refactoring
- Test before - Ensure tests pass
- Apply refactoring - Make incremental changes
- Test after - Verify tests still pass
- Repeat - Continue until code clean
- Delete dead code, duplicates, speculative code
- Extract long methods (>10 lines)
- Split large classes (>200 lines or >5 responsibilities)
- Replace primitives with domain objects
- Simplify conditionals with polymorphism/guard clauses
- Fix inappropriate coupling between classes
- Apply remaining techniques as needed
Goal: Clean, maintainable code with minimal complexity and maximum clarity.
Sources: Refactoring Catalog, Code Smells