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. Refactoring Improving the Design of Existing Code @mehlah

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

    and rework it into well-designed code, with baby steps” Mehdi Lahmam B.
  5. puts “Show me the code.” puts “Talk is cheap.”

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

    daysRented: int Rental priceCode: int
  7. A video store aCustomer aRental aMovie *[for all rentals] getMovie

    getPriceCode getDaysRented statement
  8. What are your impressions ?

  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
  10. Let’s Refactor

  11. Before you start refactoring, check that you have a solid

    suite of tests. These tests must be self-checking. Tip
  12. Decomposing and Redistributing the Statement Method

  13. None
  14. Any fool can write code that a computer can understand.

    Good programmers write code that humans can understand. Tip
  15. Moving the Amount Calculation

  16. None
  17. None
  18. None
  19. State of classes after moving the charge method Customer Movie

    1 * 1 * statement() daysRented: int Rental priceCode: int getCharge()
  20. Extracting Frequent Renter Points

  21. None
  22. After extraction and movement of the frequent renter points calculation

    Customer Movie 1 * 1 * statement() daysRented: int Rental priceCode: int getCharge() getFrequentRenterPoints()
  23. aCustomer aRental aMovie *[for all rentals] getCharge getFrequentRenterPoints statement getPriceCode

    getPriceCode
  24. Removing Temps

  25. None
  26. Customer Movie 1 * 1 * statement() daysRented: int Rental

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

    getFrequentRenterPoints statement getPriceCode getPriceCode getTotalFrequentRenterPoints
  28. Replacing the Conditional Logic on Price Code with Polymorphism

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

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

    Movie getCharge()
  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
  33. 2. Add a Price class with subclasses and change movie's

    accessors for the price code to use the new class
  34. None
  35. 3. Replace Movie's getCharge conditional with a type code behavior

    in the Price Object
  36. None
  37. 4. Move getFrequentRenterPoints method from Movie to Price and replace

    conditional with polymorphism
  38. Notes Typefaces: Open Sans + Abril GitHub repo (see closed

    Pull Requests) https://github.com/craftsmen/refactoring-php-example
  39. Thanks @mehlah