Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Refactoring - Improving the Design of Existing Code
Search
Mehdi Lahmam B.
January 03, 2013
Programming
8
830
Refactoring - Improving the Design of Existing Code
Inspired by Martin Fowler's Refactoring book.
Mehdi Lahmam B.
January 03, 2013
Tweet
Share
More Decks by Mehdi Lahmam B.
See All by Mehdi Lahmam B.
Possible to measure developer productivity?
mehlah
0
16
PG FDW FTW
mehlah
0
130
Product culture
mehlah
0
32
OpenAPI and AsyncAPI specifications as contracts
mehlah
0
620
Technical Debt
mehlah
1
250
Data informed growth
mehlah
0
180
Serverless Ruby and AWS Lambda
mehlah
0
140
Middleware all the things
mehlah
2
800
Confident refactors
mehlah
1
83
Other Decks in Programming
See All in Programming
Polarsの成長: v0.14からv1.0までの変遷と今後の展望
zerebom
1
350
Prompt FlowによるLLMアプリケーション開発
yuto2000
1
1k
AWSでゲームサーバーを運用! Amazon GameLiftのお話
iriikeita
0
200
コード生成を伴うLLMエージェント - 2024.07.18 Tokyo AI
smiyawaki0820
11
4.1k
AHC035解説
terryu16
0
710
最古の関数型言語「Lisp」ことはじめ / lisp_in_kamiyama
uhooi
1
190
12年前の『型システム入門』翻訳の思い出話
mame
11
1.2k
Harnessing Large Language Models for Training-free Video Anomaly Detection
tereka114
1
1.3k
Introduction to GitOps
hwchiu
0
110
CSC307 Lecture 11
javiergs
PRO
0
240
Modern Angular: Renovation for Your Applications
manfredsteyer
PRO
0
140
SRE チーム立ち上げ前に考えたこと・取り組んだこと / Considerations and Preparations Before Establishing an SRE Team
mackey0225
3
320
Featured
See All Featured
Designing for humans not robots
tammielis
247
25k
Ruby is Unlike a Banana
tanoku
96
10k
Done Done
chrislema
179
15k
Clear Off the Table
cherdarchuk
89
320k
The Art of Programming - Codeland 2020
erikaheidi
48
13k
Robots, Beer and Maslow
schacon
PRO
157
8.1k
Building an army of robots
kneath
301
42k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
228
16k
Automating Front-end Workflow
addyosmani
1362
200k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
277
13k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
13
430
Embracing the Ebb and Flow
colly
81
4.3k
Transcript
Refactoring Improving the Design of Existing Code @mehlah
“A change to the system that leaves its behavior unchanged,
but enhances some nonfunctional quality – simplicity, flexibility, understandability, performance” Kent Beck, Extreme Programming Explained
“A change made to internal structure of software to make
it easier to understand and modify without changing its observable behavior.” Martin Fowler, Refactoring
“To refactor is to take a bad design, chaos even,
and rework it into well-designed code, with baby steps” Mehdi Lahmam B.
puts “Show me the code.” puts “Talk is cheap.”
A video store Customer Movie 1 * 1 * statement()
daysRented: int Rental priceCode: int
A video store aCustomer aRental aMovie *[for all rentals] getMovie
getPriceCode getDaysRented statement
What are your impressions ?
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
Let’s Refactor
Before you start refactoring, check that you have a solid
suite of tests. These tests must be self-checking. Tip
Decomposing and Redistributing the Statement Method
None
Any fool can write code that a computer can understand.
Good programmers write code that humans can understand. Tip
Moving the Amount Calculation
None
None
None
State of classes after moving the charge method Customer Movie
1 * 1 * statement() daysRented: int Rental priceCode: int getCharge()
Extracting Frequent Renter Points
None
After extraction and movement of the frequent renter points calculation
Customer Movie 1 * 1 * statement() daysRented: int Rental priceCode: int getCharge() getFrequentRenterPoints()
aCustomer aRental aMovie *[for all rentals] getCharge getFrequentRenterPoints statement getPriceCode
getPriceCode
Removing Temps
None
Customer Movie 1 * 1 * statement() daysRented: int Rental
priceCode: int getCharge() getFrequentRenterPoints() getTotalCharge() getTotalFrequentRenterPoints()
aCustomer aRental aMovie getTotalCharge *[for all rentals]getCharge *[for all rentals]
getFrequentRenterPoints statement getPriceCode getPriceCode getTotalFrequentRenterPoints
Replacing the Conditional Logic on Price Code with Polymorphism
1. Move getCharge et getFrequentRenterPoints from Rental to Movie class
None
Movie getCharge() Regular Movie getCharge() Children Movie getCharge() New Release
Movie getCharge()
Price getCharge Regular Price getCharge Childrens Price getCharge New Release
Movie getCharge Movie getCharge return price->getCharge() Using the State pattern [GoF] on movie
2. Add a Price class with subclasses and change movie's
accessors for the price code to use the new class
None
3. Replace Movie's getCharge conditional with a type code behavior
in the Price Object
None
4. Move getFrequentRenterPoints method from Movie to Price and replace
conditional with polymorphism
Notes Typefaces: Open Sans + Abril GitHub repo (see closed
Pull Requests) https://github.com/craftsmen/refactoring-php-example
Thanks @mehlah