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
910
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
15
Possible to measure developer productivity?
mehlah
0
43
PG FDW FTW
mehlah
0
160
Product culture
mehlah
0
67
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
850
Other Decks in Programming
See All in Programming
After go func(): Goroutines Through a Beginner’s Eye
97vaibhav
0
230
Web フロントエンドエンジニアに開かれる AI Agent プロダクト開発 - Vercel AI SDK を観察して AI Agent と仲良くなろう! #FEC余熱NIGHT
izumin5210
3
390
止められない医療アプリ、そっと Swift 6 へ
medley
1
120
Goで実践するドメイン駆動開発 AIと歩み始めた新規プロダクト開発の現在地
imkaoru
4
600
Django Ninja による API 開発効率化とリプレースの実践
kashewnuts
0
930
明日から始めるリファクタリング
ryounasso
0
110
プログラミングどうやる? ~テスト駆動開発から学ぶ達人の型~
a_okui
0
190
Your Perfect Project Setup for Angular @BASTA! 2025 in Mainz
manfredsteyer
PRO
0
130
非同期jobをtransaction内で 呼ぶなよ!絶対に呼ぶなよ!
alstrocrack
0
520
なぜGoのジェネリクスはこの形なのか? Featherweight Goが明かす設計の核心
ryotaros
7
1k
あなたの知らない「動画広告」の世界 - iOSDC Japan 2025
ukitaka
0
380
AIエージェント時代における TypeScriptスキーマ駆動開発の新たな役割
bicstone
4
1.5k
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
224
10k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
51k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.5k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.6k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
188
55k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
Designing for humans not robots
tammielis
254
25k
The World Runs on Bad Software
bkeepers
PRO
71
11k
Six Lessons from altMBA
skipperchong
28
4k
Designing Experiences People Love
moore
142
24k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
9
580
Building Applications with DynamoDB
mza
96
6.6k
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