Your SlideShare is downloading. ×
Refactoring   - a discipline to write a better code
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Refactoring - a discipline to write a better code

1,744
views

Published on

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

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


1 Comment
1 Like
Statistics
Notes
  • We wholesle many styles of bodybuilding.They are very comfotable and easy to macth with all kinds of clothes.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
1,744
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
43
Comments
1
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Refactoring – A way to write a better code
    Andrew Chaa
    Part 1
  • 2. Questions, Slides, and Samples
    Please feel free to ask questions anytime
  • 3. Outline
    Code that smells
    If it stinks, change it (Kent Beck’s Grandma)
    Way to improve codes
  • 4. What can you expect?
    “Clean code that works” - Ron Jeffries
    Higher quality software
    Methodical approach to software development
  • 5. 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 …
  • 6. Bad Smells in Code
    Duplicated Code
    Long Method
    Large Class
    Long Parameter List
    Divergent Change
    Switch Statement
    Temporary Field
    And many more…
  • 7. 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
  • 8. 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
  • 9. Inline Method
    intgetRating() {
    return (moreThanFiveLateDeliveries()) ? 2 : 1;
    }
    booleanmoreThanFiveLateDeliveries() {
    return _numberOfLateDeliveries > 5;
    }
    intgetRating() {
    return (_numberOfLateDeliveries > 5) ? 2 : 1;
    }
  • 10. Remove Temp
    double basePrice = anOrder.basePrice();
    return (basePrice > 1000)
    return (anOrder.basePrice() > 1000)
  • 11. Replace Temp with Query
    double 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;
    }
  • 12. Introduce Explaining Variable
    if ( (platform.toUpperCase().indexOf("MAC") > -1) &&
    (browser.toUpperCase().indexOf("IE") > -1) &&
    wasInitialized() && resize > 0 )
    {
    // do something
    }
    final booleanisMacOs = platform.toUpperCase().indexOf("MAC") > -1;
    final booleanisIEBrowser = browser.toUpperCase().indexOf("IE") > -1;
    final booleanwasResized = resize > 0;
    if (isMacOs && isIEBrowser && wasInitialized() && wasResized)
    {
    // do something
    }
  • 13. Split Temporary Variable
    double 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);
  • 14. Remove Assignments to Parameters
    int discount (intinputVal, int quantity, intyearToDate) {
    if (inputVal > 50) inputVal -= 2;
    int discount (intinputVal, int quantity, intyearToDate) {
    int result = inputVal;
    if (inputVal > 50) result -= 2;
  • 15. Replace Method with Method Object
    class Order...
    double price() {
    double primaryBasePrice;
    double secondaryBasePrice;
    double tertiaryBasePrice;
    // long computation;
    ...
    }
  • 16. 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();
  • 17. Substitute Algorithm
    String foundPerson(String[] people){
    for (inti = 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 (inti=0; i<people.length; i++)
    if (candidates.contains(people[i]))
    return people[i];
    return "";
    }
  • 18. Resources
    Refactoring catalogs: http://www.refactoring.com/catalog/index.html
    Podcast about Refactoringhttp://www.se-radio.net/index.php?post_id=172668http://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