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
More Decks by Mehdi Lahmam B.
See All by Mehdi Lahmam B.
Beyond code: Becoming a Product Engineer
mehlah
0
80
Possible to measure developer productivity?
mehlah
0
83
PG FDW FTW
mehlah
0
210
Product culture
mehlah
0
120
OpenAPI and AsyncAPI specifications as contracts
mehlah
0
720
Technical Debt
mehlah
1
340
Data informed growth
mehlah
0
300
Serverless Ruby and AWS Lambda
mehlah
0
200
Middleware all the things
mehlah
2
910
Other Decks in Programming
See All in Programming
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
460
[JA] Domain Modeling with Vanilla Rails: 「操作」から再考するRailsの設計論
yasaichi
4
740
JIS keyboard support in Omarchy
lef237
0
150
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
240
[Dev Group Paris Octobre 26] Agentforce, SIforce, Claudeforce, Slackbot… on s'y retrouve ?
nabondance
0
120
Simple Storage Service(S3) is not simple
iwatsukayura
0
310
PumaからFalconに移行したRailsアプリケーションでの実践
falcon8823
0
190
S3 TablesのIcebergデータに、AIエージェントからアクセスしてみた / 20261016developersio2026joetsu-accessing-iceberg-in-s3tables-via-agent
kasacchiful
PRO
0
190
Verilogで学ぶCPU自作入門
uyuki234
8
4.2k
🇪🇸 React Alicante 2026 (Workshop)
ythecombinator
PRO
0
100
Web Framework Benchmark Battle 2026, Any%
a_matsuda
2
2.7k
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
410
Featured
See All Featured
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
430
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
610
Exploring anti-patterns in Rails
aemeredith
4
540
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
Paper Plane
katiecoart
PRO
5
54k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Believing is Seeing
oripsolob
1
250
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
3
3.9k
[RailsConf 2023] Rails as a piece of cake
palkan
59
7.1k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.9k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
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