Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Refactoring - Improving the Design of Existing ...

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. “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. “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. “To refactor is to take a bad design, chaos even,

    and rework it into well-designed code, with baby steps” Mehdi Lahmam B.
  4. A video store Customer Movie 1 * 1 * statement()

    daysRented: int Rental priceCode: int
  5. 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. Before you start refactoring, check that you have a solid

    suite of tests. These tests must be self-checking. Tip
  7. Any fool can write code that a computer can understand.

    Good programmers write code that humans can understand. Tip
  8. State of classes after moving the charge method Customer Movie

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

    Customer Movie 1 * 1 * statement() daysRented: int Rental priceCode: int getCharge() getFrequentRenterPoints()
  10. Customer Movie 1 * 1 * statement() daysRented: int Rental

    priceCode: int getCharge() getFrequentRenterPoints() getTotalCharge() getTotalFrequentRenterPoints()
  11. aCustomer aRental aMovie getTotalCharge *[for all rentals]getCharge *[for all rentals]

    getFrequentRenterPoints statement getPriceCode getPriceCode getTotalFrequentRenterPoints
  12. Price getCharge Regular Price getCharge Childrens Price getCharge New Release

    Movie getCharge Movie getCharge return price->getCharge() Using the State pattern [GoF] on movie
  13. 2. Add a Price class with subclasses and change movie's

    accessors for the price code to use the new class
  14. Notes Typefaces: Open Sans + Abril GitHub repo (see closed

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