95
CODE REFACTORING

Code Refactoring - 3.0

Embed Size (px)

Citation preview

Page 1: Code Refactoring - 3.0

CODE REFACTORING

Page 2: Code Refactoring - 3.0

CODE REFACTORING DEFINITION

Page 3: Code Refactoring - 3.0

WHY CODE REFACTORING?

Page 4: Code Refactoring - 3.0

WHEN TO DO CODE REFACTORING

Page 5: Code Refactoring - 3.0

CODE REFACTORING - REQUIREMENTS

Page 6: Code Refactoring - 3.0

IDENTIFYING THE CODE THAT SMELLS

Page 7: Code Refactoring - 3.0

TOOLS FOR REFACTORING

Page 8: Code Refactoring - 3.0

COMPOSING METHODS

Page 9: Code Refactoring - 3.0

Extract Method

Turn the fragment into a method whose name explains the purpose of the method.

Page 10: Code Refactoring - 3.0

Inline Method

Put the method’s body into the body of its callers and remove the method.

Page 11: Code Refactoring - 3.0

Inline Temp

Replace all references to that temp with the expression.

Page 12: Code Refactoring - 3.0

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

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

Split Temporary Variable

Make a separate temporary variable for each assignment.

Page 15: Code Refactoring - 3.0

Remove Assignments to Parameters

Use a temporary variable instead

Page 16: Code Refactoring - 3.0

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

Substitute Algorithm

Replace the body of the method with the new algorithm

Page 18: Code Refactoring - 3.0

Substitute Algorithm

Replace the body of the method with the new algorithm

Page 19: Code Refactoring - 3.0

MOVING FEATURES BETWEEN OBJECTS

Page 20: Code Refactoring - 3.0

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

Move Field

Create a new field in the target class, and change all its users

Page 22: Code Refactoring - 3.0

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

Inline ClassMove all its features into another class and delete it.

Page 24: Code Refactoring - 3.0

Hide DelegateCreate methods on the server to hide the delegate.

Page 25: Code Refactoring - 3.0

Remove Middle ManGet the client to call the delegate directly.

Page 26: Code Refactoring - 3.0

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

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

ORGANIZING DATA

Page 29: Code Refactoring - 3.0

Self Encapsulate FieldCreate getting and setting methods for the field and use only those to access the field.

Page 30: Code Refactoring - 3.0

Replace Data Value with Object

Turn the data item into an object.

Page 31: Code Refactoring - 3.0

Change Value to Reference

Turn the object into a reference object.

Page 32: Code Refactoring - 3.0

Change Reference to Value

Turn it into a value object

Page 33: Code Refactoring - 3.0

Replace Array with Object

Replace the array with an object that has a field for each element.

Page 34: Code Refactoring - 3.0

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

Change Unidirectional Association to Bidirectional

Add back pointers, and change modifiers to update both sets.

Page 36: Code Refactoring - 3.0

Change Bidirectional Association to Unidirectional

Drop the unneeded end of the association.

Page 37: Code Refactoring - 3.0

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

Encapsulate FieldMake it private and provide accessors.

Page 39: Code Refactoring - 3.0

Encapsulate CollectionMake it return a read-only view and provide add/remove methods

Page 40: Code Refactoring - 3.0

Replace Record with Data Class

Make a dumb data object for the record.

Page 41: Code Refactoring - 3.0

Replace Type Code with Class

Replace the number with a new class.

Page 42: Code Refactoring - 3.0

Replace Type Code with Subclasses

Replace the type code with subclasses.

Page 43: Code Refactoring - 3.0

Replace Type Code with State/Strategy

Replace the type code with a state object.

Page 44: Code Refactoring - 3.0

Replace Subclass with Fields

Change the methods to superclass fields and eliminate the subclasses.

Page 45: Code Refactoring - 3.0

SIMPLIFYING CONDITIONAL EXPRESSIONS

Page 46: Code Refactoring - 3.0

Decompose ConditionalExtract methods from the condition, then part, and else parts.

Page 47: Code Refactoring - 3.0

Consolidate Conditional Expression

Combine them into a single conditional expression and extract it.

Page 48: Code Refactoring - 3.0

Consolidate Duplicate Conditional Fragments

Move it outside of the expression.

Page 49: Code Refactoring - 3.0

Remove Control FlagUse a break or return instead.

Page 50: Code Refactoring - 3.0

Replace Nested Conditional with Guard Clauses

Use guard clauses for all the special cases.

Page 51: Code Refactoring - 3.0

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

Introduce Null ObjectReplace the null value with a null object.

Page 53: Code Refactoring - 3.0

Introduce AssertionMake the assumption explicit with an assertion.

Page 54: Code Refactoring - 3.0

MAKING METHOD CALLS SIMPLER

Page 55: Code Refactoring - 3.0

Rename MethodChange the name of the method.

Page 56: Code Refactoring - 3.0

Add ParameterAdd a parameter for an object that can pass on this information.

Page 57: Code Refactoring - 3.0

Remove ParameterRemove it.

Page 58: Code Refactoring - 3.0

Separate Query from Modifier

Create two methods, one for the query and one for the modification.

Page 59: Code Refactoring - 3.0

Parameterize MethodCreate one method that uses a parameter for the different values.

Page 60: Code Refactoring - 3.0

Replace Parameter with Explicit Methods

Create a separate method for each value of the parameter.

Page 61: Code Refactoring - 3.0

Preserve Whole ObjectSend the whole object instead.

Page 62: Code Refactoring - 3.0

Replace Parameter with Method

Remove the parameter and let the receiver invoke the method.

Page 63: Code Refactoring - 3.0

Introduce Parameter Object

Replace them with an object.

Page 64: Code Refactoring - 3.0

Remove Setting MethodRemove any setting method for that field.

Page 65: Code Refactoring - 3.0

Hide MethodMake the method private.

Page 66: Code Refactoring - 3.0

Replace Constructor with Factory Method

Replace the constructor with a factory method.

Page 67: Code Refactoring - 3.0

Encapsulate DowncastMove the downcast to within the method.

Page 68: Code Refactoring - 3.0

Replace Error Code with Exception

Throw an exception instead.

Page 69: Code Refactoring - 3.0

Replace Exception with Test

Change the caller to make the test first.

Page 70: Code Refactoring - 3.0

DEALING WITH GENERALIZATION

Page 71: Code Refactoring - 3.0

Pull Up FieldMove the field to the superclass.

Page 72: Code Refactoring - 3.0

Pull Up MethodMove them to the superclass.

Page 73: Code Refactoring - 3.0

Pull Up Constructor BodyCreate a superclass constructor; call this from the subclass methods.

Page 74: Code Refactoring - 3.0

Push Down MethodMove it to those subclasses.

Page 75: Code Refactoring - 3.0

Push Down FieldMove the field to those subclasses.

Page 76: Code Refactoring - 3.0

Extract SubclassCreate a subclass for that subset of features.

Page 77: Code Refactoring - 3.0

Extract SuperclassCreate a superclass and move the common features to the superclass.

Page 78: Code Refactoring - 3.0

Extract InterfaceExtract the subset into an interface.

Page 79: Code Refactoring - 3.0

Collapse HierarchyMerge them together.

Page 80: Code Refactoring - 3.0

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

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

Replace Delegation with Inheritance

Make the delegating class a subclass of the delegate.

Page 83: Code Refactoring - 3.0

OTHER BIG REFACTORINGS

Page 84: Code Refactoring - 3.0

Tease Apart InheritanceCreate two hierarchies and use delegation to invoke one from the other.

Page 85: Code Refactoring - 3.0

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

Separate Domain from Presentation

Separate the domain logic into separate domain classes

Page 87: Code Refactoring - 3.0

Extract HierarchyCreate a hierarchy of classes in which each subclass represents a special case.

Page 88: Code Refactoring - 3.0

CONVENTIONAL REFACTORING SOLUTION

Page 89: Code Refactoring - 3.0

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
Page 91: Code Refactoring - 3.0

REFACTORING TEMPLATE

Page 92: Code Refactoring - 3.0

KEY BENEFITS OF REFACTORING

Page 93: Code Refactoring - 3.0

KEY ADVANTAGES OF REFACTORING

Page 94: Code Refactoring - 3.0

CODE REVIEW – BEST PRACTICES

Page 95: Code Refactoring - 3.0

Questions/Discussions