Code refactoring
-
Upload
lalit-kale -
Category
Software
-
view
154 -
download
0
Transcript of 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
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
5
Overview
• What is Refactoring?
• Why Refactoring?
• Refactoring Myths
• What To Refactor?
• Workflows Of Refactoring
• Refactoring Principles
• Refactoring Tools
• Refactoring Techniques
• Refactoring Thumbnails
• Summary
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:
• 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
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
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.”
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.
13
Why Refactor?
“Programs must be written for people to read, and only incidentally for machines to execute”—Abelson and Sussman
14
Why Refactor?
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
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
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
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.
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
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
27
Refactoring Principles• YAGNI – You Ain’t Gonna Need It!
• Boy-Scout Rule
• SOLID Principles
• Greatly Helpful Practice: Pair Programming
28
Refactoring Tools
• For .NET• Visual Studio IDE• Resharper by JetBrains• CodeRush by
DevExpress• JustCode by Telerik
• 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
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.
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
33
Refactoring Techniques• Refactoring Catalog
• http://refactoring.com/catalog/
• Problem Solution Summary of
well-known refactorings
• Maintained by Martin Fowler
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
36
Refactoring Thumbnails• Express the evolution of a design over period of time • Sequence of high-level refactoring• Present intermediate stages
Time
37
Refactoring Thumbnails - Example• Start with : copy-paste code• Evolve to : inheritance-based reuse • Passing an : intermediate stage • End with : interface-based solution
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
42
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
. 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 [email protected]
http://lalitkale.wordpress.com