Refactorings Flashcards
Define Rename Method/Field
Description: Method/field name doesn’t explain what it does
Smell: Comments
Fix: Rename the method
Define Extract Method
Description: Too long of methods or code needs comments to be understood
Smell: duplicate code, long method, feature envy, comments, switch statements, data class, message chains, data class
Fix: Take out related code from function and create a new method with it
Define Substitute Algorithm
Description: So you want to replace an existing algorithm with a new one?
Smell: duplicate code, long method
Fix: Replace the body of the method that implements the algorithm with a new algorithm.
Define Introduce Explaining Variable
Description: You have a really complex expression
Smell: ?
Fix: Create a local/temp variable to simplify expression and define what is happening
Define Replace Temp with Query
Description: You place the result of an expression (i.e a = b*c) in a local variable for later use in your code.
Smell: Long method, duplicate code
Fix: Move the entire expression to a separate method and return the result from it. Query the method instead of using a variable. Incorporate the new method in other methods, if necessary.
Define Replace Magic Number with Symbolic Constant
Description: Repeated use of a number to represent something (i.e 9.81 as gravity)
Smell: Comments
Fix: define final static variable to help make the number understandable
Define Add/Remove Parameter
Description: A method is missing data/A parameter isn’t used in the body of a method.
Smell: Speculative Generality(remove param)
Fix: Create copy of method but with necessary parameter/remove unused parameter from method.
Define Encapsulate Field
Description: You have a public variable
Smell: Data Class
Fix: Make it private and add getters and setters
Define Extract Class
Description: When one class does the work of two, awkwardness results.
Smell: Duplicate code, large class, divergent change, data clumps, primitive obsession, temporary fields, inappropriate intimacy
Fix: Instead, create a new class and place the fields and methods responsible for the relevant functionality in it.
Define Extract Superclass
Description: You have two classes with common fields and methods.
Smell: Duplicate code
Fix: Create a shared superclass for them and move all the identical fields and methods to it.
Define Move Method/Field
Description: A method/field is used more in another class than in its own class.
** rule of thumb is to put field in the class that has the most methods using it
Smell: Data class, shotgun surgery, message chains, inappropriate intimacy, switch statements, parallel inheritance hierarchy
Fix: Create a new method in the class that uses the method the most, then move code from the old method to there. Turn the code of the original method into a reference to the new method in the other class or else remove it entirely.
Define Pull up method/field
Description: Your subclasses have methods that perform similar work or duplicate fields.
Smell: duplicate code
Fix: Make the methods/fields identical and then move them to the relevant superclass.
Define Push down method/field
Description: method/field is only used in a few subclasses
Smell: Refused Bequest
Fix: push down method/field to subclasses that actually use it
Define Replace Constructor with Factory method
Description: You have a complex constructor that does something more than just setting parameter values in object fields.
Smell: ?
Fix: Create a factory (static) method and use it to replace constructor calls.
Define Form Template Method
Description: Your subclasses implement algorithms that contain similar steps in the same order.
Smell: Duplicate Code
Fix: Move the algorithm structure and identical steps to a superclass, and leave implementation of the different steps in the subclasses.