1 / 71

Refactoring

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

effie
Download Presentation

Refactoring

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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.

  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, thelast 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:

More Related