SlideShare a Scribd company logo
1 of 24
Download to read offline
Refactoring 
Dhaval Dalal 
software-artisan.com 
@softwareartisan
dhaval.dalal@software-artisan.com 
Refactoring Metaphor 
John Thompson, the hatter makes 
and sells hats for ready money!
dhaval.dalal@software-artisan.com 
Refactoring Metaphor 
John Thompson, the hatter makes 
and sells hats for ready money!
dhaval.dalal@software-artisan.com 
Refactoring Metaphor 
John Thompson, the hatter makes 
and sells hats for ready money!
dhaval.dalal@software-artisan.com 
Refactoring Metaphor 
John Thompson, the hatter makes 
and sells hats for ready money!
dhaval.dalal@software-artisan.com 
Refactoring Metaphor 
John Thompson
dhaval.dalal@software-artisan.com 
What is Refactoring? 
Refactoring means improving the design 
after it has been written. 
Martin Fowler.
dhaval.dalal@software-artisan.com 
Why Should I Refactor? 
! Development is a process of Discovery 
and code constantly evolves. 
! Refactoring is a code curative 
technique, just as Sleep is a Curative 
Agency for the body playing part in 
repairing mechanism of the body. 
5
dhaval.dalal@software-artisan.com 
Why Should I Refactor? 
Any fool can write code that a computer 
can understand. Good programmers write 
code that humans can understand. The 
purpose of refactoring is to make the 
software easier to understand and modify. 
Martin Fowler.
dhaval.dalal@software-artisan.com 
Why Should I Refactor? 
! It improves the design of software 
" By repairing a highly coupled, non-cohesive 
design. 
! Helps to find bugs faster. 
! Helps to Program Faster.
dhaval.dalal@software-artisan.com 
Code Smells…lets code along 
! Duplicate Code 
! Switch Statements 
! Long Method 
! Long Parameter List 
! Large Class 
! Indecent Exposure 
! Divergent Change 
! Shotgun Surgery 
! ...and many more. 
If it Stinks, 
Change it! 
! Speculative Generality Kent Beck
dhaval.dalal@software-artisan.com 
Deodorize With Fresheners…code along 
! Encapsulate Collections 
! Encapsulate Downcast 
! Replace Error Code with Exception 
! Introduce Variable 
! Introduce Symbolic Constants 
! Rename Method/Variable 
! Remove Control Flag 
! Simplify Conditionals 
! Simply Nested Conditionals with Guard Clauses 
! Move Method 
! Chain Multiple Constructors/Methods 
! Replace Type Code with Enums or Class
dhaval.dalal@software-artisan.com 
Stronger Deodorizing Agents… 
! Replace Array with Object 
! Replace conditionals with Polymorphism 
! Replace Type Code with State/Strategy 
! Introduce Null Object 
...and many more
dhaval.dalal@software-artisan.com 
General Principles in Refactoring 
! From Near to Far. 
" That which is obvious and close to us now 
is the starting point, a.k.a the inflection 
point. 
" That which is abstract is the goal. 
! Take baby-steps. 
11
dhaval.dalal@software-artisan.com 
Refactoring Cycle 
12 
Choose on 
appropriate 
Refactoring to apply 
Apply Refactoring 
Run ALL Tests 
(Get GREEN Bar) 
Reached 
Desired 
Structure? 
Yes 
No
dhaval.dalal@software-artisan.com 
Undertaking Big Refactorings 
(Bridge Replacement Metaphor) 
! Let traffic continue to 
flow on old bridge. 
! Create new refactored 
implementation. 
13 
! Create another bridge 
side-by-side the old one. 
! Take an Interface-oriented 
approach. 
! House the old code in 
one implementation. 
! When the new bridge is 
ready, delink traffic from 
the old one and re-route it 
over the new bridge. 
! Use the new impl. by 
replacing the old one.
dhaval.dalal@software-artisan.com 
Refactoring To Patterns 
! Arrive to pattern via series of small 
refactorings. 
" Rigorously apply OO principles along-side 
continuous refactoring. 
# SRP, OCP, LSP, ISP, DIP (SOLID) etc... 
" You will eventually evolve to some known 
pattern. 
! When you “intuit” that a pattern will 
readily apply. 
" Approach this in baby steps. 14
dhaval.dalal@software-artisan.com 
Refactoring and Legacy Code 
! Code without tests is Legacy. 
" Does anyone still create this? 
" Michael Feathers’ has an excellent book on 
this. 
#Working Effectively with Legacy Code 
! Refactoring code without tests is like 
performing a tight-rope walk without any 
safety net underneath. 
15
dhaval.dalal@software-artisan.com 
Motivators and Inhibitors
dhaval.dalal@software-artisan.com 
As in many things the answer lies in 
balancing opposing forces, 
especially when its difficult to make 
hidden Business Value explicit. 
Pragmatic Vs Dogmatic 
Agility Vs Fragility 
17 
When to Refactor?
dhaval.dalal@software-artisan.com Views on Refactoring 
! A traditionalist view on Refactoring is 
re-work! 
! In reality, it is responding to new 
information as it arrives. 
! At the same time, avoid “Major 
Refactorings” 
" Do initial architecture envisioning. 
18
dhaval.dalal@software-artisan.com 
No Silver Bullet 
Refactoring is no “silver bullet”, yet it is 
a valuable tool, a pair of silver pliers 
that helps you keep good grip on your 
code. Without refactoring, the design 
of the program will decay. 
Martin Fowler
dhaval.dalal@software-artisan.com 
References 
! Refactoring 
" Martin Fowler. 
! Refactoring to Patterns 
" Joshua Kerievsky. 
! Refactoring Motivations 
" Jay Fields’ Blog 
! Creating an Agile Culture to drive organizational Change 
" David Anderson
Thank You! 
Dhaval Dalal 
dhaval.dalal@software-artisan.com

More Related Content

More from Dhaval Dalal

More from Dhaval Dalal (20)

Mars rover-extension
Mars rover-extensionMars rover-extension
Mars rover-extension
 
How Is Homeopathy Near To Yoga?
How Is Homeopathy Near To Yoga?How Is Homeopathy Near To Yoga?
How Is Homeopathy Near To Yoga?
 
Approaching ATDD/BDD
Approaching ATDD/BDDApproaching ATDD/BDD
Approaching ATDD/BDD
 
Paradigms Code jugalbandi
Paradigms Code jugalbandiParadigms Code jugalbandi
Paradigms Code jugalbandi
 
Data Reconciliation
Data ReconciliationData Reconciliation
Data Reconciliation
 
DRYing to Monad in Java8
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8
 
CodeRetreat
CodeRetreatCodeRetreat
CodeRetreat
 
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr20154-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
 
Jumping-with-java8
Jumping-with-java8Jumping-with-java8
Jumping-with-java8
 
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar20153-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
 
CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015
CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015
CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015
 
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-IssueCodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
 
The tao-of-transformation-workshop
The tao-of-transformation-workshopThe tao-of-transformation-workshop
The tao-of-transformation-workshop
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with Groovy
 
Language portfolio
Language portfolioLanguage portfolio
Language portfolio
 
Code jugalbandi
Code jugalbandiCode jugalbandi
Code jugalbandi
 
A case-for-graph-db
A case-for-graph-dbA case-for-graph-db
A case-for-graph-db
 
Transition contours
Transition contoursTransition contours
Transition contours
 
Healthy Code Magazine June-2014 Issue Midas-Touch-Article
Healthy Code Magazine June-2014 Issue Midas-Touch-ArticleHealthy Code Magazine June-2014 Issue Midas-Touch-Article
Healthy Code Magazine June-2014 Issue Midas-Touch-Article
 
Neo4j MySql MS-SQL comparison
Neo4j MySql MS-SQL comparisonNeo4j MySql MS-SQL comparison
Neo4j MySql MS-SQL comparison
 

Recently uploaded

Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
FIDO Alliance
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
panagenda
 

Recently uploaded (20)

Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overview
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdf
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 

Refactoring