Refactoring - a discipline to write a better code

18
REFACTORING – A WAY TO WRITE A BETTER CODE Andrew Chaa Part 1

description

Refactoring is not only a way to improve the existing code, but also a discipline to write a better code

Transcript of Refactoring - a discipline to write a better code

Page 1: Refactoring   - a discipline to write a better code

REFACTORING – A WAY TO WRITE A

BETTER CODE

Andrew Chaa

Part 1

Page 2: Refactoring   - a discipline to write a better code

Questions, Slides, and Samples

Please feel free to ask questions anytime

Page 3: Refactoring   - a discipline to write a better code

Outline

Code that smells If it stinks, change it (Kent Beck’s

Grandma) Way to improve codes

Page 4: Refactoring   - a discipline to write a better code

What can you expect? “Clean code that works”

- Ron Jeffries Higher quality software Methodical approach to software

development

Page 5: Refactoring   - a discipline to write a better code

History of Refactoring

Kent Beck and War Cunningham in smalltalk

Martin Folwer’s famous book of Refactoring.

Supported by various tools now such as VS 2005/8, ReSharper, Eclipse …

Page 6: Refactoring   - a discipline to write a better code

Bad Smells in Code Duplicated Code Long Method Large Class Long Parameter List Divergent Change Switch Statement Temporary Field And many more…

Page 7: Refactoring   - a discipline to write a better code

Composing Methods

Extract Method Inline Method Inline Temp Replace Temp with Query Introduce Explaining Variable Split Temporary Variable Remove Assignments to Parameters Replace Method with Method Object Substitute Algorithm

Page 8: Refactoring   - a discipline to write a better code

void printOwing() {

printBanner();

//print details

System.out.println ("name: " + _name);

System.out.println ("amount " + getOutstanding());

}

void printOwing() { printBanner(); printDetails(getOutstanding());}

void printDetails (double outstanding) { System.out.println ("name: " + _name); System.out.println ("amount" + outstanding);}

Extracting Method

Page 9: Refactoring   - a discipline to write a better code

Inline Methodint getRating() {

return (moreThanFiveLateDeliveries()) ? 2 : 1;

}

boolean moreThanFiveLateDeliveries() {

return _numberOfLateDeliveries > 5;

}

int getRating() {

return (_numberOfLateDeliveries > 5) ? 2 : 1;

}

Page 10: Refactoring   - a discipline to write a better code

Remove Tempdouble basePrice = anOrder.basePrice();

return (basePrice > 1000)

return (anOrder.basePrice() > 1000)

Page 11: Refactoring   - a discipline to write a better code

Replace Temp with Querydouble basePrice = _quantity * _itemPrice;

if (basePrice > 1000)

return basePrice * 0.95;

else

return basePrice * 0.98;

if (basePrice() > 1000)

return basePrice() * 0.95;

else

return basePrice() * 0.98;

...

double basePrice() {

return _quantity * _itemPrice;

}

Page 12: Refactoring   - a discipline to write a better code

Introduce Explaining Variableif ( (platform.toUpperCase().indexOf("MAC") > -1) &&

(browser.toUpperCase().indexOf("IE") > -1) &&

wasInitialized() && resize > 0 )

{

// do something

}

final boolean isMacOs = platform.toUpperCase().indexOf("MAC") > -1;

final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1;

final boolean wasResized = resize > 0;

if (isMacOs && isIEBrowser && wasInitialized() && wasResized)

{

// do something

}

Page 13: Refactoring   - a discipline to write a better code

Split Temporary Variabledouble temp = 2 * (_height + _width);

System.out.println (temp);

temp = _height * _width;

System.out.println (temp);

final double perimeter = 2 * (_height + _width);

System.out.println (perimeter);

final double area = _height * _width;

System.out.println (area);

Page 14: Refactoring   - a discipline to write a better code

Remove Assignments to Parametersint discount (int inputVal, int quantity, int yearToDate) {

if (inputVal > 50) inputVal -= 2;

int discount (int inputVal, int quantity, int yearToDate) {

int result = inputVal;

if (inputVal > 50) result -= 2;

Page 15: Refactoring   - a discipline to write a better code

Replace Method with Method Objectclass Order...

double price() {

double primaryBasePrice;

double secondaryBasePrice;

double tertiaryBasePrice;

// long computation;

...

}

Page 16: Refactoring   - a discipline to write a better code

Demo Extract method, amountFor(Rental each) Rename variable: thisAmount -> result Moving Method: to Rental, GetCharge Change the reference: each.GetCharge(); Replace Temp with Query: thisAmount Extract Method: frequentRenterPoints Move Method: frequentRenterPoints Remove Temp with Query: GetTotalCharge(); Remove Temp with Query:

GetTotalFrequentRenterPoints ();

Page 17: Refactoring   - a discipline to write a better code

Substitute AlgorithmString foundPerson(String[] people){

for (int i = 0; i < people.length; i++) {

if (people[i].equals ("Don")) { return "Don“;}

if (people[i].equals ("John")) { return "John“;}

if (people[i].equals ("Kent")) {return "Kent“;}

}

return "";

}

String foundPerson(String[] people){

List candidates = Arrays.asList(new String[] {"Don", "John", "Kent"});

for (int i=0; i<people.length; i++)

if (candidates.contains(people[i]))

return people[i];

return "";

}