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.

Refactoring, 2nd Edition

Code we've written once has to be kept readable, maintainable, understandable and extensible for many years. Good code is not self-serving but the foundation for working together.

Refactoring can help you to keep the quality of the relevant parts of our systems high.
The technique is really easy (almost too easy) - improve the naming, structure, and responsibility in small steps that don't change behavior and run your tests after each step.

18 years ago I got hooked on Refactoring when Martin Fowler's first book came out. I've been using it since then on a daily basis on many different projects. Since then a lot has changed, especially with the help of modern IDEs with their automated refactorings and intentions.

Now he asked me to help review the 2nd edition. Our discussions reminded me that each generation of developers should be taught this crucial skill. That's why I want to give an overview of core refactorings and code-smells but also demonstrate the tips and tricks of today's tools that make this task so much easier.
Plus a sneak preview of the upcoming book.

  • Login to see the comments

Refactoring, 2nd Edition

  1. 1. Refactoring, 2nd Ed. A love story Michael Hunger
  2. 2. Michael Hunger Open Sourcerer Neo4j @mesirii
  3. 3. I know what you're here for! Covers By:
  4. 4. It crashed at 940!
  5. 5. Which Refactoring do you like most? Extract Method Delete Unused Code Ok, you won :)
  6. 6. Questions?! 1. Who has seen the Refactoring book in a shelf? 2. Who has read it? 3. Who thought that it's just common sense? 4. Who refactors code on a daily basis? 5. Who mentors new developers?
  7. 7. “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” M. Fowler (1999)
  8. 8. My Refactoring Story Started 18 years ago
  9. 9. Martin Fowler Foreword by Erich Gamma Contributions by Kent Beck, William Opdyke, Don Roberts 28. Juni 1999
  10. 10. "A must-read"
  11. 11. I really liked that book too!
  12. 12. As did others ... … e.g. authors!
  13. 13. My Student thesis at TU Dresden
  14. 14. Student thesis at TU Dresden ""
  15. 15. Refactoring Tools for Java in 2000!
  16. 16. Fast Forward 18 years
  17. 17. A nice surprise
  18. 18. Michael, I'm Martin Fowler's editor at Addison-Wesley. We are working on a revision of his Refactoring book. Martin suggested that I reach out to you about reviewing the manuscript. He was very impressed with feedback you've provided on other projects. Thanks, Greg -- Gregory Doench Executive Editor Pearson Technology Group
  19. 19. Several Weeks and 435 Pages later
  20. 20. 28. November 2018 Latest Memo: Most people will be disappointed by the second edition
  21. 21. Like the original, this edition explains what refactoring is; why you should refactor; how to recognize code that needs refactoring; and how to actually do it successfully, no matter what language you use. ● Understand the process and general principles of refactoring ● Quickly apply useful refactorings to make a program easier to comprehend and change ● Recognize “bad smells” in code that signal opportunities to refactor ● Explore the refactorings, each with explanations, motivation, mechanics, and simple examples ● Build solid tests for your refactorings ● Recognize tradeoffs and obstacles to refactoring
  22. 22. Why a new book?
  23. 23. Refactoring is as important as ever Educate a new generation!
  24. 24. What changed?
  25. 25. No one knows anymore what video rentals are!
  26. 26. Javascript !?!?!
  27. 27. Book Structure (Basically the same) 1. Opening Narrative Example Theatre Invoicing 2. Principles 3. Code Smells 4. Testing 5. Catalogue a. most important ones first b. other refactorings 6. Dropped tangential topics and big refactorings
  28. 28. Refactoring Changes "Split Temporary Variable" => "Split Variable" "Extract Method" => "Extract Function" Add Parameter, Remove Parameter, Rename Method => Change Function Declaration
  29. 29. Extract Function Inline Function Extract Variable Inline Variable Rename Variable Encapsulate Variable Highlighted: Most important Refactorings Migrate Function Declaration Change Function Declaration Introduce Parameter Object Combine Functions into Class Combine Functions into Transform
  30. 30. New Refactorings All about Functions and Pipelines
  31. 31. Combine Functions into Class Combine Functions into Transform Move Statements into Function Move Statements to Callers Remove Dead Code Rename Field Rename Variable Replace Command with Function New Refactorings Replace Derived Variable with Query Replace Inline Code with Function Call Replace Loop with Pipeline Replace Query with Parameter Replace Subclass with Delegate Return Modified Value Split Phase
  32. 32. Web-Book Available on Safari
  33. 33. My take on the new book
  34. 34. Back to the Essence of Refactoring
  35. 35. Student thesis at TU Dresden ""
  36. 36. Topics 1. What, When, How 2. Benefits 3. Costs / Issues 4. History 5. Catalogue 6. Tools 7. Example
  37. 37. What?
  38. 38. Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing the observable behavior of the software.
  39. 39. Refactor (verb): to restructure software by applying a series of refactorings without changing the observable behavior of the software.
  40. 40. When?
  41. 41. Code Smells! If it stinks, change it - Grandma Beck
  42. 42. Mysterious Name Duplicated Code Long Function Long Parameter List Global Data Mutable Data Divergent Change Shotgun Surgery Feature Envy Data Clumps Primitive Obsession Repeated Switches Code Smells Lazy Element Speculative Generality Temporary Field Message Chains Middle Man Insider Trading Large Class Alternative Classes with Different Interfaces Data Class Refused Bequest Comments Loops
  43. 43. Adding features is hard?
  44. 44. Working Effectively with Legacy Code Michael Feathers
  45. 45. Full of complexity and bugs
  46. 46. Code as a Crime Scene Software Design X-Rays Adam Tornhill
  47. 47. Code is not habitable
  48. 48. Timeless Way of Building Christopher Alexander
  49. 49. How?
  50. 50. Refactoring is as much about safety testing, small steps, commit as it is about code changes.
  51. 51. Refactoring changes the programs in small steps. If you make a mistake, it is easy to find the bug.
  52. 52. Two Hats
  53. 53.
  54. 54. Add Function 1. Red 2. Green 3. Refactor
  55. 55. 1. Refactor 2. Green 3. Commit Refactor
  56. 56. Before you start refactoring, check that you have a solid suite of tests. These tests must be self-checking. Martin Fowler
  57. 57. Tests, are your safety net Listen to the flight attendent this one time
  58. 58. Make sure all tests are fully automatic and that they check their own results. Martin Fowler
  59. 59. Fast Tests
  60. 60. Test Ordering R.I.P. JunitMax
  61. 61. Many small commits Can squash later
  62. 62. Run your tests frequently. Localize tests whenever you compile - every test at least every day. Martin Fowler
  63. 63. A suite of tests is a powerful bug detector that decapitates the time it takes to find bugs. Martin Fowler
  64. 64. Unit tests are like the second pair of eyes of a pair programmer. Kent Beck
  65. 65. Kinds of Refactoring
  66. 66. Quick Fixes Refactoring Rewrite
  67. 67. Continous Clean-Up
  68. 68. Continous Clean-Up ● Rename ● Replace comment with function ● Make intent clear ● Intention in your IDE ● Not about polishing
  69. 69. DRY Three strikes and you refactor.
  70. 70. Boy Scout Rule Leave it better than you found it
  71. 71. Use >> Reuse Reuse within your module not across.
  72. 72. Planned Refactoring
  73. 73. ● Before adding a feature ● Addressing technical debt ● Improve testability (seams) ● Improve understandability ● For learning about a new code-base Planned Refactoring
  74. 74. (BIG) Refactoring / Rewrite
  75. 75. Sunken Cost Fallacy
  76. 76. Deliberate Discovery Dan North
  77. 77. Benefits
  78. 78. Forces you to test
  79. 79. Don’t let the fear that testing can’t catch all bugs stop you from writing the tests that will catch most bugs. Martin Fowler
  80. 80. It is better to write and run incomplete tests than not to run complete tests. Martin Fowler
  81. 81. Code Ownership Modify your code ownership policies to smooth refactoring.
  82. 82. Fights Code Rot and Decay
  83. 83. Improves Understanding
  84. 84. Costs
  85. 85. APIs Don’t publish interfaces prematurely. Downstream dependencies.
  86. 86. Side Effects e.g. Performance
  87. 87. How do I tell my Boss?
  88. 88. Time invested
  89. 89. Refactoring & Design
  90. 90. Refactoring & Patterns
  91. 91. Refactoring & Patterns ● patterns encapsulate good solutions in a context ● represent roles of components ● avoid code-smells ● they can be the goal of a design improvment ● refactorings are steps to those designs
  92. 92. Refactoring Catalogue
  93. 93. Refactoring Catalogue (my favorites) Move Function Split Loop Replace Loop with Pipeline (Stream) Replace Magic Literal Replace Derived Variable with Query Replace Typecode with Subclass Replace Superclass with Delegate Replace Control Flag with Break (Return) Introduce Special Case (Null Object) Replace Nested Conditionals with Guad Clauses Extract Function Extract Variable Push Statements into Function Push members up Extract Interface / Superclass Migrate Function Declaration Combine Functions into Transform Encapsulate Collection Replace Temp with Query Separate Query from Modifier Parameterize Function
  94. 94. Tools
  95. 95. Tools today make Refactoring a Joy
  96. 96. Smart Understanding of code & intent Useful suggestions Intentions & Quick fixes
  97. 97. Use and Learn the Shortcuts Pragmatic Programmer
  98. 98. Intentions & Quick Fixes
  99. 99. IntelliJ & all Jetbrains IDEs
  100. 100. Help Productivity Guide
  101. 101. What did you NOT use yet
  102. 102. Eclipse
  103. 103. Eclipse Language Server
  104. 104. Netbeans
  105. 105. VS Code & Visual Studio
  106. 106. Resources
  107. 107. Ask your questions? Thank you for your time! Follow @mesirii