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 ...
Search
Mehdi Lahmam B.
January 03, 2013
Programming
8
890
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
41
PG FDW FTW
mehlah
0
160
Product culture
mehlah
0
60
OpenAPI and AsyncAPI specifications as contracts
mehlah
0
650
Technical Debt
mehlah
1
280
Data informed growth
mehlah
0
220
Serverless Ruby and AWS Lambda
mehlah
0
160
Middleware all the things
mehlah
2
840
Confident refactors
mehlah
1
120
Other Decks in Programming
See All in Programming
イベントストーミング図からコードへの変換手順 / Procedure for Converting Event Storming Diagrams to Code
nrslib
2
800
なんとなくわかった気になるブロックテーマ入門/contents.nagoya 2025 6.28
chiilog
1
270
NPOでのDevinの活用
codeforeveryone
0
830
ISUCON研修おかわり会 講義スライド
arfes0e2b3c
1
440
テストから始めるAgentic Coding 〜Claude Codeと共に行うTDD〜 / Agentic Coding starts with testing
rkaga
12
4.4k
A2A プロトコルを試してみる
azukiazusa1
2
1.4k
Is Xcode slowly dying out in 2025?
uetyo
1
270
初学者でも今すぐできる、Claude Codeの生産性を10倍上げるTips
s4yuba
16
11k
Discover Metal 4
rei315
2
130
Claude Code + Container Use と Cursor で作る ローカル並列開発環境のススメ / ccc local dev
kaelaela
9
5k
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
470
データの民主化を支える、透明性のあるデータ利活用への挑戦 2025-06-25 Database Engineering Meetup#7
y_ken
0
360
Featured
See All Featured
Six Lessons from altMBA
skipperchong
28
3.9k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Bash Introduction
62gerente
613
210k
Git: the NoSQL Database
bkeepers
PRO
430
65k
How to Think Like a Performance Engineer
csswizardry
25
1.7k
Optimizing for Happiness
mojombo
379
70k
Making the Leap to Tech Lead
cromwellryan
134
9.4k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
6
300
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
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