Uploaded on


More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. October 2012
  • 2. Introduction to RefactoringNoun: a change made to the internal structure of software to make iteasier to understand and cheaper to modify without changing itsobservable behaviorVerb: to restructure software by applying a series of refactoringswithout changing its observable behavior.
  • 3. Good to know, BrunoNow, show some code please.
  • 4. Extract MethodMotivationExtract Method is one of the most common refactorings. If a method thatis too long or look at code that needs a comment to understand itspurpose.It does take a little getting used to if you are used to seeing largermethods. And small methods really work only when you have good names,so you need to pay attention to naming.The key is the semantic distance between the method name and themethod body. If extracting improves clarity, do it, even if the name islonger than the code you have extracted.
  • 5. Extract MethodCodevoid printOwing(double amount){ printBanner(); //print details System.out.println ("name:" + _name); System.out.println ("amount" + amount);}
  • 6. Extract MethodCodevoid printOwing(double amount) { printBanner(); //print details System.out.println ("name:" + _name); System.out.println ("amount" + amount);}void printOwing(double amount) { printBanner(); printDetails(amount);}void printDetails (double amount) { System.out.println ("name:" + _name); System.out.println ("amount" + amount);}
  • 7. Introduce Explaining VariableMotivationExpressions can become very complex and hard to read. In suchsituations variables can be helpful to break down the expression intosomething more manageable.Introduce Explaining Variable is particularly valuable with conditionallogic in which it is useful to take each clause of a condition and explainwhat the condition means with a well-named.
  • 8. Introduce Explaining VariableCodedouble price(){ // price is base price - quantity discount + shipping return _quantity * _itemPrice – Math.max(0, _quantity - 500) * _itemPrice * 0.05 + Math.min(_quantity * _itemPrice * 0.1, 100.0);}
  • 9. Introduce Explaining VariableCodedouble price(){ // price is base price - quantity discount + shipping return _quantity * _itemPrice – Math.max(0, _quantity - 500) * _itemPrice * 0.05 + Math.min(_quantity * _itemPrice * 0.1, 100.0);}double price(){ double basePrice = _quantity * _itemPrice; double quantityDiscount = Math.max(0, _quantity - 500) * _itemPrice *0.05; double shipping = Math.min(basePrice * 0.1, 100.0); return basePrice - quantityDiscount + shipping;}
  • 10. Replace Temp with QueryMotivationThe problem with temps is that they are temporary and local. Becausethey can be seen only in the context of the method in which they are used,temps tend to encourage longer methods, because that’s the only way youcan reach the temp. By replacing the temp with a query method, anymethod in the class can get at the information. That helps a lot in comingup with cleaner code for the class.The straightforward cases of this refactoring are those in which temps areassigned only to once and those in which the expression that generatesthe assignment is free of side effects. Other cases are trickier butpossible.
  • 11. Replace Temp with QueryCodedouble getPrice(){ int basePrice = _quantity * _itemPrice; double discountFactor; if (basePrice > 1000) discountFactor = 0.95; else discountFactor = 0.98; return basePrice * discountFactor;}
  • 12. Replace Temp with QueryCodeprivate int basePrice() { return _quantity * _itemPrice;}private double discountFactor() { if (basePrice() > 1000) return 0.95; else return 0.98;}double getPrice() { return basePrice() * discountFactor();}
  • 13. Substitute AlgorithmMotivationI’ve never tried to skin a cat. I’m told there are several ways to do it. I’msure some are easier than others. So it is with algorithms. If you find aclearer way to do something, you should replace the complicated way withthe clearer way. Refactoring can break down something complex intosimpler pieces, but sometimes you just reach the point at which you haveto remove the whole algorithm and replace it with something simpler. Thisoccurs as you learn more about the problem and realize that there’s aneasier way to do it. It also happens if you start using a library that suppliesfeatures that duplicate your code.
  • 14. Substitute AlgorithmCodeString 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 "";}
  • 15. Substitute AlgorithmCodeString 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 "";}
  • 16. Extract ClassMotivationYou’ve probably heard that a class should be a crisp abstraction, handle afew clear responsibilities, or some similar guideline. In practice, classesgrow. You add some operations here, a bit of data there. You add aresponsibility to a class feeling that it’s not worth a separate class, but asthat responsibility grows and breeds, the class becomes too complicated.Soon your class is as crisp as a microwaved duck.
  • 17. Extract ClassCodepublic String getName() { return _name;}public String getTelephoneNumber() { return ("(" + _officeAreaCode + ") " + _officeNumber);}String getOfficeAreaCode() { return _officeAreaCode;}void setOfficeAreaCode(String arg) { _officeAreaCode = arg;}String getOfficeNumber() { return _officeNumber;}void setOfficeNumber(String arg) { _officeNumber = arg;}private String _name;private String _officeAreaCode;private String _officeNumber;
  • 18. Extract ClassCodeprivate String _name;private TelephoneNumber _officeTelephone = new TelephoneNumber();public String getName() { return _name;}public String getTelephoneNumber(){ return _officeTelephone.getTelephoneNumber();}TelephoneNumber getOfficeTelephone() { return _officeTelephone;}
  • 19. Extract ClassCodeprivate String _number;private String _areaCode;public String getTelephoneNumber() { return ("(" + _areaCode + ") " + _number);}String getAreaCode() { return _areaCode;}void setAreaCode(String arg) { _areaCode = arg;}String getNumber() { return _number;}void setNumber(String arg) { _number = arg;}