refactoring lecture 4. definition refactoring is a process of changing the internal structure of the...

71
REFACTORING Lecture 4

Upload: henry-hawkins

Post on 26-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

REFACTORINGLecture 4

Page 2: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

2

Definition• Refactoring is a process of changing the internal structure

of the program, not affecting its external behavior and its goal is to facilitate the understanding of its work.

• There is a sequence of small transformations in the basis of refactoring.

Page 3: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

3

Goals• The goal of refactoring is to make the code easier for

understanding or else the refactoring cannot be considered successful.

• Refactoring should be distinguished from the performance optimization. As well as refactoring, the performance optimization usually does not change the behavior of the program, but it only accelerates its work. But this optimization often makes the code more difficult for understanding contrary to refactoring.

• On the other hand, we should distinguish refactoring from re-engineering, the last one expands the functionality of the software. As a rule, large refactoring precede reengineering.

Page 4: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

4

Reasons for refactoring• You must add a new function, which does not fit into the

code design decision;• It is necessary to correct the error, the reasons of which is

not immediately clear;• It is used for overcoming the difficulties in team

development, which are due to the complex logic of the program.

Page 5: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

5

Refactorings• To introduce the Null object• To form the template of method• To replace delegation with inheritance• To replace subclass with fields• To separate query and modifier• To remove the mediator• To save the whole object• Replacing Extension Wrapper with Extension Method

Page 6: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

6

Introduce the Null object• Applicability: there are the multiple matches on the value

of one type to null.• Brief description: create a null implementation of the

object and replace a null reference with an instance of this object.

Page 7: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

7

Introduce the Null object• Technique:

• Create a subclass of the original class which will act as a null version of the original class.

• Create the isNull method, which returns true or false in the null version of the class and the original class.

• Find all places where at the request of the source object, we return null and edit them so that the program returns a null-object instead of null.

• Find and replace all comparisons with null by calling isNull. Use assertions to check the validity of objects.

• Replace each of these cases the operation in a null variant of class with the alternative behavior.

• Remove the checks conditions in those places where we have the overloading behavior.

Page 8: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

8

Introduce the Null object• Let us have the essence of the site with the user:

Page 9: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

9

Introduce the Null object• Use cases:

Page 10: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

10

Introduce the Null object• Create a null object and the IsNull method:

Page 11: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

11

Introduce the Null object• Create a factory method that returns an instance of null

object, and replace the places where can be returned a null pointer.

Page 12: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

12

Introduce the Null object• Replace check for a null pointer with calling IsNull

Page 13: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

13

Introduce the Null object• You can now replace the implementations of the methods

with the null object

Page 14: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

14

Introduce the Null object• As a result we are getting rid of checks on the client

Page 15: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

15

Introduce the Null object• Very often some null objects return other null objects. For

example, in this case, we need to create a null implementation for the entity PaymentHistory

Page 16: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

16

Introduce the Null object

• As a result we remove the last check in the client code:

Page 17: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

17

Form the template of method• Applicability: there are two methods in subclasses that

perform similar steps in the same order, however, these steps are different.

• Brief description: form from similar steps methods with the same signature in order to make the original methods identical. After that, put them in the parent class.

Page 18: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

18

Form the template of method• Technique:

• Make the decomposition of the methods in order to make the selected methods completely coinciding or completely different.

• Using the “Pull up of method” move the identical methods to the parent class.

• Rename the differing methods so that the signatures of all methods at each step become the same.

• Use the “Pull up of method” to one of the original methods. Determine their signatures as abstract methods of the parent class.

Page 19: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

19

Form the template of method• Let us have a “Customer” class, which has methods to

display information about account in the text and HTML form

Page 20: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

20

Form the template of method

Page 21: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

21

Form the template of method• These methods should appear in subclasses of some

common parent class. For this, we create classes TextStatement and HTMLStatement and delegate to them the job.

Page 22: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

22

Form the template of method• Implementation of the method GetValue for class

TextStatement:

Page 23: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

23

Form the template of method• Now let us select the header output to the methods with

the same signature. In the class TextStatement:

Page 24: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

24

Form the template of method• And in class HTMLStatement:

Page 25: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

25

Form the template of method• Similarly dealing with the rest elements of the account

Page 26: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

26

Form the template of method

Page 27: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

27

Form the template of method• Now GetValue methods in both classes are identical:

Page 28: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

28

Form the template of method• You need to raise the GetValue method in the parent

class. While raising other methods it is necessary to declare them as abstract methods.

Page 29: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

29

Replace delegation with inheritance• Applicability: you use too many simple delegations in the

methods of a class.• Brief description: make a delegating class is a subclass of

the delegate class.

Page 30: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

30

Replace delegation with inheritance• Technique:

• Make a delegating class is a subclass of the delegate class• Make the field of delegation to refer to the object itself• Remove the simple methods of delegation.• Replace all other delegation with the appeals to the object itself.• Delete the delegation field.

Page 31: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

31

Replace delegation with inheritance• Let there's a simple Employee class that delegates all the

work to the Person

Page 32: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

32

Replace delegation with inheritance• Class Person

Page 33: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

33

Replace delegation with inheritance• Declare Employee as a subclass of Person, and also

make the field delegation to refer to the object itself

Page 34: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

34

Replace delegation with inheritance• Then remove the simple methods of delegation. Calls to

methods of the delegation are replaced for normal calls. After that you can delete a field of delegation:

Page 35: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

35

Replace delegation with inheritance• Code after refactoring

Page 36: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

36

Replace subclass with fields• Applicability: there are subclasses that differ only by

methods which return constants.• Brief description: replace methods with fields in the parent

class and delete subclasses.

Page 37: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

37

Replace subclass with fields• Technique:

• Apply for subclasses method “Replace constructor with factory method”.

• Remove the references to the subclasses. • For each constant method, declare the relevant fields.• Declare protected constructor to initialize the fields. • Modify the existing constructors in order to use the new

constructor.• Implement each constant method to return a field, and remove

method of the subclass.

Page 38: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

38

Replace subclass with fields• Let us have a Person class and subclasses allocated on

the gender:

Page 39: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

39

Replace subclass with fields

Page 40: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

40

Replace subclass with fields• The first step is to create factory methods:

Replace the constructor calls with the calls of the factory methods, thereby getting rid of the references to subclasses in the client code:

Page 41: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

41

Replace subclass with fields• We announce the fields for each constant method and

create a private constructor to initialize:

Page 42: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

42

Replace subclass with fields• Add constructors in subclasses, which call new

constructor of the base class:

Page 43: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

43

Replace subclass with fields• Now put methods returning the fields to the base class

and remove the corresponding methods from the subclasses:

Page 44: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

44

Replace subclass with fields

• Now with the Person class is not abstract, we can delete subclasses and embed a call of constructors into the factory methods

Page 45: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

45

Separation query and modifier• Applicability: There is a method that returns a value, but,

in addition, changing the state of the object.• Brief description: create two methods: one for the request

and one for the modification.

Page 46: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

46

Separation query and modifier• Technique:

• Create a query that returns the same value as the original method.• Modify the original method to return the result of a call to the

request.• For each calling replace a call the original method with call a

request. • Calling a query place after calling the original method.• Declare the return type as void, and remove the “return” statement.

Page 47: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

47

Separation query and modifier• Let a function that informs the security name of the villain

and sends warning:

• The function is used as follows:

Page 48: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

48

Separation query and modifier• The first step is to create a function-query that returns the

same value as the original function, but does not generate side effects:

Page 49: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

49

Separation query and modifier• Alternately, we can replace all operators “return” in the

original functions with the callings of the new query:

Page 50: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

50

Separation query and modifier• Now change the client code so that there are two callings

occurred - firstly modifier and then the query:

Page 51: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

51

Separation query and modifier• Set the type of the return value of the original function as

“void”:

Page 52: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

52

Separation query and modifier• Now you can rename function modifier and eliminate

unnecessary duplication:

• The client code after the refactoring:

Page 53: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

53

Removing the mediator• Applicability: Class is busy with too simple delegation.• Brief description: you must force the client to refer to the

delegate directly.

Page 54: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

54

Removing the mediator• Technique:

• Create the access method to the delegate.• For each case of client's use of the mediator method replace its a

calling with appealing to the delegate.• Remove the mediator.

Page 55: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

55

Removing the mediator• Let us have a class “Person” that hides the delegation to

the “Department”:

Page 56: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

56

Removing the mediator• To find out who is the manager of the certain person, the

client makes a request:

• First of all we create an access method to a delegate:

Page 57: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

57

Removing the mediator• After that you have to consider each method, using this

method “Person”, and change it so it calls to the class-delegate:

• It is possible that some clients want to leave methods-delegates. In this case you need to store some simple delegation.

Page 58: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

58

Saving the whole object• Applicability: You get multiple values from the object,

which are passed then as parameters in method’s calling.• Brief description: you must pass the object completely

instead of values.

Page 59: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

59

Saving the whole object• Technique:

• Create a new parameter to pass the whole object.• Determine which parameters we need from the whole object, and

replace the references in the body of the method with calls a method of a parameter object.

• Remove from the caller method the code, which gets removed parameters.

Page 60: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

60

Saving the whole object• Consider an object representing the room and registering

the highest and the lowest temperature. It compares this range with a predetermined plan of heating:

Page 61: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

61

Saving the whole object• Instead of unpacking the values you can pass the whole

object of the range. First, let's add to the list of parameters some object from which we will get values:

Page 62: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

62

Saving the whole object• Then replace appealing the parameters with the calling of

the methods of the parameter-object and remove unneeded parameters:

Page 63: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

63

Saving the whole object• Now we don't need temporary variables:

Page 64: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

64

Saving the whole object• Such application of objects allows us to understand that

behavior can be placed in the object itself:

Page 65: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

65

Replacing Extension Wrapper with Extension Method• Applicability: we have a wrapper class that extends the

functionality of an existing one through the delegation.• Brief description: remove the wrapper class and

implement the additional functionality in the form of extension methods.

Page 66: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

66

Extension methods• Extension methods allow you to add methods to existing

types without creating a new derived type, recompiling or other modification of the original type.

• For the client code, written in C#, there is no visible difference between calling the extension method and calling the methods actually defined in the type.

Page 67: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

67

Extension methods• Extension methods are defined as static methods, but are

called using the syntax appealing to the method of the instance. Their first parameter specifies what type the method operates with (such parameter must be preceded by modifier this).

• In this example, class “String” is added method of counting the number of words:

• Usage example:

Page 68: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

68

Replacing Extension Wrapper with Extension Method• Technique:

• Create a static extension class and copy there the implementation of methods extending the functionality.

• In the wrapper class replace the implementation of the extension functionality with the delegation to the extension method.

• Remove the wrapper class, when it will contain only delegating methods.

Page 69: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

69

Replacing Extension Wrapper with Extension Method• Suppose we have a class that extends the functionality of

the SqlConnection class as follows:

Page 70: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

70

Replacing Extension Wrapper with Extension Method• Create an extension class and put there the

implementation of the method RestoreDatabase:

Page 71: REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and

71

Replacing Extension Wrapper with Extension Method• In the original class, we will replace an implementation of the method

with calling the extension method:

• Repeating this operation for each extension method, we will obtain the class that contains only delegating methods. After that you can delete the original class and create instead of it the SqlConnection class.