{"id":196,"date":"2014-03-01T19:23:59","date_gmt":"2014-03-01T19:23:59","guid":{"rendered":"http:\/\/boulderinformationservices.wordpress.com\/?p=196"},"modified":"2014-03-01T19:23:59","modified_gmt":"2014-03-01T19:23:59","slug":"function-dryecho-do-not-repeat-yourself-dry-dry-dry","status":"publish","type":"post","link":"https:\/\/boulderinformationservices.com\/blog\/function-dryecho-do-not-repeat-yourself-dry-dry-dry\/","title":{"rendered":"function dry(){echo &#8220;Do not repeat yourself&#8221;;} dry(); dry(); dry();"},"content":{"rendered":"<h2>Jumpy Thumbnails<\/h2>\n<p>It was a snowy Saturday morning and I didn&#8217;t have a lot of pressing matters, so I decided to track down a silly issue, off the clock, for a client of mine. On two similar pages, the space below a group of thumbnails was different. The thumbnails were &lt;img&gt; tags wrapped in &lt;a&gt;tags, inside a &lt;td&gt;.<\/p>\n<p>I started by using Firebug to see what styles were applied to the various elements on the page. I expected the padding or margin on either the &lt;img&gt; or &lt;a&gt; would be different between the pages.<\/p>\n<p>Firebug showed that there were no differences in the applied styles, even though clearly there were differences on the page. Firebug even &#8220;admitted&#8221; as much by showing one &lt;a&gt; element grayed out, and showing the other one active. In Firebug, a grayed-out element means that means that it is not displaying. But again, the applied styles were identical, and on both pages the &lt;img&gt; tag were displaying. I noticed that page with the grayed-out &lt;a&gt; tag had less space &#8212; in fact, the &lt;a&gt; occupied no space at all, (implying a float? or zero height\/width?) where on the other page, the &lt;a&gt; had dimensions.<\/p>\n<p>Obviously it wasn&#8217;t a style issue since Firebug reported the CSS being identical. I decided to look at the raw HTML source code (which is generated by PHP) and discovered that one image contained the meaningless &#8220;align=&#8217;bottom'&#8221; and the other contained the equally meaningless &#8220;align=&#8217;left'&#8221; &#8212; I say &#8220;meaningless&#8221; because once you use CSS style sheets, they generally control the alignment, and not these HTML attributes.<\/p>\n<p>Nevertheless, I removed them both. Sure enough, things started lining up.<\/p>\n<h2>dry();<\/h2>\n<p>There is actually a larger problem at work here. Because why should there be two, different, meaningless ways of doing the same thing?<\/p>\n<p>The code that I found (but didn&#8217;t write) repeats two (slightly) different versions of the same 60-100 lines of code. What I found was something like this:<\/p>\n<pre>if (logic for top level page)\n{\n    \/\/ 60 lines of code to draw thumbnails\n} else if (logic for sub-level page) {\n    \/\/ 60 lines of code to draw thumbnails\n    \/\/ 40 lines of code to do some other stuff\n}<\/pre>\n<p>This is what is known as a Bad Practice. Once you write 60 lines of code, when you need to do the same thing again later, you don&#8217;t copy and paste those 60 lines. You encapsulate them and re-use them. If you need to modify 5 lines, you separate the 5 lines that are different and you reuse the 55 that are the same.<\/p>\n<p>Because when you don&#8217;t, you end up making a change to one (&#8220;align=&#8217;bottom'&#8221;) that you forget to make to the other (&#8220;align=&#8217;left'&#8221;).<\/p>\n<p>And you end up with a finished product that is buggy and inconsistent and difficult to fix.<\/p>\n<h2>Humbleness Disclaimer<\/h2>\n<p>This sort of programming mistake really frustrates me. I spend hours debugging some stupid flaw that, really, is hardly worth fixing. I mean, 4 pixels of white-space difference should not be worth an hour of my time. (In fact, I didn&#8217;t bill my client for this fix because my professional advice would have been to save their money.)<\/p>\n<p>If the code had made use of a &#8220;drawThumbnails()&#8221; function, this wouldn&#8217;t have been an issue. If the pages are supposed to look the same, they should be made from the same mold. This is why &#8220;DRY&#8221; (Don&#8217;t Repeat Yourself) is an important rule for programmers.<\/p>\n<p>And now that I&#8217;ve finished my rant, I&#8217;d like to acknowledge that somewhere, out there in the wide Internet, is a post just like this one, written by some poor programmer who inherited my awful code from five years ago.<\/p>\n<p>To you, I apologize. I am always learning and always improving. Unfortunately that means that stuff I wrote years ago is guaranteed to be embarrassing today. Maybe I didn&#8217;t get enough time to finish the project well, maybe I was swamped with other work and had to leave well enough alone. Or maybe I was just younger and dumber and there are no excuses. Hopefully you&#8217;ll forgive me rather than curse my name.<\/p>\n<p>Let&#8217;s all agree to write better code, not release anything that isn&#8217;t ready, and not be too hard on the developers who came before us.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Jumpy Thumbnails It was a snowy Saturday morning and I didn&#8217;t have a lot of pressing matters, so I decided to track down a silly issue, off the clock, for a client of mine. On two similar pages, the space &hellip; <a href=\"https:\/\/boulderinformationservices.com\/blog\/function-dryecho-do-not-repeat-yourself-dry-dry-dry\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,16],"tags":[],"_links":{"self":[{"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/posts\/196"}],"collection":[{"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/comments?post=196"}],"version-history":[{"count":0,"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"wp:attachment":[{"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boulderinformationservices.com\/blog\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}