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
790
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.
PG FDW FTW
mehlah
0
110
Product culture
mehlah
0
28
OpenAPI and AsyncAPI specifications as contracts
mehlah
0
610
Technical Debt
mehlah
1
240
Data informed growth
mehlah
0
170
Serverless Ruby and AWS Lambda
mehlah
0
140
Middleware all the things
mehlah
2
800
Confident refactors
mehlah
1
74
If only I knew this shit in college
mehlah
0
170
Other Decks in Programming
See All in Programming
Open Source Swiftc Workshop
kitasuke
1
290
object-oriented-conference-2024
fuwasegu
6
1.9k
オブジェクト指向コードレビューの新しいアプローチ
akkie76
3
1.5k
Go1.22からの疑似乱数生成器について/go-122-pseudo-random-generator
convto
1
160
もうすぐ新年度、Babylon.jsがお勧めな3個の理由
hideg
0
170
[スクリプト] Swiftの型推論を学ぼう
omochi
0
120
「コンパイル時のユニットテスト」導入するとユニットテストを 書かなくてよくなるのか?
tomohisa
9
2.2k
LLMチャットボットのアプリケーション設計Tips
os1ma
4
660
DocC Tutorial と TCA におけるテスト機能の紹介
kalupas226
1
330
WinUI 3デモ - "CommunityToolkit.Mvvm"NuGetパッケージ編
andrewkeepcoding
0
130
Ruby製社内ツールのGo移行
bgpat
2
260
Kotlinを用いたDSL的な設計手法と使用上の注意
kohii00
3
530
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
160
13k
Web development in the modern age
philhawksworth
201
10k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
18
1.7k
How To Stay Up To Date on Web Technology
chriscoyier
781
250k
Practical Orchestrator
shlominoach
180
9.7k
The Art of Programming - Codeland 2020
erikaheidi
40
12k
Ruby is Unlike a Banana
tanoku
95
10k
Statistics for Hackers
jakevdp
789
220k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
67
38k
For a Future-Friendly Web
brad_frost
170
8.9k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
240
1.2M
A designer walks into a library…
pauljervisheath
199
23k
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