Refactoring - Improving the Design of Existing Code

Refactoring - Improving the Design of Existing Code

Inspired by Martin Fowler's Refactoring book.

C74bdcd3fa7c3d3f23290f46430b1463?s=128

Mehdi Lahmam B.

January 03, 2013
Tweet

Transcript

  1. 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
  2. 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
  3. 4.

    “To refactor is to take a bad design, chaos even,

    and rework it into well-designed code, with baby steps” Mehdi Lahmam B.
  4. 6.

    A video store Customer Movie 1 * 1 * statement()

    daysRented: int Rental priceCode: int
  5. 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
  6. 11.

    Before you start refactoring, check that you have a solid

    suite of tests. These tests must be self-checking. Tip
  7. 13.
  8. 14.

    Any fool can write code that a computer can understand.

    Good programmers write code that humans can understand. Tip
  9. 16.
  10. 17.
  11. 18.
  12. 19.

    State of classes after moving the charge method Customer Movie

    1 * 1 * statement() daysRented: int Rental priceCode: int getCharge()
  13. 21.
  14. 22.

    After extraction and movement of the frequent renter points calculation

    Customer Movie 1 * 1 * statement() daysRented: int Rental priceCode: int getCharge() getFrequentRenterPoints()
  15. 25.
  16. 26.

    Customer Movie 1 * 1 * statement() daysRented: int Rental

    priceCode: int getCharge() getFrequentRenterPoints() getTotalCharge() getTotalFrequentRenterPoints()
  17. 27.

    aCustomer aRental aMovie getTotalCharge *[for all rentals]getCharge *[for all rentals]

    getFrequentRenterPoints statement getPriceCode getPriceCode getTotalFrequentRenterPoints
  18. 30.
  19. 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
  20. 33.

    2. Add a Price class with subclasses and change movie's

    accessors for the price code to use the new class
  21. 34.
  22. 36.
  23. 38.

    Notes Typefaces: Open Sans + Abril GitHub repo (see closed

    Pull Requests) https://github.com/craftsmen/refactoring-php-example