Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Refactoring Legacy Code - true story

4.244 Aufrufe

Veröffentlicht am

Veröffentlicht in: Internet
  • DOWNLOAD FULL BOOKS, INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier

Refactoring Legacy Code - true story

  1. 1. Refactoring legacy code True story
  2. 2. Juha.aaltonen@ambientia.fi
  3. 3. Aki Salmi @rinkkasatiainen • Hiking guide • Supervisor • Programmer • Blipper – but no pics today.
  4. 4. Refactoring legacy code True story
  5. 5. My goals for the talk • Show how what I have learned at coderetreats have been taken into production use • GOAL 1: Encourage us to practice the craft • Show what I’ve done – Test Driving my design • Show steps rather than finished ‘product’ • GOAL 2: initiate discussions on what I’ve done • Learn • GOAL 3: reflect on my work and decisions and how they actually seem now.
  6. 6. Tips for listeners • There’s going to be a lot of code • Rather than reading the code, try to smell it. • How clean the code seems to be?
  7. 7. What I’ve done – what I believe in • Is not either good or bad. • It has bugs (I’ve seen those) • It provides value to the customer every day
  8. 8. What is valuable (for someone) • Client • clients of the Client • Myself & fellow ambientians • How likely this code is going to change? • How likely the change I make is going to introduce bugs in future additions to this feature
  9. 9. Key process decisions • TDD as design tool. • Unit tests for changes • Practice TDD in real environment. Try to get feedback • Refactor often • Keep tests clean • ”if it is not important for the test, it is important not to be in the test!” • Note: Builder pattern
  10. 10. Key process decisions • Use proper tools • GIT, IntelliJ IDEA, VIM • Hamcrest, mockito, various other open-source components
  11. 11. 4 elements of simple design 1. Passes its tests 2. Minimizes duplication 3. Maximizes clarity 4. Has fewer elements
  12. 12. Coderetreat • 1 day of coding • Pair programming, 6 different pairing partner • Learn through pairing • Deliberate practice • experiment
  13. 13. Key takeaways from coderetreats • Baby steps – commit to git often. Rebase to keep git log clean • TDD-as-if-you-meant-it • Avoid conditionals, switches, try/catch • Only 4 lines per method • Avoid naked primitives • Only one assert/behavior per test • Sapir-Whorf hypothesis • Tell – Don’t ask: no getters/setters for objects
  14. 14. Object calisthenics 1. One level of indentation per method 2. Don’t use the ELSE keyword 3. Wrap all primitives and Strings 4. First class collections 5. One dot per line 6. Don’t abbreviate 7. Keep all entities small 8. No classes with more than two instance variables 9. No getters / setters / properties
  15. 15. Background of the system
  16. 16. Domain logic POJO, Spring, Hibernate UI (Grails) Controllers + Views Database
  17. 17. A live service since 2008 Providing value to Client and end-users every day since
  18. 18. Case 1: adding a search parameter
  19. 19. Search for a product The method had 21 parameters and 200LOC
  20. 20. And there’s more
  21. 21. ..and more ..you see the pattern.
  22. 22. What to do add yet another parameter?
  23. 23. What I did was a brief study • How the methods are used in the service? • Grails-based service (200LOC) uses it • Determine the current responsibilities • Service builds valid parameters, DAO consumes it. • Where the changes could be made? • Both the service & DAO • Is the method likely to change later? • YES
  24. 24. Step 1
  25. 25. An integration test, See the builder-pattern If it’s not important for the test, it is important not to be on the test
  26. 26. I changed the signature
  27. 27. The first model
  28. 28. And part of the service
  29. 29. Key decisions • Factory to hide implementation details • Sometimes Criteria handled Date, sometimes Calendar • Make it first to work, refactor then. • Create a ProductSearchCriteria per type – think about the name of the object • There were no tests before – try to make minimal impact on code.
  30. 30. Test on localhost. • The context is a bit more complex than I originally thought: • In one case, it was not enough to limit on Date/Calendar • Thus, the original factory-idea would turn into a bit more complicated problem
  31. 31. Step 2
  32. 32. Firstly: factory into builder • Build what you need – add items to CompositeSearchCriteria as need arises • Again – do the minimal changes to the grails- service in order to minimize errors
  33. 33. Second try
  34. 34. Second try
  35. 35. Tests for builder
  36. 36. Builder maybe too many responsibilities?
  37. 37. Key decisions • Do the smallest amount that is needed • Builder to support only those methods that are used.
  38. 38. Step 3
  39. 39. Minor changes to fix a bug
  40. 40. Bug # 2
  41. 41. Minor changes to fix a bug
  42. 42. Bug # 3
  43. 43. Sometimes maths just is too much
  44. 44. Bug # 4
  45. 45. Minor changes to fix a bug
  46. 46. Learnings • Fast to refactor • Next time: start from integration test • But one cannot integration test the whole – too many parameters • Minor changes somewhere in the controller code caused it to fail on other places. • Later, a colleague joined me. His first task was to add a new concept to domain. • Also for search
  47. 47. Case 2: Attachment handling
  48. 48. Targets • System used to have ~ 20 different attachment types. • Only 5 were needed. • Earlier supported only 1 attachment / type / product. Now should support more.
  49. 49. AttachmentService(Dump?)
  50. 50. Decision to be made • Where to start? • The only (business) knowledge came from the previous changes • I decided to tackle the switch-case structure • It was spread 4 times throughout the code • To ease the change from 20 to 5
  51. 51. A story of FileType
  52. 52. AttachmentFileType– RepositoryTest
  53. 53. AttachmentFileTypeRepos…
  54. 54. FileType
  55. 55. Step 2 Get rid of switch-case
  56. 56. From …
  57. 57. … to
  58. 58. Or in picture
  59. 59. Data-driven tests
  60. 60. ProductService from…
  61. 61. … to • Private method, thus no tests for this. • How am I sure this works? • I am not. Now. I was pretty sure. I think. Hope.
  62. 62. AttachmentService from…
  63. 63. … to
  64. 64. Step 3 Work with attachment directory/name
  65. 65. Things not to do • Have a constructor to throw an Exception. • How would I change this now: • Factory to create the directory • Create AttachmentDirectory only if dir exists • ready?
  66. 66. AttachmentDirectory
  67. 67. AttachmentDirFactory
  68. 68. AttDirFactoryTest
  69. 69. AttachmentFile(Factory) • Similar concept with AttachmentFile and AttachmentFileFactory • With one difference • AttachmentFile is interface and has two concrete classes PlainAttachmentFile and WebImageAttachmentFile • Use of Template-pattern (which changed a lot later)
  70. 70. AttachmentFileTemplTest
  71. 71. AttachmentFileTemplate starting to get messy
  72. 72. verify: AttachmentService
  73. 73. Step 4 do similar changes to AttachmentService# moveToAttachmentDir
  74. 74. AttachmentFile
  75. 75. AttachmentFile • Too many responsibilities. • Would turn up with lot of methods. I needed to do something. • Did try to not to repeat myself (not shown – ask for more info later)
  76. 76. The first draft
  77. 77. Step 5 Copying and moving files
  78. 78. >150 LOC @ AttachmentService
  79. 79. … to 4*4
  80. 80. The classes within the process
  81. 81. Steps • Factory to create an AttachmentFile • Factory creates a AttachmentFileProcess – process to move/copy/clone/delete the AttachmentFile • For specific AttachmentFileType, it does different things • The execute method takes one argument, AttachmentFileAction, which either is move/copy/clone/delete/rename
  82. 82. Some steps
  83. 83. AttchmntFileProcessFactory
  84. 84. PlainAttachmentFileProcess
  85. 85. ASpecificImageProcess
  86. 86. ASpecificImageProcess
  87. 87. FileCopyAction
  88. 88. FileDeleteAction
  89. 89. A mysterious test
  90. 90. Later steps • Renaming classes, reordering packages • Using template
  91. 91. One last thing – listen the tests
  92. 92. The difference:
  93. 93. And that led to • New class: AttachmentFileName • A domain logic for handling name of the Attachment • I’m working with this. Now.
  94. 94. Learnings • Slow to refactor • Test-Driven design can work even in brown-field projects • Integration to old system required integration tests • How valuable the changes were to the customer • With my current understanding: • Split the changes to two – deploy both separately.
  95. 95. Tests 0 50 100 150 200 250 alkuT1 Commit1-id:… pre-step2-id:… Commit2-id… Commit3-id:… Commit4-bugfix-id:… Commit5-bugfix-id:… Attachment-commit1-id:… Attachment-commit2-id:… Attachment-commit3-id:… Attachment-commit4-id:… Attachment-commit5-id:… Searchbugfix#3-id:… Search-bugfix#4-id:… pre-attachmetnt6-id:… Attachment-commit6-id:… Integrationtest-id:… Attachmentcommit7-id:… Attachmentcommit8-id:… Attachmentcommit8-id:… Attachmentcommit9-id:… Attachmentcommit10-id:… pre-attachment11-id:… Attachmentcommit11-id:… Attachmentcommit12-id:… Attachmentcommit13-id:… Attachmentcommit14-id:… Attachmentcommit15-id:… Attachmentcommit16-id:… Attachmentcommit17,integrationtest… Attachmentcommit18,changesto… Attachmentcommit19-attachment… Attachmentcommit20,fixingabug:… Attachmentcommit21,UI Attachmentcommit22:… Attachmentcommit23,changesto… Attachmentcommit24,filename:… Attachmentcommit25,massimport:… Attachmentcommit26,integ.test… Attachmentcommit27,filetype.equals… Attachmentcommit28,… Attachmentcommit29,errorhandling… Attachmentcommit30,Filterstowork… Attachmentcommit31:… Attachmentcommit32,uifix:… Passed Failed
  96. 96. Questions?
  97. 97. Puhelin: +358 50 341 5620 email: aki.salmi@iki.fi / aki.salmi@ambientia.fi Twitter: @rinkkasatiainen Aki Salmi

×