SlideShare a Scribd company logo
Code Refactoring
Lalit Kale
LalitKale@gmail.com
Preface
• Audience:
• Beginner and intermediate level of developers, technical leads and software architects
• interested in software design and development
• having trouble in understanding and Implementing software design Principles and Patterns
• People who are keen on improving their craft
• Presentation:
• Approx. Time: 1 Hour
• Discussion Over Monotonous Delivery
• Planned slides for Questions are marked with Question Icon, Feel free to jump in to express your thoughts
• Code Snippets to understand the concepts – Not Ready for Production
• All Views/Opinions expressed here are mine and nothing to do with my current/past employers
2
About Me
• Working on .NET from last 11 years
• Journey: Software Developer  Sr. Developer  Tech Lead  Architect
• Startup To Large Enterprise
• Hands on experience (Code daily for living)
• Interested in software architecture of High-Volume High Traffic distributed Systems
• Beginner in Python and Machine Learning
3
4
Source of Inspiration
Martin Fowler
Overview
• What is Refactoring?
• Why Refactoring?
• Refactoring Myths
• What To Refactor?
• Workflows Of Refactoring
• Refactoring Principles
• Refactoring Tools
• Refactoring Techniques
• Refactoring Thumbnails
• Summary
5
What is Refactoring?
6
What is Refactoring?
• Wikipedia
Refactoring is a redrafting process in which talk page content is moved, removed, revised,
restructured, hidden, or otherwise changed. It applies only in contexts where editors make
signed statements (such as talk and user namespaces),and has a number of uses, including:
7
• Improving the clarity and readability of a page
• Removal of off-topic, uncivil, unclear, or otherwise distracting material
• Restructuring of discussions for clarity
• Relocation of material to different sections or pages where it is more appropriate
What is Refactoring?
• Ward Cunningham
“Amelioration Pattern: The act of relieving ills and changing for the
better”
• ills = Software Rot factors
• Rigidity
• Fragility
• Needless complexity
• Toxicity
8
What is Code Refactoring?
• Martin Fowler
• "Refactoring is the process of changing a software system in such a way that it does not alter the
external behavior of the code yet improves its internal structure."
- Refactoring, The design of existing Code, Martin Fowler, 1999
• “Refactoring is a disciplined technique for restructuring an existing body of code, altering its
internal structure without changing its external behavior.
– Refactoring.com, Martin Fowler 2016
9
What is Code Refactoring?
“Refactoring is a disciplined technique for restructuring an existing body of
code, altering its internal structure without changing its external behavior.”
10
What is Code Refactoring?
Its heart is a series of small behavior preserving transformations. Each
transformation (called a “refactoring”) does little, but a sequence of
transformations can produce a significant restructuring. Since each
refactoring is small, it’s less likely to go wrong. The system is kept fully
working after each small refactoring, reducing the chances that a system can
get seriously broken during the restructuring.
11
Why Refactor?
12
13
Why Refactor?
“Programs must be written for people to read, and only incidentally for machines to
execute”—Abelson and Sussman
Why Refactor?
14
Green
Refactor
Red
Refactoring Makes code adaptable to change
Write a Failing Test Do All sins to Pass the Failing Test
– Kent Beck
Pay for all sins and clean up code
Why Refactor?
• Keep code focused
• Improving the clarity and readability of code
• Removal of unclear, or otherwise distracting boilerplates and code segments
• Restructuring for clarity
• Helps to pay timely technical debt
• Leads to lightweight pragmatic design
• Avoids code rot
15
Zen says “To move faster, you need to do it right”
Refactoring Myths
17
Refactoring Myths
• “If It isn’t Broken, Don't Fix It”
• So We do not need refactoring
• Then,
• You add more features
• You enhance existing features
• Better infrastructure emerge
• Your competition change
• Which Results in changing your application/system
• Fact: Change is one of the strongest force in software development
18
Refactoring Myths and Misunderstandings
• Symptoms
• “Refactoring Takes Time”
• “We need to plan for separate refactoring time “
• “This entire project needs refactoring”
• “Refactoring this project will need at least X Weeks/months”
• Underlying Myth: Refactoring is one time activity/Task
• Fact: Refactoring is continual Activity
19
Refactoring Myths and Misunderstandings
• Refactoring is Re-work
• Fact: Refactoring gives opportunity to fix inefficiencies in system
• For Refactoring, we need special tools
• Fact: Tools are important but not mandatory. Refactoring Mindset is mandatory
• Refactoring can replace Code Reviews
• Fact: Objective of Code reviews are quite different than those of refactoring.
So, you can not compare apples with oranges.
20
21
Refactoring Myths
Refactoring ≠ Re-Engineering
What To Refactor?
• In general
• Code written to satisfy the feature
• Unit Tests
• But Can be
• Database Queries and Schema
• Assembly/Package Arrangements
• Configurations
• API Endpoints
• Integration Tests
22
Workflows of Refactoring
23
24
Workflows of Refactoring
Refactoring Principles
• DRY – Do not Repeat Yourself
“Every piece of knowledge must have a single, unambiguous, authoritative
representation within a system” – Pragmatic Programmers
• If nothing else, duplication is the single most enemy you can pick up in
refactoring stage
25
Refactoring Principles
• KISS: Keep It Simple Stupid!
• Avoid Clever code. Write Clear Code.
26
Refactoring Principles
• YAGNI – You Ain’t Gonna Need It!
• Boy-Scout Rule
• SOLID Principles
• Greatly Helpful Practice: Pair Programming
27
Refactoring Tools
• For .NET
• Visual Studio IDE
• Resharper by JetBrains
• CodeRush by DevExpress
• JustCode by Telerik
28
• For Java
• Eclipse IDE
• IntelliJ
• JRefactory
Refactoring Techniques
29
Refactoring Techniques
• Abstraction Focused
• Encapsulate Field – force code to access the field with getter and setter methods
• Generalize Type – create more general types to allow for more code sharing
• Replace type-checking code with State/Strategy
• Replace conditional with polymorphism
30
Refactoring Techniques
• Isolation Focused
• Extract Class – moves part of the code from an existing class into a new class.
• Extract Method – to turn part of a larger method into a new method. By breaking down code
in smaller pieces, it is more easily understandable. This is also applicable to functions.
31
Refactoring Techniques
• Code Clarity Focused
• Move Method or Move Field – move to a more appropriate Class or source file
• Rename Method or Rename Field – changing the name into a new one that better reveals its
purpose
• Pull Up – move to a superclass
• Push Down – move to a subclass
32
Refactoring Techniques
• Refactoring Catalog
• http://refactoring.com/catalog/
• Problem Solution Summary of well-
known refactorings
• Maintained by Martin Fowler
33
34
Refactoring Thumbnails
Refactoring catalog is overwhelming…How I can remember everything?
You don’t need to…
Use Refactoring Thumbnails by Sven Gorts
Refactoring Thumbnails
• Clear, Pragmatic design will only emerge, If team members share a common understanding
• Common Understanding requires clear and unambiguous communication
• Should be quick and simple enough to iterate
• Without making actual code changes
Refactoring Thumbnails is the visual technique which satisfies all these
requirements
35
Refactoring Thumbnails
• Express the evolution of a design over period of time
• Sequence of high-level refactoring
• Present intermediate stages
36
Time
Refactoring Thumbnails - Example
• Start with : copy-paste code
• Evolve to : inheritance-based reuse
• Passing an : intermediate stage
• End with : interface-based solution
37
Time
38
Refactoring Thumbnails - Example
Move Feature Refactoring
39
Evolution Chart
Eliminate Duplication By Composition
Summary
• Refactoring is continuous activity and part of development workflow
• Re-Engineering != Refactoring
• Refactoring needs (unit+ Integration) tests to verify external behaviors.
• Refactoring Tools are extremely helpful but not mandatory
• Refactoring Techniques are better visualized with Refactoring Thumbnails and
Evolution Charts
40
Summary
Refactoring ➔ Clean Code ➔ Faster Delivery ➔ Happy Customers 
41
Resources
• https://en.wikipedia.org/wiki/Wikipedia:Refactoring_talk_pages
• http://www.refactoring.com
• www.Refactoring.be
• Book: Refactoring: Improving the Design of Existing Code
42
Thank You!
43
.
This presentation is shared under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license. More information for this license is available at http://creativecommons.org/licenses/by-nc-sa/4.0/
All trademarks are the property of their respective owners. Lalit Kale makes no warranties, express, implied or statutory, as to the information in this presentation.
Lalit Kale
lalitkale@gmail.com
http://lalitkale.wordpress.com

More Related Content

What's hot (20)

ODP
Refactoring Techniques
Mayada Ghanem
 
PPTX
Code smell overview
Pham Manh Lan
 
PPTX
Tech talks#6: Code Refactoring
Nguyễn Việt Khoa
 
PDF
Refactoring
Tausun Akhtary
 
PDF
Refactoring 101
Adam Culp
 
PPT
extreme Programming
Bilal Shah
 
PPTX
Code smells and remedies
Md.Mojibul Hoque
 
PPTX
Reverse engineering
Yuffie Valen
 
PPTX
DESIGN PATTERNS: Strategy Patterns
International Institute of Information Technology (I²IT)
 
PDF
Refactoring
Ricardo Terra
 
ODP
Introduction to Version Control
Jeremy Coates
 
PPT
Continuous integration
amscanne
 
PPT
Builder pattern
Shakil Ahmed
 
PPT
Software Inspection And Defect Management
Ajay K
 
PPTX
Technical Debt
Gary Short
 
PPT
Introduction to design patterns
Amit Kabra
 
PPTX
Software re engineering
deshpandeamrut
 
PPT
Pressman ch-11-component-level-design
Oliver Cheng
 
PPTX
Prototype Model
RhealynAcejo
 
Refactoring Techniques
Mayada Ghanem
 
Code smell overview
Pham Manh Lan
 
Tech talks#6: Code Refactoring
Nguyễn Việt Khoa
 
Refactoring
Tausun Akhtary
 
Refactoring 101
Adam Culp
 
extreme Programming
Bilal Shah
 
Code smells and remedies
Md.Mojibul Hoque
 
Reverse engineering
Yuffie Valen
 
Refactoring
Ricardo Terra
 
Introduction to Version Control
Jeremy Coates
 
Continuous integration
amscanne
 
Builder pattern
Shakil Ahmed
 
Software Inspection And Defect Management
Ajay K
 
Technical Debt
Gary Short
 
Introduction to design patterns
Amit Kabra
 
Software re engineering
deshpandeamrut
 
Pressman ch-11-component-level-design
Oliver Cheng
 
Prototype Model
RhealynAcejo
 

Viewers also liked (20)

PDF
Code Refactoring
Yu-Chih Lin
 
PDF
Multicore Explosion and its impact in application software development (Year ...
Vasu Thiyagarajan
 
PPTX
Refactoring code smell
martintsch
 
PPTX
Refaktoryzacja kodu w języku PHP
DevCastZone
 
PDF
FLTK Summer Course - Part III - Third Impact
Michel Alves
 
PDF
TMS - Schedule of Presentations and Reports
Michel Alves
 
PDF
Using Git on the Command Line
Brian Richards
 
PDF
FLTK Summer Course - Part I - First Impact - Exercises
Michel Alves
 
ODP
Servicios web con Python
Manuel Pérez
 
PDF
Blisstering drupal module development ppt v1.2
Anil Sagar
 
PDF
FLTK Summer Course - Part VI - Sixth Impact - Exercises
Michel Alves
 
PPTX
Manipulating file in Python
shoukatali500
 
PDF
"Git Hooked!" Using Git hooks to improve your software development process
Polished Geek LLC
 
PPT
Introduction to Git Commands and Concepts
Carl Brown
 
PDF
Git hooks For PHP Developers
Umut IŞIK
 
PDF
FLTK Summer Course - Part II - Second Impact
Michel Alves
 
PDF
Advanced Git
Sergiu-Ioan Ungur
 
PDF
FLTK Summer Course - Part VIII - Eighth Impact
Michel Alves
 
PDF
Code Refactoring - Live Coding Demo (JavaDay 2014)
Peter Kofler
 
PDF
FLTK Summer Course - Part II - Second Impact - Exercises
Michel Alves
 
Code Refactoring
Yu-Chih Lin
 
Multicore Explosion and its impact in application software development (Year ...
Vasu Thiyagarajan
 
Refactoring code smell
martintsch
 
Refaktoryzacja kodu w języku PHP
DevCastZone
 
FLTK Summer Course - Part III - Third Impact
Michel Alves
 
TMS - Schedule of Presentations and Reports
Michel Alves
 
Using Git on the Command Line
Brian Richards
 
FLTK Summer Course - Part I - First Impact - Exercises
Michel Alves
 
Servicios web con Python
Manuel Pérez
 
Blisstering drupal module development ppt v1.2
Anil Sagar
 
FLTK Summer Course - Part VI - Sixth Impact - Exercises
Michel Alves
 
Manipulating file in Python
shoukatali500
 
"Git Hooked!" Using Git hooks to improve your software development process
Polished Geek LLC
 
Introduction to Git Commands and Concepts
Carl Brown
 
Git hooks For PHP Developers
Umut IŞIK
 
FLTK Summer Course - Part II - Second Impact
Michel Alves
 
Advanced Git
Sergiu-Ioan Ungur
 
FLTK Summer Course - Part VIII - Eighth Impact
Michel Alves
 
Code Refactoring - Live Coding Demo (JavaDay 2014)
Peter Kofler
 
FLTK Summer Course - Part II - Second Impact - Exercises
Michel Alves
 
Ad

Similar to Code refactoring (20)

PPTX
Refactoring
Rabeya Bashri
 
PDF
The Power Of Refactoring (4developers Krakow)
Stefan Koopmanschap
 
PDF
The Power Of Refactoring (php|tek 09)
Stefan Koopmanschap
 
PPTX
Refactoring, 2nd Edition
jexp
 
PDF
Refactoring 2TheMax (con ReSharper)
DotNetMarche
 
PDF
Refactoring 2 The Max
Alfredo Morresi
 
PDF
The Power Of Refactoring (PHPCon Italia)
Stefan Koopmanschap
 
ODP
Refactoring: Improving the design of existing code
Knoldus Inc.
 
PPT
Principlesinrefactoring 090906230021-phpapp01
Sopheak Sem
 
PDF
Code Refactoring in Software Development
philipthomas428223
 
PPT
Principles in Refactoring
Chamnap Chhorn
 
PDF
Code refactoring workshop (in Javascript)
Ilias Bartolini
 
PPTX
Refactoring code in .net
Tung Nguyen Thanh
 
PPTX
Refactoring
AngelLuisBlasco
 
PDF
Code refactoring
Long Nguyen
 
PPTX
SAD10 - Refactoring
Michael Heron
 
PPTX
Refactoring, Emergent Design & Evolutionary Architecture
Brad Appleton
 
PDF
Unleashing the Power of Automated Refactoring with JDT
Naresh Jain
 
PDF
The Power Of Refactoring (PHPNW)
Stefan Koopmanschap
 
PDF
Software Craftsmanship
Manfredi Giordano
 
Refactoring
Rabeya Bashri
 
The Power Of Refactoring (4developers Krakow)
Stefan Koopmanschap
 
The Power Of Refactoring (php|tek 09)
Stefan Koopmanschap
 
Refactoring, 2nd Edition
jexp
 
Refactoring 2TheMax (con ReSharper)
DotNetMarche
 
Refactoring 2 The Max
Alfredo Morresi
 
The Power Of Refactoring (PHPCon Italia)
Stefan Koopmanschap
 
Refactoring: Improving the design of existing code
Knoldus Inc.
 
Principlesinrefactoring 090906230021-phpapp01
Sopheak Sem
 
Code Refactoring in Software Development
philipthomas428223
 
Principles in Refactoring
Chamnap Chhorn
 
Code refactoring workshop (in Javascript)
Ilias Bartolini
 
Refactoring code in .net
Tung Nguyen Thanh
 
Refactoring
AngelLuisBlasco
 
Code refactoring
Long Nguyen
 
SAD10 - Refactoring
Michael Heron
 
Refactoring, Emergent Design & Evolutionary Architecture
Brad Appleton
 
Unleashing the Power of Automated Refactoring with JDT
Naresh Jain
 
The Power Of Refactoring (PHPNW)
Stefan Koopmanschap
 
Software Craftsmanship
Manfredi Giordano
 
Ad

More from Lalit Kale (20)

PPTX
Serverless microservices
Lalit Kale
 
PPTX
Develop in ludicrous mode with azure serverless
Lalit Kale
 
PPTX
For Business's Sake, Let's focus on AppSec
Lalit Kale
 
PPTX
Introduction To Microservices
Lalit Kale
 
PPTX
Dot net platform and dotnet core fundamentals
Lalit Kale
 
PPTX
Application Security Tools
Lalit Kale
 
PPTX
Threat Modeling And Analysis
Lalit Kale
 
PPTX
Application Security-Understanding The Horizon
Lalit Kale
 
DOCX
Coding guidelines
Lalit Kale
 
DOCX
Code review guidelines
Lalit Kale
 
PPT
State management
Lalit Kale
 
PPT
Implementing application security using the .net framework
Lalit Kale
 
PPT
Data normailazation
Lalit Kale
 
PPT
Opps
Lalit Kale
 
DOCX
Versioning guidelines for product
Lalit Kale
 
PPT
Bowling Game Kata by Robert C. Martin
Lalit Kale
 
PPTX
Domain Driven Design
Lalit Kale
 
PPT
Web 2.0 concept
Lalit Kale
 
PPT
Jump Start To Ooad And Design Patterns
Lalit Kale
 
PPT
How To Create Strategic Marketing Plan
Lalit Kale
 
Serverless microservices
Lalit Kale
 
Develop in ludicrous mode with azure serverless
Lalit Kale
 
For Business's Sake, Let's focus on AppSec
Lalit Kale
 
Introduction To Microservices
Lalit Kale
 
Dot net platform and dotnet core fundamentals
Lalit Kale
 
Application Security Tools
Lalit Kale
 
Threat Modeling And Analysis
Lalit Kale
 
Application Security-Understanding The Horizon
Lalit Kale
 
Coding guidelines
Lalit Kale
 
Code review guidelines
Lalit Kale
 
State management
Lalit Kale
 
Implementing application security using the .net framework
Lalit Kale
 
Data normailazation
Lalit Kale
 
Versioning guidelines for product
Lalit Kale
 
Bowling Game Kata by Robert C. Martin
Lalit Kale
 
Domain Driven Design
Lalit Kale
 
Web 2.0 concept
Lalit Kale
 
Jump Start To Ooad And Design Patterns
Lalit Kale
 
How To Create Strategic Marketing Plan
Lalit Kale
 

Recently uploaded (20)

PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 

Code refactoring

  • 2. Preface • Audience: • Beginner and intermediate level of developers, technical leads and software architects • interested in software design and development • having trouble in understanding and Implementing software design Principles and Patterns • People who are keen on improving their craft • Presentation: • Approx. Time: 1 Hour • Discussion Over Monotonous Delivery • Planned slides for Questions are marked with Question Icon, Feel free to jump in to express your thoughts • Code Snippets to understand the concepts – Not Ready for Production • All Views/Opinions expressed here are mine and nothing to do with my current/past employers 2
  • 3. About Me • Working on .NET from last 11 years • Journey: Software Developer  Sr. Developer  Tech Lead  Architect • Startup To Large Enterprise • Hands on experience (Code daily for living) • Interested in software architecture of High-Volume High Traffic distributed Systems • Beginner in Python and Machine Learning 3
  • 5. Overview • What is Refactoring? • Why Refactoring? • Refactoring Myths • What To Refactor? • Workflows Of Refactoring • Refactoring Principles • Refactoring Tools • Refactoring Techniques • Refactoring Thumbnails • Summary 5
  • 7. What is Refactoring? • Wikipedia Refactoring is a redrafting process in which talk page content is moved, removed, revised, restructured, hidden, or otherwise changed. It applies only in contexts where editors make signed statements (such as talk and user namespaces),and has a number of uses, including: 7 • Improving the clarity and readability of a page • Removal of off-topic, uncivil, unclear, or otherwise distracting material • Restructuring of discussions for clarity • Relocation of material to different sections or pages where it is more appropriate
  • 8. What is Refactoring? • Ward Cunningham “Amelioration Pattern: The act of relieving ills and changing for the better” • ills = Software Rot factors • Rigidity • Fragility • Needless complexity • Toxicity 8
  • 9. What is Code Refactoring? • Martin Fowler • "Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure." - Refactoring, The design of existing Code, Martin Fowler, 1999 • “Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. – Refactoring.com, Martin Fowler 2016 9
  • 10. What is Code Refactoring? “Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.” 10
  • 11. What is Code Refactoring? Its heart is a series of small behavior preserving transformations. Each transformation (called a “refactoring”) does little, but a sequence of transformations can produce a significant restructuring. Since each refactoring is small, it’s less likely to go wrong. The system is kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring. 11
  • 13. 13 Why Refactor? “Programs must be written for people to read, and only incidentally for machines to execute”—Abelson and Sussman
  • 14. Why Refactor? 14 Green Refactor Red Refactoring Makes code adaptable to change Write a Failing Test Do All sins to Pass the Failing Test – Kent Beck Pay for all sins and clean up code
  • 15. Why Refactor? • Keep code focused • Improving the clarity and readability of code • Removal of unclear, or otherwise distracting boilerplates and code segments • Restructuring for clarity • Helps to pay timely technical debt • Leads to lightweight pragmatic design • Avoids code rot 15
  • 16. Zen says “To move faster, you need to do it right”
  • 18. Refactoring Myths • “If It isn’t Broken, Don't Fix It” • So We do not need refactoring • Then, • You add more features • You enhance existing features • Better infrastructure emerge • Your competition change • Which Results in changing your application/system • Fact: Change is one of the strongest force in software development 18
  • 19. Refactoring Myths and Misunderstandings • Symptoms • “Refactoring Takes Time” • “We need to plan for separate refactoring time “ • “This entire project needs refactoring” • “Refactoring this project will need at least X Weeks/months” • Underlying Myth: Refactoring is one time activity/Task • Fact: Refactoring is continual Activity 19
  • 20. Refactoring Myths and Misunderstandings • Refactoring is Re-work • Fact: Refactoring gives opportunity to fix inefficiencies in system • For Refactoring, we need special tools • Fact: Tools are important but not mandatory. Refactoring Mindset is mandatory • Refactoring can replace Code Reviews • Fact: Objective of Code reviews are quite different than those of refactoring. So, you can not compare apples with oranges. 20
  • 22. What To Refactor? • In general • Code written to satisfy the feature • Unit Tests • But Can be • Database Queries and Schema • Assembly/Package Arrangements • Configurations • API Endpoints • Integration Tests 22
  • 25. Refactoring Principles • DRY – Do not Repeat Yourself “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system” – Pragmatic Programmers • If nothing else, duplication is the single most enemy you can pick up in refactoring stage 25
  • 26. Refactoring Principles • KISS: Keep It Simple Stupid! • Avoid Clever code. Write Clear Code. 26
  • 27. Refactoring Principles • YAGNI – You Ain’t Gonna Need It! • Boy-Scout Rule • SOLID Principles • Greatly Helpful Practice: Pair Programming 27
  • 28. Refactoring Tools • For .NET • Visual Studio IDE • Resharper by JetBrains • CodeRush by DevExpress • JustCode by Telerik 28 • For Java • Eclipse IDE • IntelliJ • JRefactory
  • 30. Refactoring Techniques • Abstraction Focused • Encapsulate Field – force code to access the field with getter and setter methods • Generalize Type – create more general types to allow for more code sharing • Replace type-checking code with State/Strategy • Replace conditional with polymorphism 30
  • 31. Refactoring Techniques • Isolation Focused • Extract Class – moves part of the code from an existing class into a new class. • Extract Method – to turn part of a larger method into a new method. By breaking down code in smaller pieces, it is more easily understandable. This is also applicable to functions. 31
  • 32. Refactoring Techniques • Code Clarity Focused • Move Method or Move Field – move to a more appropriate Class or source file • Rename Method or Rename Field – changing the name into a new one that better reveals its purpose • Pull Up – move to a superclass • Push Down – move to a subclass 32
  • 33. Refactoring Techniques • Refactoring Catalog • http://refactoring.com/catalog/ • Problem Solution Summary of well- known refactorings • Maintained by Martin Fowler 33
  • 34. 34 Refactoring Thumbnails Refactoring catalog is overwhelming…How I can remember everything? You don’t need to… Use Refactoring Thumbnails by Sven Gorts
  • 35. Refactoring Thumbnails • Clear, Pragmatic design will only emerge, If team members share a common understanding • Common Understanding requires clear and unambiguous communication • Should be quick and simple enough to iterate • Without making actual code changes Refactoring Thumbnails is the visual technique which satisfies all these requirements 35
  • 36. Refactoring Thumbnails • Express the evolution of a design over period of time • Sequence of high-level refactoring • Present intermediate stages 36 Time
  • 37. Refactoring Thumbnails - Example • Start with : copy-paste code • Evolve to : inheritance-based reuse • Passing an : intermediate stage • End with : interface-based solution 37 Time
  • 38. 38 Refactoring Thumbnails - Example Move Feature Refactoring
  • 40. Summary • Refactoring is continuous activity and part of development workflow • Re-Engineering != Refactoring • Refactoring needs (unit+ Integration) tests to verify external behaviors. • Refactoring Tools are extremely helpful but not mandatory • Refactoring Techniques are better visualized with Refactoring Thumbnails and Evolution Charts 40
  • 41. Summary Refactoring ➔ Clean Code ➔ Faster Delivery ➔ Happy Customers  41
  • 44. . This presentation is shared under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license. More information for this license is available at http://creativecommons.org/licenses/by-nc-sa/4.0/ All trademarks are the property of their respective owners. Lalit Kale makes no warranties, express, implied or statutory, as to the information in this presentation. Lalit Kale [email protected] http://lalitkale.wordpress.com