430 likes | 848 Views
Refactoring. 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!. Refactoring.
E N D
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! PBA WEB – BEWP
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! PBA WEB – BEWP
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 PBA WEB – BEWP
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! PBA WEB – BEWP
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) PBA WEB – BEWP
Refactoring • Refactoring relies heavily on the availability of tests! • Use tests to check that functionality is preserved • Tests should preferably be • Automatic • Self-checking PBA WEB – BEWP
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 PBA WEB – BEWP
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 PBA WEB – BEWP
Refactoring techniques • ”Is it hard to refactor…?” • Many named techniques for refactoring exist, ranging from extremely simple to quite sophisticated PBA WEB – BEWP
Refactoring techniques • Refactoring template: • Name of technique • Motivation • Mechanics • Example PBA WEB – BEWP
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) PBA WEB – BEWP
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 PBA WEB – BEWP
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 PBA WEB – BEWP
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 PBA WEB – BEWP
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) PBA WEB – BEWP
Extract Method • Extract Method – example (before) void PrintAmountToPay(double amount) { PrintHeading(); Console.WriteLine(”Name: ” + this.name); Console.WriteLine(”Amount: ” + amount); } PBA WEB – BEWP
Extract Method • Extract Method – example (after) void PrintAmountToPay(double amount) { PrintHeading(); PrintDetails(amount); } void PrintDetails(double amount) { Console.WriteLine(”Name: ” + this.name); Console.WriteLine(”Amount: ” + amount); } PBA WEB – BEWP
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 PBA WEB – BEWP
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) PBA WEB – BEWP
Move Method • Move Method – example (before) class BankAccount { private Person owner; void PrintAddress() { Console.WriteLine(”Name : ” + owner.GetName()); Console.WriteLine(”Street : ” + owner.GetStreet()); Console.WriteLine (”City : ” + owner.GetCity()); } } PBA WEB – BEWP
Move Method • Move Method – example (after I) class Person { void PrintAddress() { Console.WriteLine(”Name : ” + GetName()); Console.WriteLine(”Street : ” + GetStreet()); Console.WriteLine (”City : ” + GetCity()); } } PBA WEB – BEWP
Move Method • Move Method – example (after II) class BankAccount { private Person owner; void PrintAddress() { owner.PrintAddress(); // Or remove entirely from class… } } PBA WEB – BEWP
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 PBA WEB – BEWP
Replace Temp with Query • Replace Temp with Query – mechanics • Find local variable which is only assigned to once • Declare local variable as const (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) PBA WEB – BEWP
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 } PBA WEB – BEWP
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; } PBA WEB – BEWP
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 PBA WEB – BEWP
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 PBA WEB – BEWP
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 } } PBA WEB – BEWP
Extract Subclass • Extract Subclass – example (after) class Employee { public abstract int GetSalary(); } class Boss : Employee { public Boss() {…} public override int GetSalary() {return 50000; } } // And so on PBA WEB – BEWP
Extract Subclass • Many additional refactoring techniques… • Check out Martin Fowlers classic book: Refactoring PBA WEB – BEWP