Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Code refactoring

732 views

Published on

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.

Published in: Software

Code refactoring

  1. 1. Code Refactoring Lalit Kale LalitKale@gmail.com
  2. 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. 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
  4. 4. 4 Source of Inspiration Martin Fowler
  5. 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
  6. 6. What is Refactoring? 6
  7. 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. 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. 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. 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. 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
  12. 12. Why Refactor? 12
  13. 13. 13 Why Refactor? “Programs must be written for people to read, and only incidentally for machines to execute”—Abelson and Sussman
  14. 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. 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. 16. Zen says “To move faster, you need to do it right”
  17. 17. Refactoring Myths 17
  18. 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. 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. 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
  21. 21. 21 Refactoring Myths Refactoring ≠ Re-Engineering
  22. 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
  23. 23. Workflows of Refactoring 23
  24. 24. 24 Workflows of Refactoring
  25. 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. 26. Refactoring Principles • KISS: Keep It Simple Stupid! • Avoid Clever code. Write Clear Code. 26
  27. 27. Refactoring Principles • YAGNI – You Ain’t Gonna Need It! • Boy-Scout Rule • SOLID Principles • Greatly Helpful Practice: Pair Programming 27
  28. 28. Refactoring Tools • For .NET • Visual Studio IDE • Resharper by JetBrains • CodeRush by DevExpress • JustCode by Telerik 28 • For Java • Eclipse IDE • IntelliJ • JRefactory
  29. 29. Refactoring Techniques 29
  30. 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. 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. 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. 33. Refactoring Techniques • Refactoring Catalog • http://refactoring.com/catalog/ • Problem Solution Summary of well- known refactorings • Maintained by Martin Fowler 33
  34. 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. 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. 36. Refactoring Thumbnails • Express the evolution of a design over period of time • Sequence of high-level refactoring • Present intermediate stages 36 Time
  37. 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. 38 Refactoring Thumbnails - Example Move Feature Refactoring
  39. 39. 39 Evolution Chart Eliminate Duplication By Composition
  40. 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. 41. Summary Refactoring ➔ Clean Code ➔ Faster Delivery ➔ Happy Customers  41
  42. 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 42
  43. 43. Thank You! 43
  44. 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

×