Notes: you need to have the Refactoring: Web Edition account in order to see details of Refactoring Strategies
- Mysterious Name
- Duplicated Code
- Long Function
- Long Parameter List
- Global Data
- Mutable Data
- Divergent Change
- Shotgun Surgery
- Feature Envy
- Data Clumps
- Primitive Obsession
- Repeated Switches
- Loops
- Lazy Element
- Speculative Generality
- Temporary Field
- Message Chains
- Middle Man
- Insider Trading
- Large Class
- Alternative Classes with Different Interfaces
- Data Class
- Refused Bequest
- Comments
- References
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- The fundamental rule of thumb is to put things together that change together. Data and the behavior that references that data usually change together—but there are exceptions. When the exceptions occur, we move the behavior to keep changes in one place. Of course, there are several sophisticated patterns that break this rule. From the Gang of Four, Strategy and Visitor immediately leap to mind. Kent Beck’s Self Delegation is another. Use these to combat the divergent change smell.
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- Refactoring Strategies
- Look at whether clients use a subset of the features of the class. Each subset is a possible separate class
- Refactoring Strategies
- One of the great benefits of using classes is the support for substitution, allowing one class to swap in for another in times of need. But this only works if their interfaces are the same
- Refactoring Strategies
- Change Function Declaration: make functions match up
- Move Function: move behavior into classes until protocols match
- Extract Superclass: if refactoring leads to duplication
- Data classes are often a sign of behavior in the wrong place, which means you can make big progress by moving it from the client into the data class itself
- Exceptions
- the intermediate data structure after you've applied Split Phase: A key characteristic of such a result record is that it's immutable (at least in practice). Immutable fields don't need to be encapsulated and information derived from immutable data can be represented as fields rather than getting methods
- Exceptions
- Refactoring Strategies
- Encapsulate Record: public fields
- Remove Setting Method: any field that should not be changed
- Move Function: move those getting/setting methods used by other classes into the data class
- Extract Function: create a function that can be moved
- Not a strong smell
- Subclasses get to inherit the methods and data of their parents. But what if they don’t want or need what they are given?
- Refactoring Strategies
- Strong smell
- if the subclass is reusing behavior but does not want to support the interface of the superclass
- Refactoring Strategies
- The reason we mention comments here is that comments are often used as a deodorant. It’s surprising how often you look at thickly commented code and notice that the comments are there because the code is bad.
- Our first action is to remove the bad smells by refactoring. When we’re finished, we often find that the comments are superfluous.
- A good time to use a comment is when you don’t know what to do. In addition to describing what is going on, comments can indicate areas in which you aren’t sure. A comment can also explain why you did something. This kind of information helps future modifiers, especially forgetful ones.
- Refactoring Strategies
- Extract Function: If you need a comment to explain what a block of code does
- Change Function Declaration: If the method is already extracted but you still need a comment to explain what it does
- Introduce Assertion: If you need to state some rules about the required state of the system