Refactoring Strategies: Beyond the Basics

1,408 views

Published on

Refactoring is a fundamental practice for an agile developer. Each refactoring is a small and isolated activity, but the value comes when you perform a sequence of refactorings to effect larger changes. This workshop will guide participants through hands-on exercises designed to teach three essential skills to become good at refactoring:
1. Identifying what needs refactoring: what makes code smell, confusing, or complex?
2. Identifying the end goal: what are the design options and which one will yield the best results?
3. Tracing a strategy to get the code from 1 to 2.

Published in: Technology
  • Be the first to comment

Refactoring Strategies: Beyond the Basics

  1. 1. REFACTORING STRATEGIES: BEYOND THE BASICS Danilo Sato (@dtsato) dsato@thoughtworks.com Mariana Bravo (@marivb) mbravo@thoughtworks.com
  2. 2. WHAT WE ARE DOING TODAY PacMan Other 2
  3. 3. AGENDA ▫︎ ▫︎Introduction Exercise 1: Step-by-Step ▫︎ Exercise 2: On Your Own ▫︎ Form Pairs and Setup 3
  4. 4. FORM PAIRS & SETUP ▫︎ ... if you have internet connection: $ git clone -b workshop_start https://github.com/dtsato/ pacman_workshop.git pacman_workshop ▫︎ ... if you have local network: $ git clone git@<IP>:/tmp/workshop pacman_workshop IP: X.X.X.X Password: **** pen ▫︎Oun allproject in IDE (Eclipse or IntelliJ) R tests ▫︎ 4
  5. 5. INTRODUCTION Concepts and Examples 5
  6. 6. REFACTORING "Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior" 6
  7. 7. CODE SMELL "Code smell is a symptom in the source code that possibly indicates a deeper problem with the system" 7
  8. 8. http://martinfowler.com/articles/workflowsOfRefactoring/ 8
  9. 9. EMERGENT DESIGN: JUNIT 9
  10. 10. REFACTORING STRATEGIES 10
  11. 11. EXERCISE 1 Step-by-Step 11
  12. 12. Maze 12
  13. 13. Tile Current Tile determined by Actor’s center 13
  14. 14. X Y [0, 6] TileCoordinate 14
  15. 15. WHAT IS THE PROBLEM? coordinates are exposed and used throughout the codebase as int x, y Tile Code smells: ● Primitive Obsession ● Data Clump 15
  16. 16. OBJECTIVES Finish the refactoring to use a new TileCoordinate class Focus on Maze class Encapsulate int x, y inside the TileCoordinate class 16
  17. 17. Refactoring Strategy STEP-BY-STEP Exercise_1_instructions.html
  18. 18. DISCUSSION What have you learned? 18
  19. 19. R LIMITED E D 19
  20. 20. CONTINUOUS DELIVERY 20
  21. 21. ALWAYS RELEASABLE 21
  22. 22. EXPAND-CONTRACT (A) Client A Subject oldWay() Client B 22
  23. 23. EXPAND-CONTRACT (A) Client A Client B Subject oldWay() newWay() 23
  24. 24. EXPAND-CONTRACT (A) Client A Client B Subject oldWay() newWay() 24
  25. 25. EXPAND-CONTRACT (A) Client A Client B Subject newWay() Inline Method 25
  26. 26. EXPAND-CONTRACT (B) Client A Subject oldWay() Client B 26
  27. 27. EXPAND-CONTRACT (B) Client A Client B Subject oldWay() newWay() 27
  28. 28. EXPAND-CONTRACT (B) Client A Client B Subject oldWay() newWay() 28
  29. 29. EXPAND-CONTRACT (B) Client A Client B Subject oldWay() newWay() 29
  30. 30. EXPAND-CONTRACT (B) Subject Client A Client B newWay() Safe Delete 30
  31. 31. EXERCISE 2 Your own Refactoring Strategy 31
  32. 32. Actor center, direction + collidesWith(Actor) + advance(millis) + getNextTile(current) Ghost Pacman 32
  33. 33. WHAT IS THE PROBLEM? The fields required to decide where to move are specific to each subclass of Actor and the implementations are very different Code Smells: ●Hard to Test ●Different constructors for Pacman/Ghost ●Different implementations 33
  34. 34. Actor MovementStrategy + getNextTile() Ghost Strategy Pacman Strategy 34
  35. 35. OBJECTIVES Extract a new interface MovementStrategy to decide what is the next Tile Implement this interface twice: PacmanStrategy and GhostStrategy Replace “Template Method” with composition in Actor class 35
  36. 36. REFACTORING STRATEGY ▫︎Keep coverage high and tests passing! ▫︎ Now it’s your turn! ▫︎Decide your strategy ▫︎Implement! ▫︎ Explore the code 36
  37. 37. DISCUSSION What have you learned? 37
  38. 38. REFERENCES ▫︎ Refactoring: Improving the design of existing code ▫︎ Martin Fowler, Kent Beck, et.al ▫︎ Refactoring to Patterns ▫︎ Joshua Kerievsky ▫︎ Extreme Programming Explained 2nd Edition ▫︎ Kent Beck, Cynthia Andres ▫︎ The Mikado Method ▫︎ Ola Ellnestam, Daniel Brolund ▫︎ Domain Driven Design: Tackling Complexity in the Heart of Software ▫︎ Eric Evans 38
  39. 39. 39
  40. 40. THANK YOU For questions or suggestions: Danilo Sato (@dtsato) dsato@thoughtworks.com Mariana Bravo (@marivb) mbravo@thoughtworks.com

×