Slide 1

Slide 1 text

Refactoring Improving the Design of Existing Code @mehlah

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

What are your impressions ?

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Let’s Refactor

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Decomposing and Redistributing the Statement Method

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Moving the Amount Calculation

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Extracting Frequent Renter Points

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Removing Temps

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Replacing the Conditional Logic on Price Code with Polymorphism

Slide 29

Slide 29 text

1. Move getCharge et getFrequentRenterPoints from Rental to Movie class

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Thanks @mehlah