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
850
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
23
PG FDW FTW
mehlah
0
140
Product culture
mehlah
0
37
OpenAPI and AsyncAPI specifications as contracts
mehlah
0
630
Technical Debt
mehlah
1
260
Data informed growth
mehlah
0
190
Serverless Ruby and AWS Lambda
mehlah
0
140
Middleware all the things
mehlah
2
820
Confident refactors
mehlah
1
98
Other Decks in Programming
See All in Programming
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1k
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
subpath importsで始めるモック生活
10tera
0
300
みんなでプロポーザルを書いてみた
yuriko1211
0
260
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
110
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.7k
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
660
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
32
1.5k
What's in a price? How to price your products and services
michaelherold
243
12k
The Language of Interfaces
destraynor
154
24k
GraphQLとの向き合い方2022年版
quramy
43
13k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Thoughts on Productivity
jonyablonski
67
4.3k
A better future with KSS
kneath
238
17k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Statistics for Hackers
jakevdp
796
220k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Become a Pro
speakerdeck
PRO
25
5k
A designer walks into a library…
pauljervisheath
203
24k
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