{"id":5202,"date":"2014-03-30T19:42:14","date_gmt":"2014-03-30T19:42:14","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/using-identical-string-literals-instead-of-a-final-variable-collection-of-common-programming-errors\/"},"modified":"2014-03-30T19:42:14","modified_gmt":"2014-03-30T19:42:14","slug":"using-identical-string-literals-instead-of-a-final-variable-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/using-identical-string-literals-instead-of-a-final-variable-collection-of-common-programming-errors\/","title":{"rendered":"Using identical String literals instead of a final variable?-Collection of common programming errors"},"content":{"rendered":"<p>The advantage is not in performance, but in maintainability and reliability.<\/p>\n<p>Let me take a real example I came across just recently. A programmer created a function that took a String parameter that identified the type of a transaction. Then in the program he did string compares against this type. Like:<\/p>\n<pre><code>if (type.equals(\"stock\"))\n{ ... do whatever ... }\n<\/code><\/pre>\n<p>Then he called this function, passing it the value &#8220;Stock&#8221;.<\/p>\n<p>Do you notice the difference in capitalization? Neither did the original programmer. It proved to be a fairly subtle bug to figure out, because even looking at both listings, the difference in capitalization didn&#8217;t strike me.<\/p>\n<p>If instead he had declared a final static, say<\/p>\n<pre><code>final static String stock=\"stock\";\n<\/code><\/pre>\n<p>Then the first time he tried to pass in &#8220;Stock&#8221; instead of &#8220;stock&#8221;, he would have gotten a compile-time error.<\/p>\n<p>Better still in this example would have been to make an enum, but let&#8217;s assume he actually had to write the string to an output file or something so it had to be a string.<\/p>\n<p>Using final statics gives at least x advantages:<\/p>\n<p>(1) If you mis-spell it, you get a compile-time error, rather than a possibly-subtle run-time error.<\/p>\n<p>(2) A static can assign a meaingful name to a value. Which is more comprehensible:<\/p>\n<pre><code>if (employeeType.equals(\"R\")) ...\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>if (employeeType.equals(EmployeeType.RETIRED)) ...\n<\/code><\/pre>\n<p>(3) When there are multiple related values, you can put a group of final statics together at the top of the program, thus informing future readers what all the possible values are. I&#8217;ve had plenty of times when I&#8217;ve seen a function compare a value against two or three literals. And that leaves me wondering: Are there other possible values, or is this it? (Better still is often to have an enum, but that&#8217;s another story.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The advantage is not in performance, but in maintainability and reliability. Let me take a real example I came across just recently. A programmer created a function that took a String parameter that identified the type of a transaction. Then in the program he did string compares against this type. Like: if (type.equals(&#8220;stock&#8221;)) { &#8230; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5202","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5202"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5202\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}