Refactoring

32
Refactoring

description

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. - PowerPoint PPT Presentation

Transcript of Refactoring

Page 1: Refactoring

Refactoring

Page 2: Refactoring

PBA WEB – BEWP 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!

Page 3: Refactoring

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 3

Page 4: Refactoring

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 4

Page 5: Refactoring

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 5

Page 6: Refactoring

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 6

Page 7: Refactoring

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 7

Page 8: Refactoring

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 8

Page 9: Refactoring

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 9

Page 10: Refactoring

Refactoring techniques

• ”Is it hard to refactor…?”

• Many named techniques for refactoring exist, ranging from extremely simple to quite sophisticated

PBA WEB – BEWP 10

Page 11: Refactoring

Refactoring techniques

• Refactoring template:– Name of technique– Motivation– Mechanics– Example

PBA WEB – BEWP 11

Page 12: Refactoring

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 12

Page 13: Refactoring

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 13

Page 14: Refactoring

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 14

Page 15: Refactoring

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 15

Page 16: Refactoring

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 16

Page 17: Refactoring

Extract Method

• Extract Method – example (before)

void PrintAmountToPay(double amount)

{

PrintHeading();

Console.WriteLine(”Name: ” + this.name);

Console.WriteLine(”Amount: ” + amount);

}

PBA WEB – BEWP 17

Page 18: Refactoring

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 18

Page 19: Refactoring

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 19

Page 20: Refactoring

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 20

Page 21: Refactoring

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 21

Page 22: Refactoring

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 22

Page 23: Refactoring

Move Method

• Move Method – example (after II)

class BankAccount

{

private Person owner;

void PrintAddress()

{

owner.PrintAddress(); // Or remove entirely from class…

}

}

PBA WEB – BEWP 23

Page 24: Refactoring

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 24

Page 25: Refactoring

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 25

Page 26: Refactoring

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 26

Page 27: Refactoring

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 27

Page 28: Refactoring

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 28

Page 29: Refactoring

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 29

Page 30: Refactoring

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 30

Page 31: Refactoring

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 31

Page 32: Refactoring

Extract Subclass

• Many additional refactoring techniques…

• Check out Martin Fowlers classic book: Refactoring

PBA WEB – BEWP 32