<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Code Dump &#187; clean code</title>
	<atom:link href="http://www.codelord.net/tag/clean-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codelord.net</link>
	<description>A place a coder rants at...</description>
	<lastBuildDate>Sat, 04 Feb 2012 12:53:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Book Review: Growing Object-Oriented Software</title>
		<link>http://www.codelord.net/2011/01/10/book-review-growing-object-oriented-software/</link>
		<comments>http://www.codelord.net/2011/01/10/book-review-growing-object-oriented-software/#comments</comments>
		<pubDate>Mon, 10 Jan 2011 21:46:42 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[goos]]></category>
		<category><![CDATA[software craftsmanship]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=315</guid>
		<description><![CDATA[Starting with a test means that we have to describe what we want to achieve before we consider how. 2010, for me, was a year with quite a good reading list. It was when I first got to read some really good books such as Clean Code, Agile Software Development, TDD by Example and Apprenticeship [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.amazon.com/gp/product/0321503627?ie=UTF8&amp;tag=thcodu02-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321503627"><img class="alignright" style="border: 0px initial initial;" src="http://codelord.net/wp-content/uploads/2010/12/goos.jpg" border="0" alt="" width="120" height="160" /></a><img style="border: none !important; margin: 0px !important;" src="http://www.assoc-amazon.com/e/ir?t=thcodu02-20&amp;l=as2&amp;o=1&amp;a=0321503627" border="0" alt="" width="1" height="1" /></p>
<blockquote><p>Starting with a test means that we have to describe <em>what</em> we want to achieve before we consider <em>how</em>.</p></blockquote>
<div>
<p>2010, for me, was a year with quite a good reading list. It was when I first got to read some really good books such as Clean Code, Agile Software Development, TDD by Example and  Apprenticeship Patterns. These are all stellar books I highly recommend.</p>
<p>Yes, indeed it was an awesome year and yet I can tell you that the best book I read this year is <a href="http://www.amazon.com/gp/product/0321503627?ie=UTF8&amp;tag=thcodu02-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321503627">Growing Object-Oriented Software, Guided by Tests</a><img style="border: none !important; margin: 0px !important;" src="http://www.assoc-amazon.com/e/ir?t=thcodu02-20&amp;l=as2&amp;o=1&amp;a=0321503627" border="0" alt="" width="1" height="1" /> (GOOS, for short).</p>
<p>I actually never heard of the authors before 2010. As opposed to books by authors such as Kent Beck and Robert Martin which one regularly hears about, I was quite astonished that I kept hearing about this book in different places.</p>
<p>I heard talks mention it, I saw lots of tweets about it and quite a few people that I highly value were praising it. This picked my interest and boy, am I glad I decided to add it to my pile.</p>
<p>I&#8217;ve read a lot about better development, better testing and better everything. And yet, I&#8217;ve never come across a book as thourough and as comprehensive as GOOS. If you read <a href="http://www.codelord.net/2010/06/15/book-review-clean-code/">my</a> <a href="http://www.codelord.net/2010/05/02/agile-software-development-you-will-never-code-the-same-again/">other</a> <a href="http://www.codelord.net/2010/01/12/every-coder-should-read-tdd-by-example/">reviews</a> you will see that what usually buys me over are good code walk-throughs. Now let me tell you, you haven&#8217;t seen a good walk-through until you&#8217;ve seen GOOS.</p>
<blockquote><p>Code isn&#8217;t sacred just because it exists, and the second time won&#8217;t take as long.</p></blockquote>
<p>On the one hand, the book is loaded with practical tips for making your tests better, faster, more readable and maintainable. It covers the nuances of testing ORM systems, GUIs, multi-threading problems and more.</p>
<p>On the other hand, every page turn is greeted with more nuggets of OOP lore. Actually, seeing all this wisdom clustered so tightly by people that have been struggling with these problems for over a decade now seems illegal to me. Are we really allowed to learn so many secrets of the profession this fast? Surely some sort of blood sacrifice has to be made?</p>
<blockquote><p>Once we start a major rework we can&#8217;t stop until finished. There&#8217;s a reason surgeons prefer keyhole surgery to opening up a patient.</p></blockquote>
<p>I&#8217;ve read GOOS over the course of a few months, consuming chapters little by little and letting the knowledge sink in. I was amazed at how much this affected my way of thinking about OOP and TDD, pretty much right off the covers. I already <a href="http://www.codelord.net/2010/12/18/adding-goos-sauce-to-gwt-mvp/">blogged</a> about how my new OOP-Spidey-Sense helped us improve our architecture.</p>
<p>I&#8217;ll finish with saying this book is a game-changer for me, even though I&#8217;ve been doing TDD for a few years now. To the authors, Nat and Steve, I take my hat off. They have earned a place of honor in my <a href="http://twitter.com/#!/avivby/deserve-a-beer">Deserve-A-Beer list</a>.</p>
<p>And to sum up all these great quotes from GOOS, here&#8217;s another gem:</p>
<blockquote><p>The last thing we should have to do is crack open the debugger and step through the tested code to find the point of disagreement.</p></blockquote>
<p>You should subscribe to my <a href="http://feeds.feedburner.com/TheCodeDump">feed</a> and follow me on <a href="http://twitter.com/avivby">twitter</a>!</p>
</div>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codelord.net%2F2011%2F01%2F10%2Fbook-review-growing-object-oriented-software%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2011%2F01%2F10%2Fbook-review-growing-object-oriented-software%2F&amp;source=avivby&amp;style=normal&amp;service=bit.ly&amp;service_api=R_6107bfac7c92ddff62b393d1e8b7abbe&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codelord.net/2011/01/10/book-review-growing-object-oriented-software/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding GOOS Sauce to GWT MVP</title>
		<link>http://www.codelord.net/2010/12/18/adding-goos-sauce-to-gwt-mvp/</link>
		<comments>http://www.codelord.net/2010/12/18/adding-goos-sauce-to-gwt-mvp/#comments</comments>
		<pubDate>Sat, 18 Dec 2010 16:06:29 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[goos]]></category>
		<category><![CDATA[software craftsmanship]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=289</guid>
		<description><![CDATA[For a few months now I&#8217;ve been using Google Web Toolkit. One thing that was bothering me was that even when following the praised MVP (Model-View-Presenter) pattern as per the documentation, you pretty quickly get into messy land. Here&#8217;s a snippet from the official GWT MVP tutorial: In this example, you see that our Presenter, [...]]]></description>
			<content:encoded><![CDATA[<p>For a few months now I&#8217;ve been using <a href="http://code.google.com/webtoolkit/">Google Web Toolkit</a>. One thing that was bothering me was that even when following the praised MVP (Model-View-Presenter) pattern as per the documentation, you pretty quickly get into messy land.</p>
<p>Here&#8217;s a snippet from the official GWT MVP <a href="http://code.google.com/webtoolkit/articles/mvp-architecture.html">tutorial</a>:</p>
<p><script src="https://gist.github.com/746606.js?file=gistfile1.java"></script> In this example, you see that our Presenter, when bound, registers a click handler for a button, in order to perform some action when it is called. This might seem nice and all, but there&#8217;s a smell. This is a violation of the <a href="http://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a> (the missing SOLID rule, one might say).  This simply makes it harder to test, since we now have to add another layer of indirection between the SUT and its collaborators. Instead of making the view a tiny bit smarter, we use it as a dumb collection of widgets the presenter manages. This is clearly not in &#8220;Tell, don&#8217;t ask&#8221; form.</p>
<p>The thing that really bothers me is how coupled the presenter gets with its view. Take the above example, and say that you decided that it would be better to have two &#8220;save&#8221; buttons on the UI. Does the presenter really care? Should it even change? And what if you actually want the save button to change to a remove button when the user picked something? Should the presenter now deal with getSaveOrRemoveButton() ? Of course not.</p>
<h3>GOOS it up</h3>
<p>After beating around this bush for quite some time, I decided to try and find a better way. I&#8217;m currently reading the brilliant Growing Object Oriented Software book, and decided to try its approach to push a better implementation. After a bit of refactoring I got this:<br />
 <script src="https://gist.github.com/746606.js?file=gistfile2.java"></script></p>
<p><a href="http://www.amazon.com/gp/product/0321503627?ie=UTF8&amp;tag=thcodu02-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321503627"><img class="alignright" style="border: 0px initial initial;" src="http://codelord.net/wp-content/uploads/2010/12/goos.jpg" border="0" alt="" width="120" height="160" /></a><img style="border: none !important; margin: 0px !important;" src="http://www.assoc-amazon.com/e/ir?t=thcodu02-20&amp;l=as2&amp;o=1&amp;a=0321503627" border="0" alt="" width="1" height="1" /></p>
<p>This might seem like a tiny change. And it is. But it makes all the difference in the world in how more responsive your design gets, especially in our world where the view is most likely to change a dozen times before settling on something. Once there are enough of these, I push the presenter as a dependency into the view, and let it call the presenter directly. The funny thing is this style is actually implicitly mentioned in the <a href="http://code.google.com/webtoolkit/articles/mvp-architecture-2.html">second part</a> of the GWT MVP tutorial. Just some GOOSing helped us get to a better, more malleable design!</p>
<p>Don&#8217;t be afraid to do something differently than the documentation, especially if you gave it a fair shot and it didn&#8217;t work out.</p>
<p>You should subscribe to my <a href="http://feeds.feedburner.com/TheCodeDump">feed</a> and follow me on <a href="http://twitter.com/avivby">twitter</a>!
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F12%2F18%2Fadding-goos-sauce-to-gwt-mvp%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F12%2F18%2Fadding-goos-sauce-to-gwt-mvp%2F&amp;source=avivby&amp;style=normal&amp;service=bit.ly&amp;service_api=R_6107bfac7c92ddff62b393d1e8b7abbe&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codelord.net/2010/12/18/adding-goos-sauce-to-gwt-mvp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Notes from the 5th Israeli Software Craftsmanship Meeting</title>
		<link>http://www.codelord.net/2010/12/17/notes-from-the-5th-israeli-software-craftsmanship-meeting/</link>
		<comments>http://www.codelord.net/2010/12/17/notes-from-the-5th-israeli-software-craftsmanship-meeting/#comments</comments>
		<pubDate>Fri, 17 Dec 2010 09:28:07 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[DRY]]></category>
		<category><![CDATA[SCIL]]></category>
		<category><![CDATA[software craftsmanship]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=293</guid>
		<description><![CDATA[This week I had the pleasure to attend to the 5th meeting of our Software Craftsmanship group, and boy what a meeting it was. For the first time we&#8217;ve tried a different format that is 100% hands-on and reduced someone-talking-with-slides time to a minimum. The meeting was composed of 3 tables: a code review table [...]]]></description>
			<content:encoded><![CDATA[<p>This week I had the pleasure to attend to the 5th meeting of our Software Craftsmanship group, and boy what a meeting it was. For the first time we&#8217;ve tried a different format that is 100% hands-on and reduced someone-talking-with-slides time to a minimum.</p>
<p>The meeting was composed of 3 tables: a code review table where people actually brought code from home/work and discussed about with others, a TDD table where a veteran TDDer talked people through a Kata and a third table, led by yours truly, that aimed to introduce people to the concept of DRY and tackling some duplications problems.</p>
<p>Unfortunately for me, this means I couldn&#8217;t take part in the other tables, but from what I could pick up I must say it was really fun getting so much positive feedback for a meeting.</p>
<p>The DRY table consisted of a Kata I&#8217;ve composed specifically to raise issues of duplication already a few minutes into it, which you can find <a href="http://www.codelord.net/2010/12/11/serializer-kata-practicing-dry/">here</a>. It was amazing seeing people keep working on the Kata after the 1-hour dedicated to it was up, and I never expected to see such variety (Java, C#, Python, Ruby, JavaScript and PHP were all spotted)!</p>
<p>The slides from my (extremely) short introduction to DRY are available here:</p>
<p><a title="View DRY - Don't Repeat Yourself on Scribd" href="http://www.scribd.com/doc/45494123/DRY-Don-t-Repeat-Yourself" style="margin: 12px auto 6px auto; font-family: Helvetica,Arial,Sans-serif; font-style: normal; font-variant: normal; font-weight: normal; font-size: 14px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; display: block; text-decoration: underline;">DRY &#8211; Don&#8217;t Repeat Yourself</a> <object id="doc_432190570022869" name="doc_432190570022869" height="600" width="100%" type="application/x-shockwave-flash" data="http://d1.scribdassets.com/ScribdViewer.swf" style="outline:none;" ><param name="movie" value="http://d1.scribdassets.com/ScribdViewer.swf"><param name="wmode" value="opaque"><param name="bgcolor" value="#ffffff"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><param name="FlashVars" value="document_id=45494123&#038;access_key=key-7fmytzkslurjwnw95er&#038;page=1&#038;viewMode=slideshow"><embed id="doc_432190570022869" name="doc_432190570022869" src="http://d1.scribdassets.com/ScribdViewer.swf?document_id=45494123&#038;access_key=key-7fmytzkslurjwnw95er&#038;page=1&#038;viewMode=slideshow" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" height="600" width="100%" wmode="opaque" bgcolor="#ffffff"></embed></object>	</p>
<p>You should subscribe to <a href="http://feeds.feedburner.com/TheCodeDump">my</a> feed and follow me on <a href="http://twitter.com/avivby">twitter</a>!
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F12%2F17%2Fnotes-from-the-5th-israeli-software-craftsmanship-meeting%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F12%2F17%2Fnotes-from-the-5th-israeli-software-craftsmanship-meeting%2F&amp;source=avivby&amp;style=normal&amp;service=bit.ly&amp;service_api=R_6107bfac7c92ddff62b393d1e8b7abbe&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codelord.net/2010/12/17/notes-from-the-5th-israeli-software-craftsmanship-meeting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Serializer Kata: Practicing DRY</title>
		<link>http://www.codelord.net/2010/12/11/serializer-kata-practicing-dry/</link>
		<comments>http://www.codelord.net/2010/12/11/serializer-kata-practicing-dry/#comments</comments>
		<pubDate>Sat, 11 Dec 2010 17:46:14 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[SCIL]]></category>
		<category><![CDATA[software craftsmanship]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=279</guid>
		<description><![CDATA[This kata is intended to help one practice the DRY principle (Don&#8217;t Repeat Yourself). You can read more about DRY here. A few notes: After completing a step in the Kata and before moving on to the next, take the time to make sure your code&#8217;s duplication ≤ 0 For the sake of focus, you [...]]]></description>
			<content:encoded><![CDATA[<p>This kata is intended to help one practice the DRY principle (Don&#8217;t Repeat Yourself). You can read more about DRY <a href="http://www.codelord.net/tag/dry/">here</a>.</p>
<p>A few notes:</p>
<ul>
<li>After completing a step in the Kata and before moving on to the next, take the time to make sure your code&#8217;s duplication ≤ 0</li>
<li>For the sake of focus, you may ignore matters of character escaping, encoding, error handling, object graph cycles and the likes</li>
<li>Our focus is on <strong>reducing duplication</strong>, it is <strong>not finishing</strong> the kata</li>
</ul>
<p>In this Kata, our goal is to implement 2 simple object serializers. One serializer is to <a href="http://en.wikipedia.org/wiki/XML">XML</a>, the other is to <a href="http://en.wikipedia.org/wiki/JSON">JSON</a>.</p>
<ol>
<li>Support serializing  a class without any members
<ol>
<li>To XML: EmptyClass -&gt; &lt;EmptyClass&gt;&lt;/EmptyClass&gt;</li>
</ol>
<ol>
<li>To JSON: EmptyClass -&gt; {}</li>
</ol>
</li>
<li>Add support for serializing a class&#8217; integer members
<ol>
<li>To XML: IntClass(a=1, b=2) -&gt; &lt;IntClass&gt;&lt;a&gt;1&lt;/a&gt;&lt;b&gt;2&lt;/b&gt;&lt;/IntClass&gt;</li>
<li>To JSON: IntClass(a=1, b=2) -&gt; { &#8220;a&#8221;: 1, &#8220;b&#8221;: 2 }</li>
</ol>
</li>
<li>Add support for serializing a class&#8217; string members
<ol>
<li>To XML: StrClass(a=&#8221;first&#8221;, b=&#8221;second&#8221;) -&gt; &lt;StrClass&gt;&lt;a&gt;first&lt;/a&gt;&lt;b&gt;second&lt;/b&gt;&lt;/StrClass&gt;</li>
<li>To JSON: StrClass(a=&#8221;first&#8221;, b=&#8221;second&#8221;) -&gt; { &#8220;a&#8221;: &#8220;first&#8221;, &#8220;b&#8221;: &#8220;second&#8221; }</li>
</ol>
</li>
<li>Add support for serializing a class&#8217; other class members
<ol>
<li>To XML: CompositeClass(inner=(a=1)) -&gt; &lt;CompositeClass&gt;&lt;inner&gt;&lt;a&gt;1&lt;/a&gt;&lt;/inner&gt;&lt;/CompositeClass&gt;</li>
<li>To JSON: CompositeClass(inner=(a=1)) -&gt; { &#8220;inner&#8221;: { &#8220;a&#8221;: 1 } }</li>
</ol>
</li>
</ol>
<p>If you found this interesting subscribe to my <a href="http://feeds.feedburner.com/TheCodeDump">feed</a> and follow me on <a href="http://twitter.com/avivby">twitter</a>!
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F12%2F11%2Fserializer-kata-practicing-dry%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F12%2F11%2Fserializer-kata-practicing-dry%2F&amp;source=avivby&amp;style=normal&amp;service=bit.ly&amp;service_api=R_6107bfac7c92ddff62b393d1e8b7abbe&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codelord.net/2010/12/11/serializer-kata-practicing-dry/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Say No to Null Checks</title>
		<link>http://www.codelord.net/2010/11/14/say-no-to-null-checks/</link>
		<comments>http://www.codelord.net/2010/11/14/say-no-to-null-checks/#comments</comments>
		<pubDate>Sun, 14 Nov 2010 20:23:01 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[software craftsmanship]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=267</guid>
		<description><![CDATA[Hey, do you check your methods&#8217; arguments to make sure they&#8217;re not null? Today, I got into a little discussion with a teammate about testing contracts of methods: should we check for null in every public method? I was against it, and he was for it. The simple reasons to do it are, first, that [...]]]></description>
			<content:encoded><![CDATA[<p>Hey, do you check your methods&#8217; arguments to make sure they&#8217;re not null?</p>
<div id="_mcePaste">
<p>Today, I got into a little discussion with a teammate about testing contracts of methods: should we check for null in every public method?</p>
<p>I was against it, and he was for it.</p>
<p>The simple reasons to do it are, first, that it makes your code more defensive. You fail explicitly instead of failing implicitly when the code tries to dereference the null object. Another argument was that given 20 callers of an interface, it&#8217;s easier to test in the interface for the precondition than to test each and every one of the callers. And, of course, that it is better API implementation, and that even if a class isn&#8217;t part of one&#8217;s public API now it might very well become part of it in the future, so why not add the tests now?</p>
<p>I&#8217;ll tackle these all. First, I have to agree that some null checks are required, at the boundaries of your system. I believe a system should have a paranoid barrier, before it everything is as suspect as someone going on a bus with a heavy coat on a hot day &#8211; that&#8217;s just waiting to blow. Once you&#8217;ve passed the barrier you know things are secure and no longer need to be paranoid.</p>
<p>So yes, some null checks are of course required.</p>
<p>But, because we want our API to be user-friendly and error-proof does that mean we need to make every public method in our code paranoid just in case it will become part of the public API at some point? 5 letters: YAGNI! <img src='http://www.codelord.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The interesting part is the testing of the callers. I agree, if we have to write the test 20 times for each caller, it will get tedious. But we don&#8217;t write the same thing twice, do we? As good old J.B Rainsberger <a href="http://blog.thecodewhisperer.com/post/207374113/who-tests-the-contract-tests"><span style="color: #000000;">teaches</span></a>, what we actually need are collaboration tests. Each of the callers collaborate with the interface. And so, we create a collaboration test that makes sure the user is using the interface according to the contract. These are usually abstract tests that require us to create derivatives that implement a factory method for creating the calling class. This way we write the tests only once and make explicit the interface and contract, even in dynamic language.</p>
<p>In general, this is a powerful solution, that solves a basic problem with defensive programming. Say we do test for nullity wherever possible, what do we do then? Our system is likely to crash or throw an exception any way, since what is the interface to do? Obviously something is wrong if we were called in a way that doesn&#8217;t match the contract, so is the hassle worth it? I think testing for nullity everywhere is a thing of the past, especially once you adopt dynamic programming and get used to the fact that most of the times you can&#8217;t even be sure the object you&#8217;ve got will answer the methods you&#8217;re about to use, so what difference does a null check make?</p>
<p>So let&#8217;s write some awesome collaboration tests tests and get cracking!</p>
<p>You should subscribe to my <a href="http://feeds.feedburner.com/TheCodeDump"><span style="color: #000000;">feed</span></a> and follow me on <a href="http://twitter.com/avivby"><span style="color: #000000;">twitter</span></a>!</p>
</div>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F11%2F14%2Fsay-no-to-null-checks%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F11%2F14%2Fsay-no-to-null-checks%2F&amp;source=avivby&amp;style=normal&amp;service=bit.ly&amp;service_api=R_6107bfac7c92ddff62b393d1e8b7abbe&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codelord.net/2010/11/14/say-no-to-null-checks/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Letting DRY Drive</title>
		<link>http://www.codelord.net/2010/11/09/letting-dry-drive/</link>
		<comments>http://www.codelord.net/2010/11/09/letting-dry-drive/#comments</comments>
		<pubDate>Tue, 09 Nov 2010 05:19:20 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[pragprowrimo2010]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[DRY]]></category>
		<category><![CDATA[software craftsmanship]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=265</guid>
		<description><![CDATA[The reason I stress DRY so much is because it is one of the simplest yet most effective pieces of knowledge we have gained for software development. When I first read about DRY (in The Pragmatic Programmer) I got it&#8217;s amazing strength in saving extra maintenance by making sure that changes will be localized as [...]]]></description>
			<content:encoded><![CDATA[<p>The reason I <a href="http://www.codelord.net/2010/11/02/short-intro-to-dry/">stress</a> <a href="http://www.codelord.net/2010/11/03/taking-dry-further/">DRY</a> <a href="http://www.codelord.net/2010/11/04/dry-dont-get-trigger-happy/">so much</a> is because it is one of the simplest yet most effective pieces of knowledge we have gained for software development. When I first read about DRY (in <a href="http://www.amazon.com/gp/product/020161622X?ie=UTF8&amp;tag=thcodu02-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=020161622X">The Pragmatic Programmer</a><img style="border: none !important; margin: 0px !important;" src="http://www.assoc-amazon.com/e/ir?t=thcodu02-20&amp;l=as2&amp;o=1&amp;a=020161622X" border="0" alt="" width="1" height="1" />) I got it&#8217;s amazing strength in saving extra maintenance by making sure that changes will be localized as much as possible in the future. And I&#8217;ve used it as such for a couple years.</p>
<p>Only after I started practicing TDD did I realize how significantly DRY can drive coding. Whenever I test drive code, the third step of the cycle, Refactor, is usually a lot about removing duplication. And the amazing part is that this alone drives a useful implementation. In the awesome book <a href="http://www.amazon.com/gp/product/0321146530?ie=UTF8&amp;tag=thcodu02-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321146530">TDD By Example</a><img style="border: none !important; margin: 0px !important;" src="http://www.assoc-amazon.com/e/ir?t=thcodu02-20&amp;l=as2&amp;o=1&amp;a=0321146530" border="0" alt="" width="1" height="1" />, Beck describes a method of implementing called Triangulation. It&#8217;s for those rare occassions where you really have no real clue what way is best to solve the problem &#8211; the ground that allows TDD to flourish. In this method, you start by getting a couple of tests to pass with dumb implementations and then refactor to generalize the code and make it DRY. The mere act of DRYing your code can simply through a solution at you. I&#8217;m still amazed when this happens to me, both because of the sheer beauty of the process, and because it means I&#8217;m doing something right.</p>
<p>About 5 years ago, when first heard about the concept of TDD, I wrote a greenfield project for about 5 months in what I believed was TDD. Only more than a year later did I realize that I wasn&#8217;t doing TDD at all, but TFD (Test First Development). I did in fact write all my tests before I wrote production code, but I didn&#8217;t let the tests <em>drive</em> the development. Now although that is better than not testing at all or TAD, it just wasn&#8217;t it, and I didn&#8217;t get why.</p>
<p>Whenever I wrote a test, I already knew how the implementation was going to look, be it simple or complex. Sometimes I might have alreayd decided that I was going to add a decorator there, and then wrote the test that forced me to implement a decorator. This, of course, is wrong. When I learned to let go and stop micro-managing my code, things took off at a better direction.</p>
<p>I write a simple test that explicitly uses the code as I would like to use it had someone else already imlpemented it. This is crucial, as it makes sure that I am not allowing myself to sustain a lousy interface simply because it&#8217;s the easier one to implement. The harder part is to always disconnect yourself from prejudice &#8211; what you might think the code should become. Currently this is a deliberate effort for me, to make sure I&#8217;m not thinking a few steps further and adding what I think I&#8217;ll need then too early. Even adding code I believe I&#8217;ll just have to add anyway in about 2 tests is wrong.</p>
<p>After making the test pass by the simplest way I could come up with I start DRYing the code up. I actually do more stuff, such as improving naming etc. but generally, DRY is the main force behind the changes in this phase for me. I listen to the code and try to make it. This is where knowing your refactorings well pays off. I might extract a method only to inline it 20 seconds later and extract a different part of it. I move things around a bit and see what feels right. Because I&#8217;ve already got the tests, there&#8217;s no reason not to use them right away to see just how far I can stretch the code to my ideal vision.</p>
<p>For me the magic is when I notice, after 30 minutes of doing this, that I&#8217;ve suddenly got a solution without realizing it. Suddenly the code falls into place. A lot of the times it turns out simpler than what I thought I&#8217;d turn out with.</p>
<p>Jumping from basic DRY to full blown TDD isn&#8217;t easy or straight forward, but let DRY drive you design for a while and see it grow on you. I&#8217;ve yet to meet someone that&#8217;s given it a shot and disagreed this a whole new way to programming.</p>
<div id="_mcePaste">You should subscribe to my <a href="http://feeds.feedburner.com/TheCodeDump">feed</a> and follow me on <a href="http://twitter.com/avivby">twitter</a></div>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F11%2F09%2Fletting-dry-drive%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F11%2F09%2Fletting-dry-drive%2F&amp;source=avivby&amp;style=normal&amp;service=bit.ly&amp;service_api=R_6107bfac7c92ddff62b393d1e8b7abbe&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codelord.net/2010/11/09/letting-dry-drive/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Refactoring to Heaven</title>
		<link>http://www.codelord.net/2010/11/07/refactoring-to-heaven/</link>
		<comments>http://www.codelord.net/2010/11/07/refactoring-to-heaven/#comments</comments>
		<pubDate>Sun, 07 Nov 2010 05:52:56 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[pragprowrimo2010]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[software craftsmanship]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=254</guid>
		<description><![CDATA[My last blog was about how many people refer to refactoring as the wrong thing. For completeness of discussion I&#8217;d like to talk about what a lot of people seemed to not get in my previous post. I said that the &#8216;Big Refactoring&#8217; is the new &#8216;Big Redesign in the Sky&#8217;, and got a bunch [...]]]></description>
			<content:encoded><![CDATA[<p>My <a href="http://www.codelord.net/2010/11/06/refactoring-youre-doing-it-wrong/">last blog</a> was about how many people refer to refactoring as the wrong thing. For completeness of discussion I&#8217;d like to talk about what a lot of people seemed to not get in my previous post. I said that the &#8216;Big Refactoring&#8217; is the new &#8216;Big Redesign in the Sky&#8217;, and got a bunch of questions along the linse of &#8220;so we&#8217;re never supposed to redesign?&#8221;</p>
<p>Well, of course that&#8217;s not the case. Even the best programmers occasionally dig themselves into a hole that will require some effort to get out of. The point is that the best thing to do is never to take those 2 days to 2 weeks period just for doing that refactoring. That is never the right approach to solve such a problem.</p>
<p><a href="http://codelord.net/wp-content/uploads/2010/11/StairwayToHeaven.jpg"><img class="aligncenter size-medium wp-image-262" title="StairwayToHeaven" src="http://codelord.net/wp-content/uploads/2010/11/StairwayToHeaven-300x225.jpg" alt="" width="300" height="225" /></a></p>
<p>First of all, I&#8217;ve seen many times that it was intended to spend &#8220;only&#8221; 2 days (which is already a huge amount of time) to do some refactoring, but 2 days in, the dev team found it they actually need 2 more days because of extra complications they&#8217;ve got, and the team&#8217;s lucky if after a week of hard work they still have a working system, not to mention better.</p>
<p>Another problem with that is, once the devs think they have a picture in mind of this perfect redesign that management won&#8217;t let them take a week to get to (with good reason!), a lot developers stop thinking about how to make the current code better. It&#8217;s just let&#8217;s add this hack here, because management won&#8217;t let us do this the right way. That&#8217;s BS. Will you come to your dentist 3 days in a row for &#8216;general groundwork&#8217; before have a 2 hour filling? I thought so.</p>
<p>Programming is about adding value. Don&#8217;t get me wrong, I like writing elegant and clean code as much as the next guy (actually, probably a bit more given I walk around with one of <a href="http://butunclebob.com/ArticleS.UncleBob.GreenWristBand"><span style="color: #000000;">these</span></a>). But, what we&#8217;re hired to do is solve problems and help someone&#8217;s life by doing it. If all we do is bitch about how the design could be better, we&#8217;re only benefiting ourselves, and not doing a good job at it.</p>
<p>So what is the right way? Well, the right way is to pull things off towards the right direction, in steady and controlled baby steps. I&#8217;ve seen this work, and I&#8217;ve heard from much better programmers than myself that they&#8217;ve got it working multiple times. After spending a day in an awesome <a href="http://www.threeriversinstitute.org/blog/?page_id=379"><span style="color: #000000;">Responsive Design</span></a> workshop by Kent Beck, I was a bit amazed to hear there are so many ways of pushing design towards a better place in baby steps. Up until that point I would have done it anyway, but Kent taught me there&#8217;s actually an art to it.</p>
<p>Do you think it&#8217;s impossible in your case because &#8220;X is too big&#8221;? Well, say you&#8217;ve got N steps in your redesign, all are safe atomic changes. Many of the times you might think the order of steps you&#8217;ve came up with simply won&#8217;t do and it will be too hard to do it without the big blunder. But, think about it this way: Those N steps have N! (N-factorial) different ways to be ordered. What are the odds that none of those ways will solve your problem? I&#8217;ve yet to see when this is not the case (and this, among others, is why you should spend the time with better and more experienced coders, such as Kent).</p>
<p>Yes, it means that &#8220;2 day refactoring&#8221; of yours will probably be spread over 2 weeks (which is just might have done anyway). But it also means that no matter what happens during those 2 weeks, you&#8217;re always on the safe side. Find out after 2 days that your idea simply won&#8217;t cut it? Well, no biggie. The code still works, and you added features those 2 days. Find out it&#8217;ll take longer? Yeah, so what. We&#8217;re doing a few small steps a day anyway. The awesome thing is that you&#8217;ve got time to learn during the work what you may have thought up wrong and use that as input for the next parts of the refactoring. No rush here. And I think the most important point is that if you simply spend 30 minutes a day to refactor things at the right direction, weaved into your daily work and refactorings, you&#8217;ll get it done without have to pitch your Big Redesign to management.</p>
<p>This allows you to simply do what you&#8217;re supposed to do: add value to customers daily, while making sure the system stays responsive to future changes. And that, my friends, is how the big refactoring should be done.</p>
<p>You should subscribe to my <a href="http://feeds.feedburner.com/TheCodeDump">feed</a> and follow me on <a href="http://twitter.com/avivby">twitter</a>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F11%2F07%2Frefactoring-to-heaven%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F11%2F07%2Frefactoring-to-heaven%2F&amp;source=avivby&amp;style=normal&amp;service=bit.ly&amp;service_api=R_6107bfac7c92ddff62b393d1e8b7abbe&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codelord.net/2010/11/07/refactoring-to-heaven/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Refactoring: You&#8217;re Doing it Wrong</title>
		<link>http://www.codelord.net/2010/11/06/refactoring-youre-doing-it-wrong/</link>
		<comments>http://www.codelord.net/2010/11/06/refactoring-youre-doing-it-wrong/#comments</comments>
		<pubDate>Sat, 06 Nov 2010 10:09:18 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[pragprowrimo2010]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[software craftsmanship]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=248</guid>
		<description><![CDATA[I&#8217;ve been writing quite a bit about DRY lately, but I think it&#8217;s time to take a few steps back and talk about something even more fundamental, refactoring. Today, refactoring as a concept is pretty common. I think it&#8217;s even passed the buzzword stage. It&#8217;s still not too known in academia (I&#8217;ve yet to hear [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been writing <a href="http://www.codelord.net/2010/11/02/short-intro-to-dry/">quite</a> <a href="http://www.codelord.net/2010/11/03/taking-dry-further/">a</a> <a href="http://www.codelord.net/2010/11/04/dry-dont-get-trigger-happy/">bit</a> about DRY lately, but I think it&#8217;s time to take a few steps back and talk about something even more fundamental, refactoring. Today, refactoring as a concept is pretty common. I think it&#8217;s even passed the buzzword stage. It&#8217;s still not too known in academia (I&#8217;ve yet to hear the term mentioned in my studies), but given that most IDEs come with refactoring support or have supplementary tools for it, people are familiar with the term.</p>
<p>It has been written a lot about that the term refactoring is being heavily misused lately. Have you ever said &#8220;we need to do a big refactoring here?&#8221; If so, you&#8217;ve done the term wrong. The Big Refactoring is the new Big Redesign in the Sky. Have you ever told your boss you need time to refactor something? You&#8217;re doing it wrong. Wrote Refactoring as a task on the board? Wrong once more.</p>
<p>Refactoring should be engrained in our constant flow of work. I mean it should be assimilated into the way you think of basic coding. Stop thinking of code changes as a stream of characters. To be an efficient coder you need to think of code at a higher level. The level of refactorings should be the level we think of code.</p>
<p>I really believe that generally <a href="http://www.sbastn.com/typing-is-not-the-bottleneck">typing is not the bottleneck</a>. In spite of that, I believe that being able to perform refactorings quickly, efficiently and seamlessly as part of your flow is a major differentiator in one&#8217;s ability to quickly check ideas and be able to push design forward. I&#8217;ve deliberately spent time to put all refactorings of IDEs I use into my muscle memory. Extracting a method is not some heavy mind process, it&#8217;s just a swift movement I do with my fingers.</p>
<p>An interesting example of how refactorings should be used I&#8217;ve heard from Kent Beck is his way to perform changes cohesively. Say you&#8217;re editing a method 10 lines long and need to perform a change on 2 lines in the middle of it. Most people might simply make the change, but Kent (at least some of the time) will extract those lines to a new method and make the change in that new method. If the new method feels right after the change he&#8217;ll just leave it there. If not, he&#8217;ll inline it. If you&#8217;re thinking that&#8217;s a lot of hassle, you&#8217;re missing all the fun here. This whole process has an overhead of 1 second on most IDEs, given you don&#8217;t have a hard time coming up with a name for the method.</p>
<p>Doing refactorings the right way means you do dozens of refactorings on each coding session. This means you get the ability to tailor your code in much bigger chunks &#8211; methods, members, classes, interfaces. After all, we usually don&#8217;t think about the need to move those parenthesis &#8211; we think about intent. Refactoring is all about intent!</p>
<p>Do yourself a favor and note your use of refactoring for a week. Learn all the hot keys your IDE has and make it a point to use them whenever possible. Whether you&#8217;re doing Java, C#, Python or Ruby, there&#8217;s no excuse not to use these basic building blocks for your every day use. Our stream of changes should be made of a lot of small, constant refactorings, pushing our design to a better place one small step at a time. Getting so familiar with this technique means you will no longer need to ask your boss for refactoring time. You won&#8217;t need those big refactorings. You&#8217;ll stop thinking about refactorings so much, they&#8217;ll simply happen.</p>
<p>Refactoring&#8217;s no longer a buzzword, but it&#8217;s time for us to learn how to use it properly. Refactor constantly and get your groove on.</p>
<p>You should subscribe to my <a href="http://feeds.feedburner.com/TheCodeDump">feed</a> and <a href="http://twitter.com/avivby">follow</a> me on twitter!
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F11%2F06%2Frefactoring-youre-doing-it-wrong%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F11%2F06%2Frefactoring-youre-doing-it-wrong%2F&amp;source=avivby&amp;style=normal&amp;service=bit.ly&amp;service_api=R_6107bfac7c92ddff62b393d1e8b7abbe&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codelord.net/2010/11/06/refactoring-youre-doing-it-wrong/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>DRY: Don&#8217;t Get Trigger Happy</title>
		<link>http://www.codelord.net/2010/11/04/dry-dont-get-trigger-happy/</link>
		<comments>http://www.codelord.net/2010/11/04/dry-dont-get-trigger-happy/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 18:11:38 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[pragprowrimo2010]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[DRY]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[software craftsmanship]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=243</guid>
		<description><![CDATA[Once you&#8217;ve started to assimilate DRY into your regular routine, it usually becomes addictive. The feedback loop constructed of removing moving parts, making stuff clearer and reducing chances of error simply gets you into a certain rhythm that&#8217;s hard to get enough of. The problem is, that as with any skill, you have to learn [...]]]></description>
			<content:encoded><![CDATA[<p>Once you&#8217;ve started to assimilate DRY into your regular routine, it usually becomes addictive. The feedback loop constructed of removing moving parts, making stuff clearer and reducing chances of error simply gets you into a certain rhythm that&#8217;s hard to get enough of.</p>
<p>The problem is, that as with any skill, you have to learn to master even the corner cases and not just blindly follow a mantra. Following a mantra is good enough for starting everything, but there comes a time you have to experience and understand for yourself when using a rule of thumb is a good idea and when it isn&#8217;t. So, let&#8217;s now talk about the &#8216;Ha&#8217; part of the <a href="http://en.wikipedia.org/wiki/Shuhari">ShuHaRi</a> cycle in DRY.</p>
<p>We know that duplicate code is a code smell and a DRY violation that needs to be removed. But, as with any rule, it has exceptions. Taking the dumb example, look at this piece of code:</p>
<p><script src="https://gist.github.com/662843.js?file=SimpleDuplication.java"></script></p>
<p>In the snippet above you can see the value &#8217;0&#8242; referenced twice. Is that a DRY violation? I&#8217;m sure you&#8217;ll agree it isn&#8217;t. After all, even though it is the same value that appears multiple times syntacticly, it has a different meaning semantically. The first zero is an initialization value and the other seems to indicate bad values in the array. The fact that they both are zero is meaningless and a coincidence. It might very well be that the next version will mark bad values as &#8216;null&#8217;, which surely shouldn&#8217;t change the way the array index is initialized.  OK, so that was an easy one. Let&#8217;s look at this next snippet, taking from some code whipped up solve the Game of Life:</p>
<p><script src="https://gist.github.com/662849.js?file=gof1.rb"></script></p>
<p>In that example, 2 appears twice also. Is that a DRY violation? After being in a <a href="http://www.codelord.net/2010/05/10/notes-from-the-first-israeli-code-retreat/">code retreat</a>, and hearing from other code retreaters, I understand that many people that try and focus on DRY think that is in fact a DRY violation. After all, both occurrences refer to the number of living neighbours a cell has and the number is part of the rules of the problem domain. But, let&#8217;s give this a bit more thought. Although both instances of 2 reference a rule of the game, they reference <em>different</em> rules of the game. Semantically, they are different. If we decide to change the minimal number of neighbours for a cell to stay alive it does not mean we will want to change the number of neighbours it takes to bring a cell to life.</p>
<p>I hope you&#8217;re catching my train of thought here. Now let&#8217;s look at another example. This is a snippet of code one might get to while doing the famous Bowling Kata:</p>
<p><script src="https://gist.github.com/662853.js?file=bowling.py"></script></p>
<p>This snippet adds the current frame&#8217;s score to the total score of the bowling game, taking into account whether the frame has a strike, a spare, or is a &#8216;simple&#8217; frame. Now, you might notice there is some &#8220;duplication&#8221; here. I can tell you that when I started refactoring this code to make it look better, I had to think a bit whether the calculation of the score in the first branch and the second branch should be extracted to be the same method.<br />
 But again, this is a mere textual duplication, and not a violation of real DRY, which is duplication of <em>intent</em>. The first one takes the score of the strike frame itself and adds the strike bonus (2 next rolls), the other takes the score of the spare frame and adds the spare bonus (1 next roll), so even though it takes the same additions, it is not the same logic. The better way is of course this:  <script src="https://gist.github.com/662854.js?file=bowling2.py"></script></p>
<p>This is a tricky one, and if you noticed it before reading give yourself a pat on the back! If you start and refactor this with an IDE, most IDEs will actually make a bit of a mess for you here, because they usually try and replace same occurrences of code once you extract a method, but here that would not be the better choice. Always keep an eye on your refactoring tools, because they, unlike you, can&#8217;t tell the difference between syntactic duplication and semantic duplication!</p>
<p>The big money of DRY comes when you learn to tell the difference between duplication of intent and mere duplication of text. This simple rule for judging your code helps you understand your code better, simply by making you actively consider what your real intention is.</p>
<p>I&#8217;ve written more about DRY <a href="http://www.codelord.net/2010/11/03/taking-dry-further/">here</a> and <a href="http://www.codelord.net/2010/11/02/short-intro-to-dry/">here</a> if you&#8217;d like to hear more.</p>
<p>You should subscribe to my <a href="http://feeds.feedburner.com/TheCodeDump">feed</a> and follow me on <a href="http://twitter.com/avivby">twitter</a>!
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F11%2F04%2Fdry-dont-get-trigger-happy%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F11%2F04%2Fdry-dont-get-trigger-happy%2F&amp;source=avivby&amp;style=normal&amp;service=bit.ly&amp;service_api=R_6107bfac7c92ddff62b393d1e8b7abbe&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codelord.net/2010/11/04/dry-dont-get-trigger-happy/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Taking DRY Further</title>
		<link>http://www.codelord.net/2010/11/03/taking-dry-further/</link>
		<comments>http://www.codelord.net/2010/11/03/taking-dry-further/#comments</comments>
		<pubDate>Wed, 03 Nov 2010 21:56:42 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[pragprowrimo2010]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[techie]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[DRY]]></category>
		<category><![CDATA[software craftsmanship]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=239</guid>
		<description><![CDATA[After learning to spot basic DRY violations, such as code you&#8217;ve just copied from somewhere, it&#8217;s time to learn how to use DRY to drive a lot more in your system. DRY can be used extensively in your code base to alert you of problems waiting to happen. For example, similar code structures in different [...]]]></description>
			<content:encoded><![CDATA[<p>After learning to <a href="http://www.codelord.net/2010/11/02/short-intro-to-dry/">spot basic DRY</a> violations, such as code you&#8217;ve just copied from somewhere, it&#8217;s time to learn how to use DRY to drive a lot more in your system.</p>
<p>DRY can be used extensively in your code base to alert you of problems waiting to happen. For example, similar code structures in different parts of the code are usually a DRY violation. This violation causes coupling which in turn will make it harder to change the code. The good scenario is that you have to tediously go through all the repetitions and change them to accommodate the change. The worse case is you forget to change one and introduce inconsistency in your code.</p>
<p>Train yourself to note these feelings of &#8220;yeah, I&#8217;ll have to do <em>that</em> again here&#8221;. Lucky for me, whenever I do something too many times I automatically start referring to it as &#8220;the dance&#8221;. So, once I hear myself saying to a teammate that &#8220;I&#8217;m doing the add-view-dance&#8221; I know it&#8217;s time to do something about it.</p>
<p>But DRY need not apply only to your code. Actually, it certainly shouldn&#8217;t! Copying is bad, even if you do it in a configuration file, or &#8220;just in that script&#8221;. It&#8217;s our tendency to disregard these &#8220;minor&#8221; parts of our system as not worthy of our attention, but these are places that are at least as likely to cause problems as our code. Taking the time to make the deployment scripts tidier will pay off once you decide to leave that server at home and deploy your app on EC2.</p>
<p>DRY used correctly will make driving changes in your app a breeze, be it a configuration change, a DB change or a real feature you need to add. The saved cycles of work saved by not having to dig around the code and look for other places your forgot to update, or hunt the bugs caused after making a change pay off immensely for the time spent on making things the good way.</p>
<p>Let&#8217;s take this one step further. Do you document your app? For example, do you supply wiki pages with explanations for installing the system, executing it or making configuration changes? There isn&#8217;t a coder out there that hasn&#8217;t been bitten by an outdated guide that was updated to reflect the way things should be done now. This is a subtle DRY violation &#8211; the is repetition between the description of the process in the wiki and the real process, as is usually saved in people&#8217;s heads or scripts. If you&#8217;ve got a script, why not simply use it as documentation?</p>
<p>Most people will probably understand your shell script better, and it is sure to be up-to-date. I tend to document the script itself to make it clearer (and generally treat it as part of the code base) which pays off when I don&#8217;t have to hear people get angry with me for not updating the wiki.</p>
<p>The even more important example is sample code. Almost every app will, at some point, require you to supply sample code for working with it. The knee-jerk solution is to hack some code and put it on a wiki page. You might even go the extra mile and run the code to make sure it works. 24 hours later, you change the code and the samples are now obsolete. Again, duplication between real code and sample code has created a problem for us. The solution, once more, is to fight this duplication. Putting the samples as part of the app&#8217;s version control, which will make sure they compile and run with every change, and pointing the wiki to these files (or even embedding them in the wiki) will save you a great hassle. Sometimes you can even make these examples part of the code, such as Python&#8217;s Doctests, which allows you to put executable usage examples right next to the code.</p>
<p>Once more, training your senses to notice these violations and finding creative solutions for them takes practice and time, but gets a lot easier once you get the trick. This is a crucial tool in the pragmatic programmer&#8217;s belt.</p>
<p>DRY, actually, is a measure of technical debt. The more violations you allow to slip in the codebase, the harder you&#8217;ll have to work later to change the code. A DRY codebase is usually more cohesive and loosely coupled which leads to a more responsive design. Effort you put into keeping your code DRY pays off, quickly and by manyfold.</p>
<p>You should subscribe to my <a href="http://feeds.feedburner.com/TheCodeDump">feed</a> and follow me on <a href="http://twitter.com/avivby">twitter</a>.
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F11%2F03%2Ftaking-dry-further%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2010%2F11%2F03%2Ftaking-dry-further%2F&amp;source=avivby&amp;style=normal&amp;service=bit.ly&amp;service_api=R_6107bfac7c92ddff62b393d1e8b7abbe&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codelord.net/2010/11/03/taking-dry-further/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

