refactoring-types
Summary of refactorings
1. TABLE OF CONTENT
3. BAD SMELLS IN CODE
1. Duplicated code
Same code structure in more than one place.
2. Long Method
The longer a procedure is the more difficult is to understand.
3. Large Classes
When a class is trying to do too much, duplicated code cannot be far behind.
4. Long Parameter List
The are hard to understand, inconsistent and difficult to use.
5. Divergent Change
When one class is commonly changed in different ways for different reasons.
6. Shotgun Surgery
When every time you make a kind of change, you have to make a lot of little changes to a lot of different classes.
7. Feature Envy
A method that seems more interested in a class other than the one it actually is in.
8. Data Clumps
Bunches of data(fields, parameters...) that hang around together.
9. Primitive Obsession
Using primitives types instead of small objects.
10. Switch Statements
The same switch statement scattered about a program in different places. Use polymorphism.
11. Parallel Inheritance Hierarchies
Every time you make a subclass of one class, you also have to make a subclass of another.
12. Lazy Class
A class that isn't doing enough to pay for itself should be eliminated.
13. Speculative Generality
All sorts of hooks and special cases to handle things that aren't required.
14. Temporary Field
An instance variable that is set only in certain circumstances.
15. Message Chain
When a client asks one object for another object, which the client then asks for yet another object...
16. Middle Man
When an object delegates much of its functionality.
17. Inappropriate Intimacy
When classes access to much to another classes.
18. Alternative Classes with Different Interfaces
Classes with methods that look to similar.
19. Incomplete Library Class
When we need extra features in libraries.
20. Data Class
Don't allow manipulation in Data Classes. Use encapsulation and immutability.
21. Refused Bequest
Subclasses that don't make uses of parents methods.
22. Comments
Not all comments but the ones that are there because the code is bad.
6. COMPOSING METHODS
1. Extract Method
You have a code fragment that can be grouped together.
to
Motivation
Increases the chances that other methods can use a method
Allows the higher-level methods to read more like a series of comments
to
2. Inline Method
A method's body is just as clear as its name.
to
Motivation
When Indirection is needless (simple delegation) becomes irritating.
If group of methods are badly factored and grouping them makes it clearer
3. Inline Temp
You have a temp that is assigned to once with a simple expression, and the temp is getting in the way of other refactorings.
to
Motivation
Use it with 4. Replace Temp with Query
4. Replace Temp with Query
You are using a temporary variable to hold the result of an expression.
to
Motivation
Replacing the temp with a query method, any method in the class can get at the information.
Is a vital step before 1. Extract Method
5. Introduce Explaining Variable
You have a complicated expression
to
Motivation
When expressions are hard to read
6. Split Temporary Variable
You have a temporary variable assigned to more than once, but is not a loop variable nor a collecting temporary variable.
to
Motivation
Variables should not have more than one responsibility.
Using a temp for two different things is very confusing for the reader.
7. Remove Assignments to Parameters
The code assign to a parameter
to
Motivation
You can change the internals of object is passed but do not point to another object.
Use only the parameter to represent what has been passed.
8. Replace Method with Method Object
You have a long method that uses local variables in such a way that you cannot apply Extract Method.
to
Motivation
When a method has lot of local variables and applying decomposition is not possible
This sample does not really needs this refactoring, but shows the way to do it.
to
9. Substitute Algorithm
You want to replace an algorithm with one that is clearer.
to
Motivation
Break down something complex into simpler pieces.
Makes easier apply changes to the algorithm.
Substituting a large, complex algorithm is very difficult; making it simple can make the substitution tractable.
7. Moving features between elements
10. Move method
A method is, or will be, using or used by more features of another class than the class on which it is defined.
Create a new method with a similar body in the class it uses most. Either turn the old method into a simple delegation, or remove it altogether.
to
Motivation
When classes do to much work or when collaborate too much and are highly coupled
11. Move field
A field is, or will be, used by another class more than the class on which it is defined. Create a new field in the target class, and change all its users.
to
Motivation
If a field is used mostly by outer classes and before 12. Extract Class
12. Extract Class
You have one class doing work that should be done by two. Create a new class and move the relevant fields and methods from the old class into the new class.
to
Motivation
Classes grow. Split them when:
subsets of methods seem to be together
subsets of data usually change together or depend on each other
13. Inline Class
A class isn't doing very much. Move all its features into another class and delete it.
to
Motivation
After refactoring normally there are a bunch of responsibilities moved out of the class, letting the class with little left.
14. Hide Delegate
A client is calling a delegate class of an object. Create methods on the server to hide the delegate.
to
The solution
Motivation
Key of encapsulation. Classes should know as less as possible of other classes.
> manager = john.getDepartment().getManager();
to
> manager = john.getManager();
15. Remove Middle Man
A class is doing too much simple delegation. Get the client to call the delegate directly.
to
Motivation
When the "Middle man" (the server) does to much is time for the client to call the delegate directly.
16. Introduce Foreign Method
A server class you are using needs an additional method, but you can't modify the class. Create a method in the client class with an instance of the server class as its first argument.
to
Motivation
When there is a lack of a method in class that you use a lot and you can not change that class.
17. Introduce Local Extension
A server class you are using needs several additional methods, but you can't modify the class. Create a new class that contains these extra methods. Make this extension class a subclass or a wrapper of the original.
to
Motivation
When plenty of 16. Introduce Foreign Method need to be added to a class.
8. ORGANIZING DATA
18. Self Encapsulate Field
You are accessing a field directly, but the coupling to the field is becoming awkward. Create getting and setting methods for the field and use only those to access the field.
to
Motivation Allows a subclass to override how to get that information with a method and that it supports more flexibility in managing the data, such as lazy initialization.
19. Replace Data Value with Object
You have a data item that needs additional data or behavior. Turn the data item into an object
to
Motivation Simple data items aren't so simple anymore.
20. Change Value to Reference
You have a class with many equal instances that you want to replace with a single object. Turn the object into a reference object.
to
Motivation Reference objects are things like customer or account. Each object stands for one object in the real world, and you use the object identity to test whether they are equal.
21. Change Reference to Value
You have a reference object that is small, immutable, and awkward to manage. Turn it into a value object
to
Motivation Working with the reference object becomes awkward. The reference object is immutable and small. Used in distributed or concurrent systems.
22. Replace Array with Object
You have an array in which certain elements mean different things. Replace the array with an object that has a field for each element
to
Motivation Arrays should be used only to contain a collection of similar objects in some order.
23. Duplicate Observed Data
You have domain data available only in a GUI control, and domain methods need access. Copy the data to a domain object. Set up an observer to synchronize the two pieces of data Motivation To separate code that handles the user interface from code that handles the business logic.
24. Change Unidirectional Association to Bidirectional
You have two classes that need to use each other's features, but there is only a one-way link. Add back pointers, and change modifiers to update both sets
to
Motivation When the object referenced needs access access to the object that refer to it.
25. Change Bidirectional Association to Unidirectional
You have a two-way association but one class no longer needs features from the other. Drop the unneeded end of the association Motivation If Bidirectional association is not needed, reduce complexity, handle zombie objects, eliminate interdependency
26. Replace Magic Number with Symbolic Constant
You have a literal number with a particular meaning. Create a constant, name it after the meaning, and replace the number with it
to
Motivation Avoid using Magic numbers.
27. Encapsulate Field
There is a public field. Make it private and provide accessors
to
Motivation You should never make your data public.
28. Encapsulate Collection
A method returns a collection. Make it return a read-only view and provide add/remove methods
to
Motivation
Encapsulated reduce the coupling of the owning class to its clients.
The getter should not return the collection object itself.
The getter should return something that prevents manipulation of the collection and hides unnecessary details.
There should not be a setter for collection, only operations to add and remove elements.
29. Remove Record with data class
You need to interface with a record structure in a traditional programming environment. Make a dumb data object for the record.
Motivation
Copying a legacy program
Communicating a structured record with a traditional programming API or database.
30. Replace Type Code with Class
A class has a numeric type code that does not affect its behavior. Replace the number with a new class.
to
Motivation Statically type checking.
31. Replace Type Code with Subclasses
You have an immutable type code that affects the behavior of a class. Replace the type code with subclasses.
to
Motivation
Execute different code depending on the value of a type.
When each type code object has unique features.
Structure to implement 38. Replace Conditional with Polymorphism
32. Replace Type Code with State/Strategy
You have a type code that affects the behavior of a class, but you cannot use subclassing. Replace the type code with a state object
to
Motivation
Similar to 31. Replace Type Code with Subclasses, but can be used if the type code changes during the life of the object or if another reason prevents subclassing.
It uses either the state or strategy pattern
Links
32. Replace Subclass with Fields
You have subclasses that vary only in methods that return constant data. Change the methods to superclass fields and eliminate the subclasses
to
Motivation
Subclasses that consists only of constant methods is not doing enough to be worth existing.
Remove such subclasses completely by putting fields in the superclass.
Remove the extra complexity of the subclasses.
9. SIMPLIFYING CONDITIONAL EXPRESSIONS
33. Decompose Conditional
You have a complicated conditional (if-then-else) statement. Extract methods from the condition, then part, and else parts
to
Motivation
Highlight the condition and make it clearly what you are branching on.
Highlight the reason for the branching
34. Consolidate Conditional Expression
You have a sequence of conditional tests with the same result. Combine them into a single conditional expression and extract it
to
Motivation
Makes the check clearer by showing that you are really making a single check
Sets you up for 1. Extract Method.
Clarify your code (replaces a statement of what you are doing with why you are doing it.)
35. Consolidate Duplicate Conditional Fragments
The same fragment of code is in all branches of a conditional expression. Move it outside of the expression.
to
Motivation Makes clearer what varies and what stays the same.
36. Remove Control Flag
You have a variable that is acting as a control flag for a series of boolean expressions. Use a break or return instead
to
Motivation
Control flag are used to determine when to stop looking, but modern languages enforce the use of
break
andcontinue
Make the real purpose of the conditional much more clear.
37. Replace Nested Conditional with Guard Clauses
A method has conditional behavior that does not make clear the normal path of execution. Use guard clauses for all the special cases
to
Motivation
If the condition is an unusual condition, check the condition and return if the condition is true.
This kind of check is often called a guard clause [Beck].
If you are using an if-then-else construct you are giving equal weight to the if leg and the else leg.
This communicates to the reader that the legs are equally likely and important.
Instead the guard clause says, "This is rare, and if it happens, do something and get out."
38. Replace Conditional with Polymorphism
You have a conditional that chooses different behavior depending on the type of an object. Move each leg of the conditional to an overriding method in a subclass. Make the original method abstract
to
Motivation
Avoid writing an explicit conditional when you have objects whose behavior varies depending on their types.
Switch statements should be less common in object oriented programs
39. Introduce Null Object
You have repeated checks for a null value. Replace the null value with a null object
to
Motivation
The object, depending on its type, does the right thing. Null objects should also apply this rule.
Use Null Object Pattern is the little brother of Special Case Pattern.
40. Introduce Assertion
A section of code assumes something about the state of the program. Make the assumption explicit with an assertion
to
Motivation
Assertions are conditional statements that are assumed to be always true.
Assertion failures should always result in unchecked exceptions.
Assertions usually are removed for production code.
As communication aids: they help the reader understand the assumptions the code is making.
As debugging aids: assertions can help catch bugs closer to their origin.
10. MAKING METHOD CALLS SIMPLER
41. Rename method
The name of a method does not reveal its purpose. Change the name of the method
to
Motivation Methods names must communicate their intention.
42. Add Parameter
A method needs more information from its caller. Add a parameter for an object that can pass on this information
to
Motivation After changed a method you require more information.
43. Remove Parameter
A parameter is no longer used by the method body. Remove it
to
Motivation A parameter is no more needed.
44. Separate Query from Modifier
You have a method that returns a value but also changes the state of an object. Create two methods, one for the query and one for the modification
to
Motivation Signaling methods with side effects and those without.
45. Parameterize Method
Several methods do similar things but with different values contained in the method body. Create one method that uses a parameter for the different values
to
Motivation Removes duplicate code and increases flexibility.
46. Replace Parameter with Explicit Methods
You have a method that runs different code depending on the values of an enumerated parameter. Create a separate method for each value of the parameter
to
Motivation
Avoid the conditional behavior
Gain compile time checking
Clearer Interface
47. Preserve Whole Object
You are getting several values from an object and passing these values as parameters in a method call. Send the whole object instead
to
Motivation
Make parameters list robust to changes
Make code more readeable
Remove possible duplicate code already done in the object passed
Negative: It creates a dependency between the parameter object and the called.
48. Replace Parameter with Method
An object invokes a method, then passes the result as a parameter for a method. The receiver can also invoke this method. Remove the parameter and let the receiver invoke the method
to
Motivation
If a method can get a value that is passed in as parameter by another means, it should.
If the receiving method can make the same calculation (does not reference any of the parameters of the calling method)
If you are calling a method on a different object that has a reference to the calling object.
49. Introduce Parameter Object
You have a group of parameters that naturally go together. Replace them with an object
to
Motivation
Reduces the size of the parameter list
Make the code more consistent
50. Remove Setting Method
A field should be set at creation time and never altered. Remove any setting method for that field
to
Motivation Make your intention clear: If you don't want that field to change once is created, then don't provide a setting method (and make the field final).
51. Hide Method
A method is not used by any other class. Make the method private
to
Motivation Whenever a method is not needed outside its class it should be hidden
52. Replace Constructor with Factory Method
You want to do more than simple construction when you create an object. Replace the constructor with a factory method
to
Motivation Create an object depending on its subclasses (types). Constructors can only return an instance of the object that is asked for so a Factory method is needed.
53. Encapsulate Downcast
A method returns an object that needs to be downcasted by its callers. Move the downcast to within the method
to
Motivation Provide as result type, the most specific type of the method signature. If the signature is to general, check the uses the clients do of that method and if coherent, provide a more specific one.
54. Replace Error Code with Exception
A method returns a special code to indicate an error. Throw an exception instead
to
Motivation When a program that spots an error can't figure out what to do about it. It needs to let its caller know, and the caller may pass the error up the chain.
55. Replace Exception with Test
You are throwing a checked exception on a condition the caller could have checked first. Change the caller to make the test first
to
Motivation
Do not overuse Exceptions they should be used for exceptional behavior (behavior that is unexpected).
Do not use them as substitute for conditional tests.
Check expected wrong conditions before before calling the operation.
11. DEALING WITH GENERALIZATION
56. Pull up field
Two subclasses have the same field. Move the field to the superclass
to
Motivation
Removes the duplicate data declaration.
Allows to move behavior from the subclasses to the superclass.
57. Pull Up Method
You have methods with identical results on subclasses. Move them to the superclass
to
Motivation
Eliminates duplicated behavior.
58. Pull Up Constructor Body
You have constructors on subclasses with mostly identical bodies. Create a superclass constructor; call this from the subclass methods
to
Motivation
Constructors are not the same as methods.
Eliminates duplicated behavior.
59. Push Down Method
Behavior on a superclass is relevant only for some of its subclasses. Move it to those subclasses.
to
Motivation When a method makes only sense in the subclass.
60. Push Down Field
A field is used only by some subclasses. Move the field to those subclasses
to
Motivation When a field makes only sense in the subclass.
61. Extract Subclass
A class has features that are used only in some instances. Create a subclass for that subset of features
to
Motivation When a class has behavior used for some instances of the class and not for others.
62. Extract Superclass
You have two classes with similar features. Create a superclass and move the common features to the superclass
to
Motivation When two classes that do similar things in the same way or similar things in different ways.
63. Extract Interface
Several clients use the same subset of a class's interface, or two classes have part of their interfaces in common. Extract the subset into an interface
to
Motivation
If only a particular subset of a class's responsibilities is used by a group of clients.
If a class needs to work with any class that can handle certain requests.
Whenever a class has distinct roles in different situations.
64. Collapse Hierarchy
A superclass and subclass are not very different. Merge them together
to
Motivation When a subclass that isn't adding any value.
65. Form Template Method
You have two methods in subclasses that perform similar steps in the same order, yet the steps are different. Get the steps into methods with the same signature, so that the original methods become the same. Then you can pull them up
to
Motivation
Use Inheritance with polymorphism for eliminating duplicate behavior that is slightly different.
When there are two similar methods in a subclass, bring them together in a superclass.
66. Replace Inheritance with Delegation
A subclass uses only part of a superclasses interface or does not want to inherit data. Create a field for the superclass, adjust methods to delegate to the superclass, and remove the subclassing
to
Motivation
Using delegation makes clear that you are making only partial use of the delegated class.
You control which aspects of the interface to take and which to ignore.
Mechanics
67. Replace Delegation with Inheritance
You're using delegation and are often writing many simple delegations for the entire interface. Make the delegating class a subclass of the delegate
to
Motivation
If you are using all the methods of the delegate.
If you aren't using all the methods of the class to which you are delegating, you shouldn't use it.
Beware of is that in which the delegate is shared by more than one object and is mutable. Data sharing is a responsibility that cannot be transferred back to inheritance.
12. BIG REFACTORINGS
68. Tease Apart Inheritance
You have an inheritance hierarchy that is doing two jobs at once. Create two hierarchies and use delegation to invoke one from the other
to
Motivation
Tangled inheritance leads to code duplication.
If every class at a certain level in the hierarchy has subclasses that begin with the same adjective, you probably are doing two jobs with one hierarchy.
Mechanics Note Identify the different jobs being done by the hierarchy. Create a two-dimensional(or x-dimensional) grid and label the axes with the different jobs.
Deal | Active Deal | Passive Deal |
Tabular Deal |
69. Convert Procedural Design to Objects
You have code written in a procedural style. Turn the data records into objects, break up the behavior, and move the behavior to the objects
to
Motivation Use OOP (at least in JAVA)
70. Separate Domain from Presentation
You have GUI classes that contain domain logic. Separate the domain logic into separate domain classes
to
Motivation
Separates two complicated parts of the program into pieces that are easier to modify.
Allows multiple presentations of the same business logic.
Its worth is proved
71. Extract Hierarchy
You have a class that is doing too much work, at least in part through many conditional statements. Create a hierarchy of classes in which each subclass represents a special case
to
Motivation
A class as implementing one idea become implementing two or three or ten.
Keep the Single Responsibility Principle.
Last updated