Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Refactoring - Improving the Design of Existing ...
Search
Mehdi Lahmam B.
January 03, 2013
Programming
1k
8
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Refactoring - Improving the Design of Existing Code
Inspired by Martin Fowler's Refactoring book.
Mehdi Lahmam B.
January 03, 2013
Transcript
39 slides
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
More Decks by Mehdi Lahmam B.
See All by Mehdi Lahmam B.
Beyond code: Becoming a Product Engineer
mehlah
0
83
Possible to measure developer productivity?
mehlah
0
87
PG FDW FTW
mehlah
0
210
Product culture
mehlah
0
120
OpenAPI and AsyncAPI specifications as contracts
mehlah
0
720
Technical Debt
mehlah
1
350
Data informed growth
mehlah
0
300
Serverless Ruby and AWS Lambda
mehlah
0
210
Middleware all the things
mehlah
2
920
Other Decks in Programming
See All in Programming
AIがコードを書いてくれる時代、我々は何を確かめるのか
yosuke_furukawa
PRO
13
6k
Rails初心者がパスキーを実装してみて大変だったこと
ktombow
0
190
ローカルLLMの基本と近況 2611 / Local LLM basics and recent situation 2611
kishida
2
430
地方こそサーバーレス — 10年続けた、顔が見えるチームで確かな基盤を / Serverless Matters Most in Regional Areas — 10 Years of Building Reliable Foundations with Close-Knit Teams
seike460
PRO
1
450
Scalaの型でマルチテナントの認可を守る
nextbeatdev
0
120
あなたはMCPの機能を、まだ知らない。
4geru
1
140
小さなアプリを作って公開するまで
onozaty
0
180
Spec-driven Development with AI
simas
PRO
0
2.3k
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
320
Vapor Mode でアクセシビリティは 壊れないか検証した話
fuqda
1
350
私なりのハーネスエンジニアリング ~独自の言語処理系の開発を通して~
seosoft
0
210
プロジェクトコードをキレイキレイしよう!暗黙的な参照を読み解く静的解析
ostk0069
0
210
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
The Spectacular Lies of Maps
axbom
PRO
1
1.1k
Writing Fast Ruby
sferik
630
63k
Exploring anti-patterns in Rails
aemeredith
4
570
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7.1k
Building Applications with DynamoDB
mza
96
7.3k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
60k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
600
Color Theory Basics | Prateek | Gurzu
gurzu
1
520
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
350
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
770
Odyssey Design
rkendrick25
PRO
3
950