Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and."— Presentation transcript:

1 REFACTORING Lecture 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. 2

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. 3

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. 4

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 5

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. 6

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. 7

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

9 Introduce the Null object Use cases: 9

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

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. 11

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

13 Introduce the Null object You can now replace the implementations of the methods with the null object 13

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

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 15

16 Introduce the Null object As a result we remove the last check in the client code: 16

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. 17

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. 18

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 19

20 Form the template of method 20

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. 21

22 Form the template of method Implementation of the method GetValue for class TextStatement: 22

23 Form the template of method Now let us select the header output to the methods with the same signature. In the class TextStatement: 23

24 Form the template of method And in class HTMLStatement: 24

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

26 Form the template of method 26

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

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. 28

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. 29

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. 30

31 Replace delegation with inheritance Let there's a simple Employee class that delegates all the work to the Person 31

32 Replace delegation with inheritance Class Person 32

33 Replace delegation with inheritance Declare Employee as a subclass of Person, and also make the field delegation to refer to the object itself 33

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: 34

35 Replace delegation with inheritance Code after refactoring 35

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. 36

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. 37

38 Replace subclass with fields Let us have a Person class and subclasses allocated on the gender: 38

39 Replace subclass with fields 39

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: 40

41 Replace subclass with fields We announce the fields for each constant method and create a private constructor to initialize: 41

42 Replace subclass with fields Add constructors in subclasses, which call new constructor of the base class: 42

43 Replace subclass with fields Now put methods returning the fields to the base class and remove the corresponding methods from the subclasses: 43

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 44

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. 45

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. 46

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: 47

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: 48

49 Separation query and modifier Alternately, we can replace all operators “return” in the original functions with the callings of the new query: 49

50 Separation query and modifier Now change the client code so that there are two callings occurred - firstly modifier and then the query: 50

51 Separation query and modifier Set the type of the return value of the original function as “void”: 51

52 Separation query and modifier Now you can rename function modifier and eliminate unnecessary duplication: The client code after the refactoring: 52

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. 53

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. 54

55 Removing the mediator Let us have a class “Person” that hides the delegation to the “Department”: 55

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: 56

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. 57

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. 58

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. 59

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: 60

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: 61

62 Saving the whole object Then replace appealing the parameters with the calling of the methods of the parameter-object and remove unneeded parameters: 62

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

64 Saving the whole object Such application of objects allows us to understand that behavior can be placed in the object itself: 64

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. 65

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. 66

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: 67

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. 68

69 Replacing Extension Wrapper with Extension Method Suppose we have a class that extends the functionality of the SqlConnection class as follows: 69

70 Replacing Extension Wrapper with Extension Method Create an extension class and put there the implementation of the method RestoreDatabase: 70

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. 71


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

Similar presentations


Ads by Google