Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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
920
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.
Beyond code: Becoming a Product Engineer
mehlah
0
36
Possible to measure developer productivity?
mehlah
0
43
PG FDW FTW
mehlah
0
170
Product culture
mehlah
0
74
OpenAPI and AsyncAPI specifications as contracts
mehlah
0
660
Technical Debt
mehlah
1
290
Data informed growth
mehlah
0
230
Serverless Ruby and AWS Lambda
mehlah
0
170
Middleware all the things
mehlah
2
860
Other Decks in Programming
See All in Programming
モダンJSフレームワークのビルドプロセス 〜なぜReactは503行、Svelteは12行なのか〜
fuuki12
0
190
Reactive Thinking with Signals and the new Resource API
manfredsteyer
PRO
0
160
AWS CDKの推しポイントN選
akihisaikeda
1
240
[堅牢.py #1] テストを書かない研究者に送る、最初にテストを書く実験コード入門 / Let's start your ML project by writing tests
shunk031
11
6.9k
スタートアップを支える技術戦略と組織づくり
pospome
8
15k
手が足りない!兼業データエンジニアに必要だったアーキテクチャと立ち回り
zinkosuke
0
400
Building AI Agents with TypeScript #TSKaigiHokuriku
izumin5210
6
1.2k
分散DBって何者なんだ... Spannerから学ぶRDBとの違い
iwashi623
0
170
AIコーディングエージェント(NotebookLM)
kondai24
0
130
C-Shared Buildで突破するAI Agent バックテストの壁
po3rin
0
220
GeistFabrik and AI-augmented software development
adewale
PRO
0
260
20251127_ぼっちのための懇親会対策会議
kokamoto01_metaps
2
410
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
How to Think Like a Performance Engineer
csswizardry
28
2.3k
Navigating Team Friction
lara
191
16k
Facilitating Awesome Meetings
lara
57
6.7k
We Have a Design System, Now What?
morganepeng
54
7.9k
Code Reviewing Like a Champion
maltzj
527
40k
jQuery: Nuts, Bolts and Bling
dougneiner
65
8.1k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.8k
The World Runs on Bad Software
bkeepers
PRO
72
12k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
Fireside Chat
paigeccino
41
3.7k
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