SlideShare uma empresa Scribd logo
1 de 24
Baixar para ler 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

Mais conteúdo relacionado

Mais de Dhaval Dalal

Mais de 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
 

Último

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 

Refactoring