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

Post on 26-Dec-2015

215 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

REFACTORINGLecture 4

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.

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.

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.

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

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.

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.

8

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

9

Introduce the Null object• Use cases:

10

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

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.

12

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

13

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

with the null object

14

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

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

16

Introduce the Null object

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

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.

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.

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

20

Form the template of method

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.

22

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

TextStatement:

23

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

the same signature. In the class TextStatement:

24

Form the template of method• And in class HTMLStatement:

25

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

26

Form the template of method

27

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

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.

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.

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.

31

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

work to the Person

32

Replace delegation with inheritance• Class Person

33

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

make the field delegation to refer to the object itself

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:

35

Replace delegation with inheritance• Code after refactoring

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.

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.

38

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

the gender:

39

Replace subclass with fields

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:

41

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

create a private constructor to initialize:

42

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

constructor of the base class:

43

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

and remove the corresponding methods from the subclasses:

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

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.

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.

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:

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:

49

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

original functions with the callings of the new query:

50

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

occurred - firstly modifier and then the query:

51

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

“void”:

52

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

unnecessary duplication:

• The client code after the refactoring:

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.

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.

55

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

the “Department”:

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:

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.

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.

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.

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:

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:

62

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

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

63

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

64

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

behavior can be placed in the object itself:

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.

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.

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:

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.

69

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

the SqlConnection class as follows:

70

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

implementation of the method RestoreDatabase:

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.

top related