{"id":288,"date":"2007-11-10T22:27:24","date_gmt":"2007-11-11T02:27:24","guid":{"rendered":"http:\/\/michaelnielsen.org\/blog\/?p=288"},"modified":"2007-11-10T22:27:24","modified_gmt":"2007-11-11T02:27:24","slug":"refactoring-checklist","status":"publish","type":"post","link":"https:\/\/michaelnielsen.org\/blog\/refactoring-checklist\/","title":{"rendered":"Refactoring checklist"},"content":{"rendered":"<p>Programming has much in common with other kinds of writing.  In particular, one of the goals of programming, as with writing, is to write in a way that is both beautiful and interesting.  Of course, as with other kinds of writing, one&#8217;s first drafts are typically neither beautiful nor interesting.  Rather, one starts by hacking something out that achieves one&#8217;s ends, without worrying too much about the quality of the code.<\/p>\n<p>For a certain kind of program, this approach is sufficient.  One may write a quick throwaway program to accomplish a one-off task. However, for more ambitious programs, this kind of throwaway approach is no longer feasible; the more ambitious the goals, the higher the quality of the code you must produce.  And the only way to get high quality code is to start with the first draft of your code, and then gradually rewrite &#8211; refactor &#8211; that code, improving its quality.<\/p>\n<p>I&#8217;m a relative novice as a programmer, and I&#8217;m still learning the art of refactoring.  I try to set aside a certain amount of time for improving the quality of my code.  Unfortunately, in my refactoring attempts, I&#8217;m occasionally stymied, simply going blank.  I may look at a section of code and think &#8220;gosh, that&#8217;s terrible&#8221;, but that doesn&#8217;t mean that I instantly have concrete, actionable ways of improving the quality of the code.<\/p>\n<p>To address this problem, I produced the following refactoring checklist as a way of stimulating my thinking when I&#8217;m refactoring. The items on the checklist are very and mechanical to check, and so provide a nice starting point to improve things, and to get more deeply into the code.<\/p>\n<ul>\n<li> Start by picking a <em>single<\/em> method to refactor. Attempting to refactor on a grander scale is too intimidating.\n<li> If a method is more than 40 lines long, look for ways of splitting out pieces of functionality into other methods.\n<li> Are variables, methods, classes and files well named?  This question can often be broken down into two subquestions.  First, is there any way I can improve the readability of my code by changing the names?  Second, is my naming consisten?  So, for example, a file named &#8220;bunny&#8221; should define a class named &#8220;bunny&#8221;, not &#8220;rabbit&#8221;.\n<li> When I see a problem, do something about it, even if I can&#8217;t see a fix right away.  In particular, at the very least add a comment which attempts to describe the problem.  This makes future refactoring easier, and sends a clear message to my subconscious that I mean to eliminate all the bad code from my program. As a bonus, more often than not it stimulates some ideas for how to improve the code.\n<li> Partial solutions are okay.  It&#8217;s fine to replace an ugly hack by a slightly less ugly hack.\n<li> How readable is the code being refactored?  In particular, is it obvious what this section of code does?  If not, how could I make it obvious?\n<li> Explain the code to someone else.  I&#8217;ve never explained a piece of code to someone without having a bunch of ideas for how to improve it.\n<li> Is there a way of abstracting away what is being done, in a way that would enable other uses?\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Programming has much in common with other kinds of writing. In particular, one of the goals of programming, as with writing, is to write in a way that is both beautiful and interesting. Of course, as with other kinds of writing, one&#8217;s first drafts are typically neither beautiful nor interesting. Rather, one starts by hacking&hellip; <a class=\"more-link\" href=\"https:\/\/michaelnielsen.org\/blog\/refactoring-checklist\/\">Continue reading <span class=\"screen-reader-text\">Refactoring checklist<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-288","post","type-post","status-publish","format-standard","hentry","entry"],"_links":{"self":[{"href":"https:\/\/michaelnielsen.org\/blog\/wp-json\/wp\/v2\/posts\/288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michaelnielsen.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaelnielsen.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaelnielsen.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michaelnielsen.org\/blog\/wp-json\/wp\/v2\/comments?post=288"}],"version-history":[{"count":0,"href":"https:\/\/michaelnielsen.org\/blog\/wp-json\/wp\/v2\/posts\/288\/revisions"}],"wp:attachment":[{"href":"https:\/\/michaelnielsen.org\/blog\/wp-json\/wp\/v2\/media?parent=288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaelnielsen.org\/blog\/wp-json\/wp\/v2\/categories?post=288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaelnielsen.org\/blog\/wp-json\/wp\/v2\/tags?post=288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}