{"id":407,"date":"2011-12-10T08:55:02","date_gmt":"2011-12-10T07:55:02","guid":{"rendered":"http:\/\/www.dtsato.com\/blog\/?p=407"},"modified":"2011-12-12T09:38:08","modified_gmt":"2011-12-12T08:38:08","slug":"refactoring-strategies-a-walkthrough-experiment","status":"publish","type":"post","link":"http:\/\/www.dtsato.com\/blog\/2011\/12\/10\/refactoring-strategies-a-walkthrough-experiment\/","title":{"rendered":"Refactoring Strategies: a walkthrough experiment"},"content":{"rendered":"<p>In my last QCon <a title=\"Refatora\u00e7\u00e3o em Larga Escala\" href=\"http:\/\/www.qconsp.com\/palestra\/danilo-sato\/refatoracao-em-larga-escala\" target=\"_blank\">presentation<\/a> in S\u00e3o Paulo, I gave the audience my take on <strong>refactoring strategies<\/strong>. You can check out my slides from that talk on <a title=\"Refatora\u00e7\u00e3o em Larga Escala\" href=\"http:\/\/www.slideshare.net\/dtsato\/refatorao-em-larga-escala\" target=\"_blank\">Slideshare<\/a> (in Portuguese, but you should be able to follow the pictures and code samples).<\/p>\n<p>One of my main points is that you need to learn how to shift between two levels of thinking when you engage in refactoring, each requiring you to develop different sets of skills. I called them: <strong>Mechanic vs. Strategic<\/strong>.<\/p>\n<p><strong>Mechanics<\/strong> is important because you should be doing small, incremental, and <a title=\"Opportunistic Refactoring\" href=\"http:\/\/martinfowler.com\/bliki\/OpportunisticRefactoring.html\" target=\"_blank\">opportunistic refactorings<\/a> all the time while you develop code. It&#8217;s not a coincidence that TDD has an explicit step of refactoring after you tests are green. In order to improve the design of your code you should master how to perform these small steps well. A lot of the modern IDEs &#8211; especially in static typed languages &#8211; provide great support for this and you should learn how to use them well.<\/p>\n<p>On a <strong>strategic<\/strong> level, you have to be able to take a step back and understand the big picture of what you&#8217;re trying to achieve. While it&#8217;s important to know how to perform individual refactorings, it&#8217;s more important at this level to know how to compose them in order to achieve the design improvement you desire.<\/p>\n<p>One of the examples I used to demonstrate this, was inspired by a <a title=\"Refactoring Experiment\" href=\"http:\/\/paulhammant.com\/2011\/09\/01\/refactoring-experiment\/\" target=\"_blank\">blog entry<\/a> that Paul Hammant published a few weeks before my talk. I got some feedback that it was hard to follow the code changes on the slides, so I decided to record a small video of me tackling his refactoring experiment.<\/p>\n<p>In the following video, the strategy is not overly complex: extracting a few methods from a single controller into separate controllers. However, you will see how the order of steps you use can affect the refactoring, making your job harder or easier. You should also be able to pick up a lot of the mechanics along the way:<\/p>\n<p><iframe loading=\"lazy\" src=\"http:\/\/player.vimeo.com\/video\/33403686?portrait=0\" frameborder=\"0\" width=\"601\" height=\"338\"><\/iframe><\/p>\n<p>You can find some of my attempts in branches on this <a title=\"Github: Refactoring Experiment\" href=\"https:\/\/github.com\/dtsato\/refactoring_experiment\" target=\"_blank\">Github repository<\/a>. If you want to try it out for yourself, or to take this experiment further, these are some of the things I would explore:<\/p>\n<ul>\n<li>The <code>Upseller<\/code> class should have it&#8217;s own tests, and I missed the <code>@Component<\/code> and <code>@Scope(\"request\")<\/code> annotations that would probably be caught by integration tests.<\/li>\n<li>Using Mockito or some other mocking framework to mock collaborators, and adding more behaviour to the domain model objects since using <code>toString<\/code> for testing is not something I would particularly do in real life.<\/li>\n<li>Perhaps I could&#8217;ve used <code>git stash<\/code> instead of <code>git reset<\/code> to save the initial refactoring steps and later reapplying it assuming the merge would be simple.<\/li>\n<li>The path I took is not the only one and you can probably explore different refactoring steps and different approaches to get to the goal.<\/li>\n<\/ul>\n<p>I would like to thank Paul Hammant for coming up with this experiment and for encouraging me to publish this screencast. Please send me some feedback if you think this was useful or helpful for you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last QCon presentation in S\u00e3o Paulo, I gave the audience my take on refactoring strategies. You can check out my slides from that talk on Slideshare (in Portuguese, but you should be able to follow the pictures and code samples). One of my main points is that you need to learn how to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[21,157,39,158,64,156,82],"class_list":["post-407","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-design","tag-intellij","tag-java","tag-mechanics","tag-refactoring","tag-strategy","tag-tdd"],"_links":{"self":[{"href":"http:\/\/www.dtsato.com\/blog\/wp-json\/wp\/v2\/posts\/407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.dtsato.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.dtsato.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.dtsato.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.dtsato.com\/blog\/wp-json\/wp\/v2\/comments?post=407"}],"version-history":[{"count":6,"href":"http:\/\/www.dtsato.com\/blog\/wp-json\/wp\/v2\/posts\/407\/revisions"}],"predecessor-version":[{"id":413,"href":"http:\/\/www.dtsato.com\/blog\/wp-json\/wp\/v2\/posts\/407\/revisions\/413"}],"wp:attachment":[{"href":"http:\/\/www.dtsato.com\/blog\/wp-json\/wp\/v2\/media?parent=407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.dtsato.com\/blog\/wp-json\/wp\/v2\/categories?post=407"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.dtsato.com\/blog\/wp-json\/wp\/v2\/tags?post=407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}