{"id":6713,"date":"2012-10-30T14:57:24","date_gmt":"2012-10-30T19:57:24","guid":{"rendered":"http:\/\/bobbeaty.com\/wp\/?p=6713"},"modified":"2012-10-31T07:07:29","modified_gmt":"2012-10-31T12:07:29","slug":"more-rubyisms-that-are-back-to-haunt-us","status":"publish","type":"post","link":"https:\/\/bobbeaty.com\/wp\/archives\/6713","title":{"rendered":"More Rubyisms that are Back to Haunt Us"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bobbeaty.com\/wp\/wp-content\/uploads\/2012\/09\/CodeMonkey.jpg\" alt=\"Code Monkeys\" title=\"CodeMonkey.jpg\" border=\"0\" width=\"125\" height=\"117\" style=\"float:right; maring-left:10px;\" \/><\/p>\n<p>I really don't like the \"shortcuts\" a lot of people put into code. They don't really make it more readable, and they certainly make it a <em>lot<\/em> more brittle. Case in point, today I realized that the code in a rule was written:<\/p>\n<pre class=\"ruby\" style=\"font-family:monospace;\">  <span style=\"color:#9966CC; font-weight:bold;\">if<\/span> days_since_assignment <span style=\"color:#006600; font-weight:bold;\">&amp;&amp;<\/span> days_since_activity\n    <span style=\"color:#008000; font-style:italic;\"># do something<\/span>\n  <span style=\"color:#9966CC; font-weight:bold;\">end<\/span><\/pre>\n<p>where these variables\/methods are <tt>nil<\/tt> for those times when there is <strong><em>no<\/em><\/strong> respective date. This <em>seems<\/em> OK, but it's really not. In order to do a good, robust, check, we need to see if it's not <tt>nil<\/tt> and <em>then<\/em> check the value or include it in an inequality. There's no other way.<\/p>\n<p>Now your typical Ruby Code Monkey will say \"No! That's not the <em>Ruby Way!<\/em>\" and they'd be right, and the Ruby Way is, in this instance, <em>horrible<\/em> for so many reasons.<\/p>\n<p>I'm a fan of making things comparable wherever possible, and that means <em>get rid of the nils<\/em>. Make it simpler - use <tt>Float::INFINITY<\/tt> for the return value of a \"days since\" method if the date doesn't exist. At least that way, it makes sense in all comparisons, and it's at least <em>no worse<\/em> in some edge cases for testing values. Most often times, it's <em>exactly<\/em> what you want.<\/p>\n<p>So I had to go in and change what <em>appeared<\/em> to be \"working\" code to something that <em>actually worked<\/em> as intended all because of this desire to minimize typing. Had the original author said:<\/p>\n<pre class=\"ruby\" style=\"font-family:monospace;\">  <span style=\"color:#9966CC; font-weight:bold;\">if<\/span> !days_since_assignment.<span style=\"color:#0000FF; font-weight:bold;\">nil<\/span>? <span style=\"color:#006600; font-weight:bold;\">&amp;&amp;<\/span> !days_since_activity.<span style=\"color:#0000FF; font-weight:bold;\">nil<\/span>?\n    <span style=\"color:#008000; font-style:italic;\"># do something<\/span>\n  <span style=\"color:#9966CC; font-weight:bold;\">end<\/span><\/pre>\n<p>then at least it would be clear what was going on. Returning an <tt>INFINITY<\/tt> now makes it clear that we're <strong><em>never<\/em><\/strong> going to fail these checks as a <tt>nil<\/tt> is <strong><em>never<\/em><\/strong> a possible value.<\/p>\n<p>It's all this <em>maturity<\/em> that I see missing: no comments\u2026 minimal typing\u2026 over the top refactoring\u2026 fascination with new and shiny things\u2026 I <em>believe<\/em> these guys could be great, but they limit themselves and they don't have to. They can see what they <em>should<\/em> be doing, and they even <em>know<\/em> they should be doing it. But they <em>choose<\/em> not to.<\/p>\n<p>I wish they didn't make that choice.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I really don&#8217;t like the &#8220;shortcuts&#8221; a lot of people put into code. They don&#8217;t really make it more readable, and they certainly make it a lot more brittle. Case in point, today I realized that the code in a rule was written: if days_since_assignment &amp;&amp; days_since_activity # do something end where these variables\/methods are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[],"class_list":["post-6713","post","type-post","status-publish","format-standard","hentry","category-coding","category-cube-life"],"_links":{"self":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/6713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/comments?post=6713"}],"version-history":[{"count":2,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/6713\/revisions"}],"predecessor-version":[{"id":6715,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/6713\/revisions\/6715"}],"wp:attachment":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/media?parent=6713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/categories?post=6713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/tags?post=6713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}