Presentation is loading. Please wait.

Presentation is loading. Please wait.

Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.

Similar presentations


Presentation on theme: "Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without."— Presentation transcript:

1 Refactoring

2 DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior” Improving the design of existing code!

3 Refactoring ”Is it needed, can’t we just get the design right the first time…?” No… ”Is refactoring just another word for coding…?” No – no new functionality is added! DCS – SWC 3

4 Refactoring The ”two hats” of software development Adding functionality: Don’t restructure existing code, just add new functionality Refactoring: Don’t add new functionality, just restructure existing code DCS – SWC 4

5 Refactoring ”If it works, why spend time/money on refactoring…?” Definition of ”it works” will change over time Adding new functionality will become harder and harder (more $$) Maintainability! DCS – SWC 5

6 Refactoring ”When should you refactor…?” Three strikes rule As a consequence of review As a consequence of bug fixing …whenever poor design is detected (bad smells in code) DCS – SWC 6

7 Refactoring Refactoring relies heavily on the availability of tests! Use tests to check that functionality is preserved Tests should preferably be –Automatic –Self-checking DCS – SWC 7

8 Refactoring Refactoring vs. Big Design Up-Front Agile vs. Waterfall… BDUF sometimes tries to fix non-existing problems, overestimates complexity Start simple, refactor if complexity emerges DCS – SWC 8

9 Refactoring Refactoring can (sometimes) be in conflict with performance Refactoring philosophy: –Performance might be an issue –Incomprehensible code is an issue! Also, refactored code can be easier to optimise for performance DCS – SWC 9

10 Refactoring techniques ”Is it hard to refactor…?” Many named techniques for refactoring exist, ranging from extremely simple to quite sophisticated DCS – SWC 10

11 Refactoring techniques Refactoring template: –Name of technique –Motivation –Mechanics –Example DCS – SWC 11

12 Refactoring techniques Rename Method –Motivation: Method name does not precisely indicate its intention –Mechanics (C&T: Compile and Test) Declare a new method with better name Copy code from existing method to new method Let old method call new method (C&T) Replace all references to old method with references to new method (C&T) Remove the old method (C&T) DCS – SWC 12

13 Refactoring techniques Work forward in as small and simple steps as possible Compile and test whenever meaningful You can use tools from your development environment...if you are sure how they work DCS – SWC 13

14 Refactoring techniques Martin Fowlers book contains more than 70 refactoring techniques We will only look at a few here…. –Extract Method –Move Method –Replace Temp with Query –Extract Subclass DCS – SWC 14

15 Extract Method Extract Method - motivation –A piece of code inside a (too long) method does something useful on its own. Turning that piece of code into a new method will Make the source method easier to understand Allow others to benefit from the new method DCS – SWC 15

16 Extract Method Extract Method – mechanics –Create a new method, with a proper name –Copy the extracted code from the source method into the body of the new method –If the extracted code contains references to local variables from the source methods, they will become local variables or parameters to the new method –If one local variable is modified, this can become the return value for the new method –Replace the extracted code in the source method with a call to the new method (C&T) DCS – SWC 16

17 Extract Method Extract Method – example (before) void printAmountToPay(double amount) { printHeading(); System.out.println(”Name: ” + this.name); System.out.println(”Amount: ” + amount); } DCS – SWC 17

18 Extract Method Extract Method – example (after) void printAmountToPay(double amount) { printHeading(); printDetails(amount); } void printDetails(double amount) { System.out.println(”Name: ” + this.name); System.out.println(”Amount: ” + amount); } DCS – SWC 18

19 Move Method Move Method - motivation –A method is using features from another class more than features from the class in which it is defined. Moving such a method to another class can Make the logic clearer Reduce coupling DCS – SWC 19

20 Move Method Move Method – mechanics –Check if features in the source class might also need to be moved, e.g. instance fields –Declare the method in the target class –Copy the code from the source method into the new method. Make any needed adjustments, e.g. remove unnecessary parameters (C&T) –Figure out how to reference the correct target object from the source –Turn source method into delegating method or remove entirely (C&T) DCS – SWC 20

21 Move Method Move Method – example (before) class BankAccount { private Person owner; void printAddress() { System.out.println(”Name : ” + owner.getName()); System.out.println(”Street : ” + owner.getStreet()); System.out.println(”City : ” + owner.getCity()); } DCS – SWC 21

22 Move Method Move Method – example (after I) class Person { void printAddress() { System.out.println(”Name : ” + getName()); System.out.println(”Street : ” + getStreet()); System.out.println(”City : ” + getCity()); } DCS – SWC 22

23 Move Method Move Method – example (after II) class BankAccount { private Person owner; void printAddress() { owner.printAddress(); // Or remove entirely from class… } DCS – SWC 23

24 Replace Temp with Query Replace temp (local variable) with Query - motivation –A temporary variable is only used for holding the result of an expression. Replacing this with a method call will: Remove a local variable (making other refactorings simpler) Enable reuse of newly created method DCS – SWC 24

25 Replace Temp with Query Replace Temp with Query – mechanics –Find local variable which is only assigned to once –Declare local variable as final (C&T) –If right-hand side of assignment is an expression, turn that expression into a method (C&T) –Replace any use of the local variable with a call to the method (new or existing) DCS – SWC 25

26 Replace Temp with Query Replace temp… – example (before) double getSalesPrice() { double basePrice = this.quantity * this.itemPrice; if (basePrice > 1000) return basePrice * 0.95; // 5 % discount else return basePrice * 0.98; // 2 % discount } DCS – SWC 26

27 Replace Temp with Query Replace temp… – example (after) double getSalesPrice() { if (basePrice() > 1000) return basePrice() * 0.95; // 5 % discount else return basePrice() * 0.98; // 2 % discount } double basePrice() { return this.quantity * this.itemPrice; } DCS – SWC 27

28 Extract Subclass Extract Subclass - motivation –A class has features that are only used in some instances. Creating subclasses that reflect this will Isolate (sub)type-specific behavior Allow easier reuse of common features without modifying the superclass DCS – SWC 28

29 Extract Subclass Extract Subclass– mechanics –Define a new relevant subclass for the source class –Provide proper constructors for subclass –Replace calls to superclass constructor with calls to subclass constructor where relevant –Move features relevant features from superclass to subclass (instance fields, methods,…) –Eliminate any fields in superclass used for type information; this is now indicated by subclasses –C&T DCS – SWC 29

30 Extract Subclass Extract Subclass – example (before) class Employee { private String employeeType; public Employee(String employeeType) {…} public int getSalary() { if (employeeType.equals(”Boss”)) return 50000 else if (employeeType.equals(”Manager”)) return 40000 else if … // And so on } DCS – SWC 30

31 Extract Subclass Extract Subclass – example (after) class Employee { public abstract int getSalary(); } class Boss extends Employee { public Boss() {…} public int getSalary() {return 50000; } } // And so on DCS – SWC 31

32 Extract Subclass Many additional refactoring techniques… Check out Martin Fowlers classic book: Refactoring DCS – SWC 32


Download ppt "Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without."

Similar presentations


Ads by Google