{"id":560,"date":"2010-03-26T10:08:59","date_gmt":"2010-03-26T17:08:59","guid":{"rendered":"https:\/\/simpleprogrammer.com\/?p=560"},"modified":"2018-02-23T10:21:08","modified_gmt":"2018-02-23T15:21:08","slug":"refactor-then-change-legacy-code","status":"publish","type":"post","link":"https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/","title":{"rendered":"Refactor then Change Legacy Code"},"content":{"rendered":"<p>I was reminded yesterday of a very important step I had been forgetting when working with legacy code. \u00a0The first step. \u00a0Refactor.<\/p>\n<p><a href=\"http:\/\/www.amazon.com\/gp\/product\/B00004SVRJ\/ref=as_li_ss_tl?ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B00004SVRJ&amp;linkCode=as2&amp;tag=makithecompsi-20\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-561\" title=\"legacy\" alt=\"legacy code\" src=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/legacy.jpg\" width=\"346\" height=\"494\" srcset=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/legacy.jpg 346w, https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/legacy-210x300.jpg 210w\" sizes=\"auto, (max-width: 346px) 100vw, 346px\" \/><\/a><\/p>\n<p>Working with legacy code can be challenging. \u00a0Especially code that was written by someone who didn&#8217;t know what they were doing and then modified 10 times by someone who didn&#8217;t care what they were doing. \u00a0(This is perhaps 90% of legacy code.)<\/p>\n<p>I was having the tendency to jump right in and start implementing my clean feature in my own class that I would integrate into the legacy code, and move the relevant logic into my own class.<\/p>\n<h2>The good way<\/h2>\n<p>Let me see if I can summarize what my steps have looked like:<\/p>\n<ol>\n<li>Create failing unit test for new functionality I am implementing.<\/li>\n<li>Create new class which only has the logic for that functionality, but that I know overlaps some of the legacy code.<\/li>\n<li>Repeat until my functionality is working.<\/li>\n<li>Integrate the use of my class into the legacy code.<\/li>\n<li>Start moving parts of the legacy code that share the responsibility of my class into my class.<\/li>\n<li>Refactor the remaining legacy code.<\/li>\n<\/ol>\n<h2>The better way<\/h2>\n<p>I don&#8217;t think these steps are that bad. \u00a0But there is one problem, which is easily solved by adding the step of &#8220;Refactor the legacy code&#8221; at the beginning. \u00a0The problem is that of clearly knowing the responsibilities of the legacy code. \u00a0Most of the time in this situation the legacy code has multiple\u00a0responsibilities. \u00a0When you are done\u00a0implementing\u00a0the new functionality and cleaning up the code, you should end up with several classes in single responsibility (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Single_responsibility_principle\">SRP<\/a>).<\/p>\n<p>The problem is that poorly written legacy code tends to hide all of the things it is doing. \u00a0Refactoring the code first allows you to be able to understand better what responsibilities may be hiding in the code. \u00a0It also allows you to better see the true structure of the logic, which helps to clearly identify the class you want to pull out to put your new logic in.<\/p>\n<p>So, a better set of steps for adding functionality to legacy code is:<\/p>\n<ol>\n<li>Refactor legacy code to be as clean as possible.<\/li>\n<li>Create failing unit test for new functionality I am implementing.<\/li>\n<li>Create new class which only has the logic for that functionality, but that I know overlaps some of the legacy code.<\/li>\n<li>Repeat until my functionality is working.<\/li>\n<li>Integrate the use of my class into the legacy code.<\/li>\n<li>Start moving parts of the legacy code that share the responsibility of my class into my class.<\/li>\n<li>Refactor the remaining legacy code.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>I was reminded yesterday of a very important step I had been forgetting when working with legacy code. \u00a0The first step. \u00a0Refactor. Working with legacy code can be challenging. \u00a0Especially code that was written by someone who didn&#8217;t know what they were doing and then modified 10 times by someone who didn&#8217;t care what they&#8230;<\/p>\n","protected":false},"author":2,"featured_media":561,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[11287,148,31679,54712,5895],"tags":[],"class_list":["post-560","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-agile","category-design","category-process-improvement","category-refactoring","category-self-improvement"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Refactor then Change Legacy Code<\/title>\n<meta name=\"description\" content=\"An explanation of why legacy code should be refactored first when adding functionality to it. Includes a list of seven steps to follow in this process.\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Refactor then Change Legacy Code\" \/>\n<meta property=\"og:description\" content=\"An explanation of why legacy code should be refactored first when adding functionality to it. Includes a list of seven steps to follow in this process.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/\" \/>\n<meta property=\"og:site_name\" content=\"Simple Programmer\" \/>\n<meta property=\"article:published_time\" content=\"2010-03-26T17:08:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-02-23T15:21:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/legacy.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"346\" \/>\n\t<meta property=\"og:image:height\" content=\"494\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"John Sonmez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Sonmez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/refactor-then-change-legacy-code\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/refactor-then-change-legacy-code\\\/\"},\"author\":{\"name\":\"John Sonmez\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"headline\":\"Refactor then Change Legacy Code\",\"datePublished\":\"2010-03-26T17:08:59+00:00\",\"dateModified\":\"2018-02-23T15:21:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/refactor-then-change-legacy-code\\\/\"},\"wordCount\":433,\"commentCount\":6,\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/refactor-then-change-legacy-code\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/03\\\/legacy.jpg\",\"articleSection\":[\"Agile\",\"Design\",\"Process Improvement\",\"Refactoring\",\"Self Improvement\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/refactor-then-change-legacy-code\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/refactor-then-change-legacy-code\\\/\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/refactor-then-change-legacy-code\\\/\",\"name\":\"Refactor then Change Legacy Code\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/refactor-then-change-legacy-code\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/refactor-then-change-legacy-code\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/03\\\/legacy.jpg\",\"datePublished\":\"2010-03-26T17:08:59+00:00\",\"dateModified\":\"2018-02-23T15:21:08+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"description\":\"An explanation of why legacy code should be refactored first when adding functionality to it. Includes a list of seven steps to follow in this process.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/refactor-then-change-legacy-code\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/refactor-then-change-legacy-code\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/refactor-then-change-legacy-code\\\/#primaryimage\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/03\\\/legacy.jpg\",\"contentUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/03\\\/legacy.jpg\",\"width\":346,\"height\":494,\"caption\":\"legacy code\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/refactor-then-change-legacy-code\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/simpleprogrammer.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Refactor then Change Legacy Code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/\",\"name\":\"Simple Programmer\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/simpleprogrammer.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\",\"name\":\"John Sonmez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"caption\":\"John Sonmez\"},\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/author\\\/jsonmez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Refactor then Change Legacy Code","description":"An explanation of why legacy code should be refactored first when adding functionality to it. Includes a list of seven steps to follow in this process.","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"Refactor then Change Legacy Code","og_description":"An explanation of why legacy code should be refactored first when adding functionality to it. Includes a list of seven steps to follow in this process.","og_url":"https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/","og_site_name":"Simple Programmer","article_published_time":"2010-03-26T17:08:59+00:00","article_modified_time":"2018-02-23T15:21:08+00:00","og_image":[{"width":346,"height":494,"url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/legacy.jpg","type":"image\/jpeg"}],"author":"John Sonmez","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Sonmez","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/#article","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/"},"author":{"name":"John Sonmez","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"headline":"Refactor then Change Legacy Code","datePublished":"2010-03-26T17:08:59+00:00","dateModified":"2018-02-23T15:21:08+00:00","mainEntityOfPage":{"@id":"https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/"},"wordCount":433,"commentCount":6,"image":{"@id":"https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/legacy.jpg","articleSection":["Agile","Design","Process Improvement","Refactoring","Self Improvement"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/","url":"https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/","name":"Refactor then Change Legacy Code","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/#primaryimage"},"image":{"@id":"https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/legacy.jpg","datePublished":"2010-03-26T17:08:59+00:00","dateModified":"2018-02-23T15:21:08+00:00","author":{"@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"description":"An explanation of why legacy code should be refactored first when adding functionality to it. Includes a list of seven steps to follow in this process.","breadcrumb":{"@id":"https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/#primaryimage","url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/legacy.jpg","contentUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/legacy.jpg","width":346,"height":494,"caption":"legacy code"},{"@type":"BreadcrumbList","@id":"https:\/\/simpleprogrammer.com\/refactor-then-change-legacy-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simpleprogrammer.com\/"},{"@type":"ListItem","position":2,"name":"Refactor then Change Legacy Code"}]},{"@type":"WebSite","@id":"https:\/\/simpleprogrammer.com\/#website","url":"https:\/\/simpleprogrammer.com\/","name":"Simple Programmer","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/simpleprogrammer.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67","name":"John Sonmez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","caption":"John Sonmez"},"url":"https:\/\/simpleprogrammer.com\/author\/jsonmez\/"}]}},"_links":{"self":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/comments?post=560"}],"version-history":[{"count":0,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/560\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media\/561"}],"wp:attachment":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media?parent=560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/categories?post=560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/tags?post=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}