{"id":257,"date":"2014-09-26T07:07:33","date_gmt":"2014-09-26T07:07:33","guid":{"rendered":"http:\/\/localhost:8000\/?p=257"},"modified":"2025-09-07T13:47:31","modified_gmt":"2025-09-07T13:47:31","slug":"bpgb-unnecessarily-dry","status":"publish","type":"post","link":"https:\/\/blog.gwadej.org\/programmer-musings\/2014\/09\/bpgb-unnecessarily-dry\/","title":{"rendered":"BPGB: Unnecessarily DRY"},"content":{"rendered":"<p>This is the second entry in the <a target=\"_blank\" rel=\"noopener\" href=\"\/programmer_musings\/series_of_posts\/best-practices-gone-bad\/\">Best Practices Gone Bad<\/a> series of posts.<br \/>\nOne principle that a lot of developers have been holding to lately is <abbr title=\"Don't Repeat Yourself\">DRY<\/abbr>. In <cite>The Pragmatic Programmer<\/cite>, Hunt and Thomas state the <abbr title=\"Don't Repeat Yourself\">DRY<\/abbr> principle as:<\/p>\n<blockquote><p>Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.<\/p><\/blockquote>\n<p>This principle was stated as an attempt to push back against a tendency for people to duplicate information and processes either on purpose or accidentally.<\/p>\n<p>I originally ran across this concept several years earlier in P.J. Plauger&#8217;s book <cite>Programming on Purpose: Essays on Software Design<\/cite>. Plauger used the phrase <em>the one, right place<\/em> to describe what Hunt and Thomas call <abbr title=\"Don't Repeat Yourself\">DRY<\/abbr>.<\/p>\n<h2 class=\"subhead\">DRY Gone Bad<\/h2>\n<p>It seems that the point where DRY normally goes bad is when the developer forgets that there are two important points in the <abbr title=\"Don't Repeat Yourself\">DRY<\/abbr> definition.<\/p>\n<ol>\n<li>piece of knowledge<\/li>\n<li>single &#8230; representation<\/li>\n<\/ol>\n<p>Often less experienced developers make <abbr title=\"Don't Repeat Yourself\">DRY<\/abbr> mistakes by focusing on the second point and forgetting the first. These developers become zealots working to remove any piece of duplicated <em>code<\/em> in the program.<\/p>\n<p>This leads to one of the most common misuses of <abbr title=\"Don't Repeat Yourself\">DRY<\/abbr> that I&#8217;ve seen by trying to remove duplication of implementation that is not really duplication of knowledge. For example, if we have the same literal value in two places in the code but they have different meanings, they are not a <em>single piece of knowledge<\/em>. They just happen to have the same value. Likewise, two subroutines that have superficially similar structure, but implement different concepts also should not be combined to make one representation.<\/p>\n<h3 class=\"subsub\">Overly DRY Constants<\/h3>\n<p>Let&#8217;s assume that we have two constants for telling which way to display time: <code>TWELVE_HOUR_TIME<\/code> (12) and <code>TWENTY_FOUR_HOUR_TIME<\/code> (24). It would not make sense to use <code>TWELVE_HOUR_TIME<\/code> when defining an array of month names, despite the fact that the literal <code>12<\/code> is in two places, it is not duplication of a single piece of knowledge.<\/p>\n<p>I&#8217;ve seen people trying to avoid the obvious problem of using <code>TWELVE_HOUR_TIME<\/code> in a place it doesn&#8217;t make sense, by defining a constant <code>TWELVE<\/code> and using it in both places. As a result, we now have a generic name that tells us no more than the literal did and we&#8217;ve still coupled together two concepts that should be separate. In a very important sense, this wouldn&#8217;t reduce duplication of knowledge.<\/p>\n<h3 class=\"subsub\">Overly DRY Code<\/h3>\n<p>In one instance I recall, a somewhat junior programmer decided that two methods weren&#8217;t <abbr title=\"Don't Repeat Yourself\">DRY<\/abbr> enough because they both contained a loop over the same internal array. His solution was to create one method containing the loop and <em>both sets of processing code<\/em>. His method expected a flag passed in to control an <code>if<\/code>&#8211;<code>else<\/code> construct inside the loop to determine which operation to perform.<\/p>\n<p>This increased the overhead for each loop iteration. It also made the overall code much harder to understand. In addition, any call to this method was now completely obtuse because its behavior depended on a flag which couldn&#8217;t really tell the next maintainer anything.<\/p>\n<p>Yes, the new version reduced duplication of implementation. But, the duplication it had reduced was not worth the maintenance nightmare that resulted. More importantly, the supposed duplication was not conceptual. No knowledge duplication was involved. The upshot of this is that if one of the concepts ever needs a different implementation, we will either add more conditional logic, making the subroutine harder to understand, or we&#8217;ll need to break them apart once again.<\/p>\n<p>If the data structure to be traversed had been more complicated than a simple array, I could understand an <code>apply<\/code> or <code>visit<\/code> method that took a code reference to apply to each element in the data structure. This would have actually removed some conceptual duplication for a complicated data structure, but for an array this is overkill.<\/p>\n<h3 class=\"subsub\">Base Class Abomination<\/h3>\n<p>Another example that seems to be common among people new to object oriented programming is the desire to create a base class to promote code reuse. This normally results in a base class with a generic, unhelpful name and multiple unrelated subclasses.<\/p>\n<p>This is may be caused by someone recognizing what the <acronym title=\"Aspect Oriented Programming\">AOP<\/acronym> aficionados call a <em>cross-cutting concern<\/em>. For example, we need to support logging, so every class in the system derives from a <code>Loggable<\/code> class.<\/p>\n<p>This has been solved by languages through the mechanism of Roles.<\/p>\n<h2 class=\"subhead\">Conclusion<\/h2>\n<p>We all know that duplication in code is a bad thing. So, the <abbr title=\"Don't Repeat Yourself\">DRY<\/abbr> principle seems like it should be applied universally. Unfortunately, even <abbr title=\"Don't Repeat Yourself\">DRY<\/abbr> requires some thought when it is applied. The original <abbr title=\"Don't Repeat Yourself\">DRY<\/abbr> principle referred to a <em>piece of knowledge<\/em> as the unit of duplication we want to reduce. Most bad examples of <abbr title=\"Don't Repeat Yourself\">DRY<\/abbr> seem to be caused by ignoring the concepts and focusing on implementation duplication.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the second entry in the Best Practices Gone Bad series of posts. One principle that a lot of developers have been holding to lately is DRY. In The Pragmatic Programmer, Hunt and Thomas state the DRY principle as: Every piece of knowledge must have a single, unambiguous, authoritative representation within a system. This\u2026 <span class=\"read-more\"><a href=\"https:\/\/blog.gwadej.org\/programmer-musings\/2014\/09\/bpgb-unnecessarily-dry\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[88,106,115,117,186,229,279],"_links":{"self":[{"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/posts\/257"}],"collection":[{"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/comments?post=257"}],"version-history":[{"count":1,"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/posts\/257\/revisions"}],"predecessor-version":[{"id":1364,"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/posts\/257\/revisions\/1364"}],"wp:attachment":[{"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/media?parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/categories?post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/tags?post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}