{"id":267,"date":"2012-07-31T07:31:01","date_gmt":"2012-07-31T07:31:01","guid":{"rendered":"http:\/\/nicholshayes.co.uk\/blog\/?p=267"},"modified":"2013-10-31T15:56:11","modified_gmt":"2013-10-31T15:56:11","slug":"initialize-keep-it-simple","status":"publish","type":"post","link":"http:\/\/nicholshayes.co.uk\/blog\/?p=267","title":{"rendered":"Initialize: keep it simple"},"content":{"rendered":"<p>The more objects I build, the more I am realising that I need to keep my initialize methods simple. Not doing so, leads to problems later on.<\/p>\n<p>The key is in the name: <strong>best practice is to keep the initialize method to just initially loading instance variables.<\/strong><\/p>\n<h3>An example<\/h3>\n<p>Suppose I want to build a class to translate Spanish text into English.<\/p>\n<h4>The wrong way to do it<\/h4>\n<div class=\"codecolorer-container ruby default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"ruby codecolorer\"><span class=\"kw1\">class<\/span> Translator <span class=\"sy0\">&lt;<\/span> <span class=\"kw3\">String<\/span><br \/>\n&nbsp; <span class=\"kw1\">def<\/span> initialize<span class=\"br0\">&#40;<\/span>text<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">super<\/span><span class=\"br0\">&#40;<\/span>to_english<span class=\"br0\">&#40;<\/span>text<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; <span class=\"kw1\">end<\/span><br \/>\n....... &nbsp;<br \/>\n&nbsp; <span class=\"kw1\">def<\/span> to_english<span class=\"br0\">&#40;<\/span>text<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; EasyTranslate.<span class=\"me1\">translate<\/span><span class=\"br0\">&#40;<\/span>text, <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"re3\">:en<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; <span class=\"kw1\">end<\/span><br \/>\n<span class=\"kw1\">end<\/span><br \/>\n<br \/>\nTranslate.<span class=\"me1\">new<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">'Este es texto'<\/span><span class=\"br0\">&#41;<\/span> &nbsp; &nbsp;<span class=\"sy0\">----&gt;<\/span> &nbsp;<span class=\"st0\">'This is text'<\/span><\/div><\/div>\n<h4>So what is the problem<\/h4>\n<p>This solution works. However, it doesn&#8217;t scale. <\/p>\n<p>What if I want to use Translator to translate Spanish to German, or English to Spanish? My initialize method assumes that I&#8217;ll only be translating to English. As well as adding code to do the new translation code, I&#8217;d also have to modify initialize to work with the new code. <\/p>\n<p>This is not too much of a problem with such a simple class, but what if there were many methods and each of them relied on the initialize method&#8217;s modification of the text as a starting point. I&#8217;d have to modify all those methods too.<\/p>\n<p>Also it is not easy to test any instance method without processing the to_english method. It then becomes difficult to isolate a method test to simply testing the actions of that method.<\/p>\n<h4>A better solution<\/h4>\n<div class=\"codecolorer-container ruby default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"ruby codecolorer\"><span class=\"kw1\">class<\/span> Translator<br \/>\n&nbsp; <span class=\"kw1\">def<\/span> initialize<span class=\"br0\">&#40;<\/span>text<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re1\">@text<\/span> = text<br \/>\n&nbsp; <span class=\"kw1\">end<\/span><br \/>\n..............................<br \/>\n&nbsp; <span class=\"kw1\">def<\/span> to_english<br \/>\n&nbsp; &nbsp; EasyTranslate.<span class=\"me1\">translate<\/span><span class=\"br0\">&#40;<\/span>@text, <span class=\"re3\">:to<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"re3\">:en<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; <span class=\"kw1\">end<\/span><br \/>\n<span class=\"kw1\">end<\/span><\/div><\/div>\n<p>Now if I want to add a method to translate to German, it is just a case of adding the new method.<\/p>\n<p>The downside, is when I use the code, the input is now more verbose:<\/p>\n<div class=\"codecolorer-container ruby default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"ruby codecolorer\">&nbsp;Translate.<span class=\"me1\">new<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">'Este es texto'<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">to_english<\/span> &nbsp; &nbsp;<span class=\"sy0\">----&gt;<\/span> &nbsp;<span class=\"st0\">'This is text'<\/span><\/div><\/div>\n<p>However I can easily fix that with a class method:<\/p>\n<div class=\"codecolorer-container ruby default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"ruby codecolorer\">&nbsp; <span class=\"kw1\">def<\/span> <span class=\"kw2\">self<\/span>.<span class=\"me1\">to_english<\/span><span class=\"br0\">&#40;<\/span>text<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; new<span class=\"br0\">&#40;<\/span>text<span class=\"br0\">&#41;<\/span>.<span class=\"me1\">to_english<\/span><br \/>\n&nbsp; <span class=\"kw1\">end<\/span><\/div><\/div>\n<p>Personally, I might not keep the class and instance methods using the same name, but this demonstrates how easy it is to create more succinct usage:<\/p>\n<div class=\"codecolorer-container ruby default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"ruby codecolorer\">&nbsp;Translate.<span class=\"me1\">to_english<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">'Este es texto'<\/span><span class=\"br0\">&#41;<\/span> &nbsp; &nbsp;<span class=\"sy0\">----&gt;<\/span> &nbsp;<span class=\"st0\">'This is text'<\/span><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The more objects I build, the more I am realising that I need to keep my initialize methods simple. Not doing so, leads to problems later on. The key is in the name: best practice is to keep the initialize &hellip; <a href=\"http:\/\/nicholshayes.co.uk\/blog\/?p=267\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/267"}],"collection":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=267"}],"version-history":[{"count":18,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/267\/revisions"}],"predecessor-version":[{"id":468,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/267\/revisions\/468"}],"wp:attachment":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=267"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}