$30 off During Our Annual Pro Sale. View Details »

Refactoring - Improving the Design of Existing Code

Refactoring - Improving the Design of Existing Code

Inspired by Martin Fowler's Refactoring book.

Mehdi Lahmam B.

January 03, 2013
Tweet

More Decks by Mehdi Lahmam B.

Other Decks in Programming

Transcript

  1. Refactoring
    Improving the Design of Existing Code
    @mehlah

    View Slide

  2. “A change to the system that leaves its
    behavior unchanged, but enhances some
    nonfunctional quality – simplicity,
    flexibility, understandability, performance”
    Kent Beck, Extreme Programming Explained

    View Slide

  3. “A change made to internal
    structure of software to make it easier to
    understand and modify without changing
    its observable behavior.”
    Martin Fowler, Refactoring

    View Slide

  4. “To refactor is to take a bad design, chaos
    even, and rework it into well-designed
    code, with baby steps”
    Mehdi Lahmam B.

    View Slide

  5. puts “Show me the code.”
    puts “Talk is cheap.”

    View Slide

  6. A video store
    Customer
    Movie
    1 *
    1
    *
    statement()
    daysRented: int
    Rental
    priceCode: int

    View Slide

  7. A video store
    aCustomer aRental aMovie
    *[for all rentals]
    getMovie
    getPriceCode
    getDaysRented
    statement

    View Slide

  8. What are
    your impressions ?

    View Slide

  9. When you find you have to add a feature
    to a program, and the program's code is
    not structured in a convenient way to
    add the feature, first refactor the
    program to make it easy to add the
    feature, then add the feature.
    Tip

    View Slide

  10. Let’s Refactor

    View Slide

  11. Before you start refactoring, check that
    you have a solid suite of tests. These
    tests must be self-checking.
    Tip

    View Slide

  12. Decomposing and Redistributing
    the Statement Method

    View Slide

  13. View Slide

  14. Any fool can write code that a computer
    can understand. Good programmers
    write code that humans can understand.
    Tip

    View Slide

  15. Moving the Amount Calculation

    View Slide

  16. View Slide

  17. View Slide

  18. View Slide

  19. State of classes after moving the charge
    method
    Customer
    Movie
    1 *
    1
    *
    statement()
    daysRented: int
    Rental
    priceCode: int
    getCharge()

    View Slide

  20. Extracting Frequent Renter
    Points

    View Slide

  21. View Slide

  22. After extraction and movement of the frequent renter
    points calculation
    Customer
    Movie
    1 *
    1
    *
    statement()
    daysRented: int
    Rental
    priceCode: int
    getCharge()
    getFrequentRenterPoints()

    View Slide

  23. aCustomer aRental aMovie
    *[for all rentals]
    getCharge
    getFrequentRenterPoints
    statement
    getPriceCode
    getPriceCode

    View Slide

  24. Removing Temps

    View Slide

  25. View Slide

  26. Customer
    Movie
    1 *
    1
    *
    statement()
    daysRented: int
    Rental
    priceCode: int
    getCharge()
    getFrequentRenterPoints()
    getTotalCharge()
    getTotalFrequentRenterPoints()

    View Slide

  27. aCustomer aRental aMovie
    getTotalCharge
    *[for all rentals]getCharge
    *[for all rentals]
    getFrequentRenterPoints
    statement
    getPriceCode
    getPriceCode
    getTotalFrequentRenterPoints

    View Slide

  28. Replacing the Conditional Logic
    on Price Code with Polymorphism

    View Slide

  29. 1. Move getCharge et getFrequentRenterPoints from Rental to Movie class

    View Slide

  30. View Slide

  31. Movie
    getCharge()
    Regular Movie
    getCharge()
    Children Movie
    getCharge()
    New Release Movie
    getCharge()

    View Slide

  32. Price
    getCharge
    Regular Price
    getCharge
    Childrens Price
    getCharge
    New Release Movie
    getCharge
    Movie
    getCharge
    return price->getCharge()
    Using the State pattern [GoF] on movie

    View Slide

  33. 2. Add a Price class with subclasses and change movie's accessors for the price code to use the new class

    View Slide

  34. View Slide

  35. 3. Replace Movie's getCharge conditional with a type code behavior in the Price Object

    View Slide

  36. View Slide

  37. 4. Move getFrequentRenterPoints method from Movie to Price and replace conditional with polymorphism

    View Slide

  38. Notes
    Typefaces: Open Sans + Abril
    GitHub repo (see closed Pull Requests)
    https://github.com/craftsmen/refactoring-php-example

    View Slide

  39. Thanks
    @mehlah

    View Slide