refactoring - a discipline to write a better code
DESCRIPTION
Refactoring is not only a way to improve the existing code, but also a discipline to write a better codeTRANSCRIPT
REFACTORING – A WAY TO WRITE A
BETTER CODE
Andrew Chaa
Part 1
Questions, Slides, and Samples
Please feel free to ask questions anytime
Outline
Code that smells If it stinks, change it (Kent Beck’s
Grandma) Way to improve codes
What can you expect? “Clean code that works”
- Ron Jeffries Higher quality software Methodical approach to software
development
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 …
Bad Smells in Code Duplicated Code Long Method Large Class Long Parameter List Divergent Change Switch Statement Temporary Field And many more…
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
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
Inline Methodint getRating() {
return (moreThanFiveLateDeliveries()) ? 2 : 1;
}
boolean moreThanFiveLateDeliveries() {
return _numberOfLateDeliveries > 5;
}
int getRating() {
return (_numberOfLateDeliveries > 5) ? 2 : 1;
}
Remove Tempdouble basePrice = anOrder.basePrice();
return (basePrice > 1000)
return (anOrder.basePrice() > 1000)
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;
}
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
}
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);
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;
Replace Method with Method Objectclass Order...
double price() {
double primaryBasePrice;
double secondaryBasePrice;
double tertiaryBasePrice;
// long computation;
...
}
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 ();
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 "";
}
Resources Refactoring catalogs:
http://www.refactoring.com/catalog/index.html
Podcast about Refactoringhttp://www.se-radio.net/index.php?post_id=172668
http://www.se-radio.net/index.php?post_id=205444
Refactoring (book)http://www.amazon.co.uk/Refactoring-Improving-Design-Existing-Technology/dp/0201485672/ref=sr_1_1?ie=UTF8&s=books&qid=1246371771&sr=8-1