{"id":1528,"date":"2016-02-22T15:55:18","date_gmt":"2016-02-22T22:55:18","guid":{"rendered":"http:\/\/10kdev.net\/?p=1528"},"modified":"2016-02-23T04:52:54","modified_gmt":"2016-02-23T11:52:54","slug":"java-constants-not-in-the-interface","status":"publish","type":"post","link":"http:\/\/10kdev.net\/?p=1528","title":{"rendered":"Java Constants &#8211; Not In The Interface"},"content":{"rendered":"<div style=\"width: 538px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/02\/02fadb63ad9e9ced414fd6eb963b8606.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/10kdev.net\/wp-content\/uploads\/2016\/02\/02fadb63ad9e9ced414fd6eb963b8606.png\" alt=\"\" width=\"528\" height=\"180\" \/><\/a><p class=\"wp-caption-text\">Business rules? Hopefully centralized to the One; or life will be short and brutal.<\/p><\/div>\n<p>I was working on some code refactoring on a massive codebase when I was faced with the issue of where to put constants for the default values of business rules. \u00a0This issue has been a constant subject of design discussion in java since I can remember, and definitely over a decade for myself. \u00a0Doing &#8220;object&#8221; code, you try your hardest to avoid big &#8220;global&#8221; files. \u00a0But sometimes you have to have a central, global location to put constants. \u00a0In my case, refactoring the same rule spread out over several classes each containing:<\/p>\n<pre>public static final boolean DEFAULT_VALUE = false;<\/pre>\n<p>So you can see the problem here &#8212; if this definition sits in several classes there&#8217;s a chance any change will be missed and you&#8217;ll have breakage. \u00a0There should be one definition to rule them all.<\/p>\n<p>Now one place you *might* be tempted to drop your constants in is an interface. \u00a0I&#8217;ve seen this before, but is decidely in my opinion a bad pattern since interfaces are generally used to define relationships\u00a0to other components and this could be construed the same thing as defining a complete method in an interface. \u00a0It was unfortunate, imo, that this had already been done in the code and it&#8217;s a good policy to match the patterns &#8212; even bad ones &#8212; until you can get around to refactoring them out. \u00a0I decided to refactor. \u00a0And the discussion\u00a0of where to put Java application constants\u00a0has been going on fever &#8212; for instance in this older \u00a0venerable C2 site \u00a0article &#8211; \u00a0<a href=\"http:\/\/c2.com\/cgi\/wiki?InterfacesForDefiningConstants\">interfaces for defining constance &#8211; c2.com<\/a>.<\/p>\n<p>Some other solutions in the recent codebases I&#8217;ve worked on:<\/p>\n<ol>\n<li>property files<\/li>\n<li>enumerations<\/li>\n<li>abstract classes<\/li>\n<li>static classes<\/li>\n<li>depending on the scope &#8211; class level<\/li>\n<\/ol>\n<p>And that is the key \u00a0&#8212; &#8220;what is the scope?&#8221; \u00a0Global\/application\/multi-context scope becomes more challenging in design.<\/p>\n<p>In many of the web applications I&#8217;ve worked on if we&#8217;ve used the constants on the backend, we&#8217;d make enumerations or a static class with the constants. \u00a0Then if you need to expose them, say to tag libraries or the front end, you can import them or make a service to expose them. \u00a0Enumerations lend themselves to this quite well.<\/p>\n<p>The important thing is to get that default centralized if it is a business rule. \u00a0Multiple definitions of a default value, or any value, become problematic. \u00a0I&#8217;ve seen implementations of lists of statuses kept in string constants, enumerations, and a database lookup table all in the same application.<\/p>\n<p>The company paid a lot of money to fix that.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was working on some code refactoring on a massive codebase when I was faced with the issue of where to put constants for the default values of business rules. \u00a0This issue has been a constant subject of design discussion in java since I can remember, and definitely over a decade for myself. \u00a0Doing &#8220;object&#8221; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[82,25,20],"tags":[],"_links":{"self":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1528"}],"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=1528"}],"version-history":[{"count":5,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1528\/revisions"}],"predecessor-version":[{"id":1533,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1528\/revisions\/1533"}],"wp:attachment":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1528"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}