{"id":134,"date":"2010-07-27T23:35:10","date_gmt":"2010-07-28T06:35:10","guid":{"rendered":"http:\/\/journeyman.ivystreetinc.com\/?p=134"},"modified":"2010-07-28T05:53:35","modified_gmt":"2010-07-28T12:53:35","slug":"a-neat-coding-trick-add-a-property-instead-of-a-parameter","status":"publish","type":"post","link":"http:\/\/10kdev.net\/?p=134","title":{"rendered":"A Neat Coding Trick: Add a Property Instead of a Parameter"},"content":{"rendered":"<p>If you&#8217;ve ever had to work on a legacy application with a HUGE code base, you might get faced with this situation:<\/p>\n<p>Manager: &#8220;Hey journeyman can you fix this address on this  page so that it knows to take a system configuration?&#8221;<\/p>\n<p>Journeyman:\u00a0 &#8220;Sure, give me a second for an estimate.&#8221;<\/p>\n<p>You find the code:<\/p>\n<pre><span style=\"color: #000080;\">public class AddressService {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 public String getFormattedAddress(Datasource ds, int yourFormat) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (yourFormat == 1) { \/* do this format *\/ }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else { \/* do this format *\/ }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return addressString;<\/span><\/pre>\n<pre><span style=\"color: #000080;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n }<\/span><\/pre>\n<p>Now go in and track down the code to change .\u00a0\u00a0 Hmm interesting function called getFormattedAddress().\u00a0 Let&#8217;s see, before I touch it do a code search in Eclipse : used in 147 places!!!!!\u00a0 Ugh . . .<\/p>\n<p>You shan&#8217;t dare change the function call in ALL those places, nightmare, just for one of the calls.<\/p>\n<p>So here&#8217;s one way to do avoid this messy situation:<\/p>\n<ol>\n<li>Add a new property, yourConfig, with getters and setters.<\/li>\n<li>Override the function getFormattedAddress so that it takes your new parameter<\/li>\n<li>Set that parameter in the overriding function.<\/li>\n<li>Modify the original function to check the parameter, using a default if the value is empty.<\/li>\n<li>Call the original function form the overriding function<\/li>\n<li>Don&#8217;t forget to reset the default value!<\/li>\n<\/ol>\n<p>Something like this:<\/p>\n<pre><span style=\"color: #000080;\">public class AddressService {\r\n      private String yourConfig = \"\";\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 public String getFormattedAddress(Datasource ds, int yourFormat) {\r\n           String localConfig = \r\n                   (getYourConfig == \"\") ? \"default\" : getYourConfig();\r\n           setYourConfig(\"\");   \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (yourFormat == 1) { \/* do this format +localConfig *\/ }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else { \/* do this format + localConfig *\/ }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return addressString;<\/span><\/pre>\n<pre><span style=\"color: #000080;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span>\r\n<pre style=\"padding-left: 30px;\"><span style=\"color: #000080;\">\u00a0\u00a0\u00a0\u00a0 public String getFormattedAddress(Datasource ds, int yourFormat, String inYourConfig) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 setYourConfig(inYourConfig);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return getFormattedAddress(ds,yourFormat);<\/span><\/pre>\n<pre style=\"padding-left: 30px;\"><span style=\"color: #000080;\">\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n    public String setYourConfig(String inStr) {this.yourConfig = inStr;}<\/span><span style=\"color: #000080;\">\r\n    public void getYourConfig() {return this.yourConfig;}<\/span><\/pre>\n<p><span style=\"color: #000080;\"> }<\/span><br \/>\nNow you can call the function from either its original state, or, pass in a config.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;ve ever had to work on a legacy application with a HUGE code base, you might get faced with this situation: Manager: &#8220;Hey journeyman can you fix this address on this page so that it knows to take a system configuration?&#8221; Journeyman:\u00a0 &#8220;Sure, give me a second for an estimate.&#8221; You find the code: [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/134"}],"collection":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=134"}],"version-history":[{"count":12,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":144,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/134\/revisions\/144"}],"wp:attachment":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}