code refactoring - 3.0
TRANSCRIPT
![Page 1: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/1.jpg)
CODE REFACTORING
![Page 2: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/2.jpg)
CODE REFACTORING DEFINITION
![Page 3: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/3.jpg)
WHY CODE REFACTORING?
![Page 4: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/4.jpg)
WHEN TO DO CODE REFACTORING
![Page 5: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/5.jpg)
CODE REFACTORING - REQUIREMENTS
![Page 6: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/6.jpg)
IDENTIFYING THE CODE THAT SMELLS
![Page 7: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/7.jpg)
TOOLS FOR REFACTORING
![Page 8: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/8.jpg)
COMPOSING METHODS
![Page 9: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/9.jpg)
Extract Method
Turn the fragment into a method whose name explains the purpose of the method.
![Page 10: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/10.jpg)
Inline Method
Put the method’s body into the body of its callers and remove the method.
![Page 11: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/11.jpg)
Inline Temp
Replace all references to that temp with the expression.
![Page 12: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/12.jpg)
Replace Temp with Query
Extract the expression into a method. Replace all references to the temp with the expression. The new method can then be used in other methods.
![Page 13: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/13.jpg)
Introduce Explaining Variable
Extract the expression into a method. Replace all references to the temp with the expression. The new method can then be used in other methods.
![Page 14: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/14.jpg)
Split Temporary Variable
Make a separate temporary variable for each assignment.
![Page 15: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/15.jpg)
Remove Assignments to Parameters
Use a temporary variable instead
![Page 16: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/16.jpg)
Replace Method with Method Object
Turn the method into its own object so that all the local variables become fields on that object. You can then decompose the method into other methods on the same object
![Page 17: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/17.jpg)
Substitute Algorithm
Replace the body of the method with the new algorithm
![Page 18: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/18.jpg)
Substitute Algorithm
Replace the body of the method with the new algorithm
![Page 19: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/19.jpg)
MOVING FEATURES BETWEEN OBJECTS
![Page 20: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/20.jpg)
Move Method
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
![Page 21: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/21.jpg)
Move Field
Create a new field in the target class, and change all its users
![Page 22: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/22.jpg)
Extract Class
Create a new class and move the relevant fields and methods from the old class into the new class.
![Page 23: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/23.jpg)
Inline ClassMove all its features into another class and delete it.
![Page 24: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/24.jpg)
Hide DelegateCreate methods on the server to hide the delegate.
![Page 25: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/25.jpg)
Remove Middle ManGet the client to call the delegate directly.
![Page 26: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/26.jpg)
Introduce Foreign Method
Create a method in the client class with an instance of the server class as its first argument.
![Page 27: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/27.jpg)
Introduce Local Extension
Create a new class that contains these extra methods. Make this extension class a subclass or a wrapper of the original.
![Page 28: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/28.jpg)
ORGANIZING DATA
![Page 29: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/29.jpg)
Self Encapsulate FieldCreate getting and setting methods for the field and use only those to access the field.
![Page 30: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/30.jpg)
Replace Data Value with Object
Turn the data item into an object.
![Page 31: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/31.jpg)
Change Value to Reference
Turn the object into a reference object.
![Page 32: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/32.jpg)
Change Reference to Value
Turn it into a value object
![Page 33: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/33.jpg)
Replace Array with Object
Replace the array with an object that has a field for each element.
![Page 34: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/34.jpg)
Duplicate Observed DataCopy the data to a domain object. Set up an observer to synchronize the two pieces of data.
![Page 35: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/35.jpg)
Change Unidirectional Association to Bidirectional
Add back pointers, and change modifiers to update both sets.
![Page 36: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/36.jpg)
Change Bidirectional Association to Unidirectional
Drop the unneeded end of the association.
![Page 37: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/37.jpg)
Replace Magic Number with Symbolic Constant
Create a constant, name it after the meaning, and replace the number with it.
![Page 38: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/38.jpg)
Encapsulate FieldMake it private and provide accessors.
![Page 39: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/39.jpg)
Encapsulate CollectionMake it return a read-only view and provide add/remove methods
![Page 40: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/40.jpg)
Replace Record with Data Class
Make a dumb data object for the record.
![Page 41: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/41.jpg)
Replace Type Code with Class
Replace the number with a new class.
![Page 42: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/42.jpg)
Replace Type Code with Subclasses
Replace the type code with subclasses.
![Page 43: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/43.jpg)
Replace Type Code with State/Strategy
Replace the type code with a state object.
![Page 44: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/44.jpg)
Replace Subclass with Fields
Change the methods to superclass fields and eliminate the subclasses.
![Page 45: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/45.jpg)
SIMPLIFYING CONDITIONAL EXPRESSIONS
![Page 46: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/46.jpg)
Decompose ConditionalExtract methods from the condition, then part, and else parts.
![Page 47: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/47.jpg)
Consolidate Conditional Expression
Combine them into a single conditional expression and extract it.
![Page 48: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/48.jpg)
Consolidate Duplicate Conditional Fragments
Move it outside of the expression.
![Page 49: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/49.jpg)
Remove Control FlagUse a break or return instead.
![Page 50: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/50.jpg)
Replace Nested Conditional with Guard Clauses
Use guard clauses for all the special cases.
![Page 51: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/51.jpg)
Replace Conditional with Polymorphism
Move each leg of the conditional to an overriding method in a subclass. Make the original method abstract.
![Page 52: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/52.jpg)
Introduce Null ObjectReplace the null value with a null object.
![Page 53: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/53.jpg)
Introduce AssertionMake the assumption explicit with an assertion.
![Page 54: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/54.jpg)
MAKING METHOD CALLS SIMPLER
![Page 55: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/55.jpg)
Rename MethodChange the name of the method.
![Page 56: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/56.jpg)
Add ParameterAdd a parameter for an object that can pass on this information.
![Page 57: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/57.jpg)
Remove ParameterRemove it.
![Page 58: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/58.jpg)
Separate Query from Modifier
Create two methods, one for the query and one for the modification.
![Page 59: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/59.jpg)
Parameterize MethodCreate one method that uses a parameter for the different values.
![Page 60: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/60.jpg)
Replace Parameter with Explicit Methods
Create a separate method for each value of the parameter.
![Page 61: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/61.jpg)
Preserve Whole ObjectSend the whole object instead.
![Page 62: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/62.jpg)
Replace Parameter with Method
Remove the parameter and let the receiver invoke the method.
![Page 63: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/63.jpg)
Introduce Parameter Object
Replace them with an object.
![Page 64: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/64.jpg)
Remove Setting MethodRemove any setting method for that field.
![Page 65: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/65.jpg)
Hide MethodMake the method private.
![Page 66: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/66.jpg)
Replace Constructor with Factory Method
Replace the constructor with a factory method.
![Page 67: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/67.jpg)
Encapsulate DowncastMove the downcast to within the method.
![Page 68: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/68.jpg)
Replace Error Code with Exception
Throw an exception instead.
![Page 69: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/69.jpg)
Replace Exception with Test
Change the caller to make the test first.
![Page 70: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/70.jpg)
DEALING WITH GENERALIZATION
![Page 71: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/71.jpg)
Pull Up FieldMove the field to the superclass.
![Page 72: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/72.jpg)
Pull Up MethodMove them to the superclass.
![Page 73: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/73.jpg)
Pull Up Constructor BodyCreate a superclass constructor; call this from the subclass methods.
![Page 74: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/74.jpg)
Push Down MethodMove it to those subclasses.
![Page 75: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/75.jpg)
Push Down FieldMove the field to those subclasses.
![Page 76: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/76.jpg)
Extract SubclassCreate a subclass for that subset of features.
![Page 77: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/77.jpg)
Extract SuperclassCreate a superclass and move the common features to the superclass.
![Page 78: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/78.jpg)
Extract InterfaceExtract the subset into an interface.
![Page 79: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/79.jpg)
Collapse HierarchyMerge them together.
![Page 80: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/80.jpg)
Form Template MethodGet the steps into methods with the same signature, so that the original methods become the same. Then you can pull them up.
![Page 81: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/81.jpg)
Replace Inheritance with Delegation
Create a field for the superclass, adjust methods to delegate to the superclass, and remove the subclassing.
![Page 82: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/82.jpg)
Replace Delegation with Inheritance
Make the delegating class a subclass of the delegate.
![Page 83: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/83.jpg)
OTHER BIG REFACTORINGS
![Page 84: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/84.jpg)
Tease Apart InheritanceCreate two hierarchies and use delegation to invoke one from the other.
![Page 85: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/85.jpg)
Convert Procedural Design to Objects
Turn the data records into objects, break up the behavior, and move the behavior to the objects.
![Page 86: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/86.jpg)
Separate Domain from Presentation
Separate the domain logic into separate domain classes
![Page 87: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/87.jpg)
Extract HierarchyCreate a hierarchy of classes in which each subclass represents a special case.
![Page 88: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/88.jpg)
CONVENTIONAL REFACTORING SOLUTION
![Page 89: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/89.jpg)
CODE REFACTORING CYCLE
Ensure all tests pass
Find code that smells
Determine Simplification
MakeSimplification
Ensure all tests still pass
![Page 90: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/90.jpg)
![Page 91: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/91.jpg)
REFACTORING TEMPLATE
![Page 92: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/92.jpg)
KEY BENEFITS OF REFACTORING
![Page 93: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/93.jpg)
KEY ADVANTAGES OF REFACTORING
![Page 94: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/94.jpg)
CODE REVIEW – BEST PRACTICES
![Page 95: Code Refactoring - 3.0](https://reader033.vdocuments.mx/reader033/viewer/2022042619/58d076161a28abc9788b5b0b/html5/thumbnails/95.jpg)
Questions/Discussions