This presentation is part of one of my webinar in clean code webinar series. The contents are slightly edited to share the information in public domain. In this presentation, I tried to provide detailed introduction to code refactoring practice.
This presentation will be useful for software architects/Managers,developers and QAs. Do share your feedback in comments.
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
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
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
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
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
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
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
lalitkale@gmail.com
http://lalitkale.wordpress.com