<?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; tdd</title>
	<atom:link href="http://www.codelord.net/tag/tdd/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>Extend Your Toolbox: Custom Matchers</title>
		<link>http://www.codelord.net/2012/02/04/extend-your-toolbox-custom-matchers/</link>
		<comments>http://www.codelord.net/2012/02/04/extend-your-toolbox-custom-matchers/#comments</comments>
		<pubDate>Sat, 04 Feb 2012 12:53:41 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[DRY]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[software craftsmanship]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=726</guid>
		<description><![CDATA[I&#8217;d like to point out a really nice testing practice that I&#8217;ve been loving more and more lately. Just about every mature testing framework out there supports the concept of custom matchers, which provide us with the ability to define our very own assertions seamlessly into the tests. Even though this ability is quite old, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d like to point out a really nice testing practice that I&#8217;ve been loving more and more lately.</p>
<p>Just about every mature testing framework out there supports the concept of custom matchers, which provide us with the ability to define our very own assertions seamlessly into the tests. Even though this ability is quite old, we don&#8217;t see it used too often and I think that&#8217;s a shame. I&#8217;ve seen this practice heavily used in the mind expanding <a href="http://www.amazon.com/gp/product/0321503627?ie=UTF8&amp;tag=thcodu02-20&amp;linkCode=shr&amp;camp=213733&amp;creative=393185&amp;creativeASIN=0321503627">GOOS</a> book and just now am starting to realize its awesomeness.<br />
<div class="wp-caption alignright" style="width: 250px"><a href="http://www.flickr.com/photos/dipster1/1403240351/"><img title="Your Testing Toolbox" src="http://farm2.staticflickr.com/1155/1403240351_68114a0c53.jpg" alt="toolbox" width="240" height="126" /></a><p class="wp-caption-text">Your Testing Toolbox</p></div><br />
Note: examples in this post are shown in Ruby using <a href="https://github.com/dchelimsky/rspec/wiki/Custom-Matchers">RSpec&#8217;s matchers</a> but the concept is pretty much identical (as can be seen for example in Java&#8217;s <a href="http://code.google.com/p/hamcrest/wiki/Tutorial">Hamcrest Matchers</a>).</p>
<h3>Matchers 101</h3>
<p>Creating your own matcher usually means creating a Matcher class that performs the assertions, supplies human readable error messages and a nice constructor.</p>
<p>Here&#8217;s an example from the <a href="https://github.com/dchelimsky/rspec/wiki/Custom-Matchers">RSpec documentation</a>:</p>
<div id="gist-1737631" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="no">RSpec</span><span class="o">::</span><span class="no">Matchers</span><span class="o">.</span><span class="n">define</span> <span class="ss">:be_a_multiple_of</span> <span class="k">do</span> <span class="o">|</span><span class="n">expected</span><span class="o">|</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="n">match</span> <span class="k">do</span> <span class="o">|</span><span class="n">actual</span><span class="o">|</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">actual</span> <span class="o">%</span> <span class="n">expected</span> <span class="o">==</span> <span class="mi">0</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="k">end</span></div><div class='line' id='LC5'><span class="k">end</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1737631/2d0980f927c298854d8ace00013c2f1a5f28dedd/rspec_matcher.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1737631#file_rspec_matcher.rb" style="float:right;margin-right:10px;color:#666">rspec_matcher.rb</a>
            <a href="https://gist.github.com/1737631">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h3>Matchers increase readability and intent</h3>
<p>As you should know, one of the <a href="http://c2.com/cgi/wiki?XpSimplicityRules">most important rules for design</a> is <em>Reveals Intent</em>. Take a quick look here, which way do you think reveals more intent?</p>
<div id="gist-1737631" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1"># This</span></div><div class='line' id='LC2'><span class="n">response</span><span class="o">[</span><span class="s1">&#39;X-Runtime&#39;</span><span class="o">].</span><span class="n">should</span> <span class="o">=~</span> <span class="sr">/[\d\.]+/</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'><span class="c1"># .. or this?</span></div><div class='line' id='LC5'><span class="n">response</span><span class="o">[</span><span class="s1">&#39;X-Runtime&#39;</span><span class="o">].</span><span class="n">should</span> <span class="n">be_a_number</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1737631/215e237f07b383c5792cbb5cca0adcc2b5f4864a/intent.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1737631#file_intent.rb" style="float:right;margin-right:10px;color:#666">intent.rb</a>
            <a href="https://gist.github.com/1737631">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Also, which error message do you prefer? &#8220;expected false to be true&#8221; or something along the lines of &#8220;expected comment to be anonymous&#8221;?</p>
<h3>Matchers create robust tests</h3>
<p>The most important advantage of all is how using matchers easily allows you to steer away from fragile tests which are the bane of a lot of testing efforts.<br />
The mark of good tests is that a change in your code doesn&#8217;t require you to perform changes in multiple tests that don&#8217;t really care for the change.<br />
Take this code for example:</p>
<div id="gist-1737631" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">expected_comment</span> <span class="o">=</span> <span class="no">Comment</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">anonymous</span><span class="p">:</span> <span class="kp">true</span><span class="p">,</span> <span class="n">user</span><span class="p">:</span> <span class="s2">&quot;the dude&quot;</span><span class="p">,</span> <span class="n">reply_to</span><span class="p">:</span> <span class="kp">nil</span><span class="p">)</span></div><div class='line' id='LC2'><span class="n">commentor</span><span class="o">.</span><span class="n">should_receive</span><span class="p">(</span><span class="ss">:add</span><span class="p">)</span><span class="o">.</span><span class="n">with</span><span class="p">(</span><span class="n">expected_comment</span><span class="p">)</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1737631/af12f952b61c745a7bce91c5980eae0b382d751a/sucky_non_dry.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1737631#file_sucky_non_dry.rb" style="float:right;margin-right:10px;color:#666">sucky_non_dry.rb</a>
            <a href="https://gist.github.com/1737631">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This might seem like a standard test, but that&#8217;s not really the case. A test should assert for a single piece of knowledge, and this test actually checks several. If the purpose of this test is to check the behavior of anonymous comments, why should it change if we no longer allow replies? Or if we no longer require users for posting comments?</p>
<p>The magic of matchers is exactly here. You create a new matcher to check specifically the aspect your test cares about and *boom*, you&#8217;re decoupled!</p>
<div id="gist-1737631" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">commentor</span><span class="o">.</span><span class="n">should_receive</span><span class="p">(</span><span class="ss">:add</span><span class="p">)</span><span class="o">.</span><span class="n">with</span><span class="p">(</span><span class="n">anonymous_comment</span><span class="p">)</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1737631/3f386a4f184528f678b316dc45c94cee32c8cae1/beautiful_and_dry.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1737631#file_beautiful_and_dry.rb" style="float:right;margin-right:10px;color:#666">beautiful_and_dry.rb</a>
            <a href="https://gist.github.com/1737631">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This simple change makes your tests DRY and cool.</p>
<p>Happy testing!</p>
<p>Your should <a href="http://feeds.feedburner.com/TheCodeDump">subscribe</a> to my feed or <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%2F2012%2F02%2F04%2Fextend-your-toolbox-custom-matchers%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2012%2F02%2F04%2Fextend-your-toolbox-custom-matchers%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/2012/02/04/extend-your-toolbox-custom-matchers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Looking Back on 18 months of Testing and TDD at a Startup</title>
		<link>http://www.codelord.net/2012/01/06/looking-back-on-18-months-of-testing-and-tdd-at-a-startup/</link>
		<comments>http://www.codelord.net/2012/01/06/looking-back-on-18-months-of-testing-and-tdd-at-a-startup/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 20:34:25 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[billguard]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=701</guid>
		<description><![CDATA[As we&#8217;re approaching a year and half here at BillGuard, I&#8217;ve started thinking back a bit about our testing habits and how well that&#8217;s turned out. I&#8217;ve seen a lot of posts about testing in startups, some saying startups shouldn&#8217;t bother to test because they&#8217;ll have to change the whole damn thing 5 minutes after [...]]]></description>
			<content:encoded><![CDATA[<p>As we&#8217;re approaching a year and half here at <a href="https://www.billguard.com">BillGuard</a>, I&#8217;ve started thinking back a bit about our testing habits and how well that&#8217;s turned out.</p>
<p>I&#8217;ve seen a lot of posts about testing in startups, some saying startups shouldn&#8217;t bother to test because they&#8217;ll have to change the whole damn thing 5 minutes after they&#8217;re done, others claim testing is the only reason they were able to keep working. Here are some of my thoughts looking back.</p>
<h3>Our Background</h3>
<p>When we started, only two of us had a test-infected background out of the five technical guys, me being big on <a href="http://www.amazon.com/gp/product/0321146530/ref=as_li_ss_tl?ie=UTF8&amp;tag=thcodu02-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321146530">TDD</a><img style="border: none !important; margin: 0px !important; display: none;" src="http://www.assoc-amazon.com/e/ir?t=thcodu02-20&amp;l=as2&amp;o=1&amp;a=0321146530" alt="" width="1" height="1" border="0" />. Two other developers never wrote tests before. We agreed that tests were important, but that&#8217;s about it. I set up a continuous integration server and with that we were off. With time, the habit of writing tests spread out among the team. Some are TDD passionate, some write tests after the fact, but we generally all believe that tests should be written extensively.</p>
<h3></h3>
<div id="attachment_704" class="wp-caption aligncenter" style="width: 250px"><a href="http://www.codelord.net/wp-content/uploads/2012/01/test_in_prod.png"><img class="size-medium wp-image-704 " title="test_in_prod" src="http://www.codelord.net/wp-content/uploads/2012/01/test_in_prod-240x300.png" alt="" width="240" height="300" /></a><p class="wp-caption-text">None of us, ever</p></div>
<h3>Not everything is worth testing</h3>
<p>We&#8217;ve seen several quite rapid changes to our UI. Having less tests in this area makes sense. We rely on QA for making sure all buttons are displayed etc. To make this clear: we have no selenium-like tests for UI components but have tests for most logic being done by the UI. I think this is generally a good practice, since having to maintain selenium tests would be hard when you throw things around a lot and change flows. Some basic automated sanity tests pretty much does it.</p>
<h3>Everyone learned to love tests</h3>
<p>I love seeing other guys in the team delete a line of code to see which test breaks and understand why it&#8217;s there. Even more I love the frowning face when no tests break. This addiction to tests shows how much value the team&#8217;s getting out of having solid tests, hands down. No need to stress this further I believe.</p>
<h3>Tests save our asses repeatedly</h3>
<p>Having an extensive suite of tests allows us to make rapid changes to our code base, as is needed in most startups, and rely on the solid tests to tell us whether we&#8217;ve screwed something up. All the code that has anything whatsoever to do with sensitive and important information is heavily tested which is a huge bonus and a necessity in our line of business (personal finance protection).</p>
<h3>TDD is just magical with complex algorithms</h3>
<p>We have quite a few complex algorithms that require multiple entities and ideas to perform. I find that the parts we&#8217;re most satisfied with maintainability-wise are the heavily TDD-ed algorithms we&#8217;ve got. Being written with rigorous TDD gives us so many advantages:</p>
<ul>
<li>This critical code usually has a lot less defects.</li>
<li>The code is a lot more readable, well decomposed and allows for easy changing once we find out a need for tweaking the algorithms.</li>
<li>Working in TDD magically forces us to form our problem domain better, making us have a language of our own in talking about the problem. This happens less naturally in other forms of working on algorithms.</li>
</ul>
<h3>Summing our testing experiences</h3>
<p>All in all, I think the whole team would agree that dedicating time to writing thorough tests is proving itself valuable and because of that people are writing more and more tests without any of us ever stopping and saying &#8220;we should write tests&#8221; (well, I swear I didn&#8217;t do it too much). It happens naturally when people get the value out of it. It&#8217;s fun seeing how today BillGuard has become a company that organically values testing so much I don&#8217;t even feel a great need to stress it to new people because they&#8217;ll quickly see there&#8217;s no real other way. We&#8217;re far from being the poster children of <a href="http://www.amazon.com/gp/product/0132350882/ref=as_li_ss_tl?ie=UTF8&amp;tag=thcodu02-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0132350882">Clean Code</a><img style="border: none !important; margin: 0px !important; display: none;" src="http://www.assoc-amazon.com/e/ir?t=thcodu02-20&amp;l=as2&amp;o=1&amp;a=0132350882" alt="" width="1" height="1" border="0" />, but I&#8217;ve got my fingers crossed.</p>
<p>If you&#8217;re interested in accomplishing the same at your work, you might find <a href="http://www.codelord.net/2011/11/28/stop-bitching-write-those-damn-tests/">this recent post</a> of mine of some help.</p>
<p>You should <a href="http://feeds.feedburner.com/TheCodeDump">subscribe</a> to my feed or <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%2F2012%2F01%2F06%2Flooking-back-on-18-months-of-testing-and-tdd-at-a-startup%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2012%2F01%2F06%2Flooking-back-on-18-months-of-testing-and-tdd-at-a-startup%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/2012/01/06/looking-back-on-18-months-of-testing-and-tdd-at-a-startup/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Fight Zombie Code</title>
		<link>http://www.codelord.net/2011/10/28/fight-zombie-code/</link>
		<comments>http://www.codelord.net/2011/10/28/fight-zombie-code/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 14:57:27 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[software craftsmanship]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=490</guid>
		<description><![CDATA[If there&#8217;s anything I hate more than dead code, it&#8217;s zombie code. Dead code is code that&#8217;s remained in the system even though it&#8217;s no longer really used. It might be small, like unused imports, instance variables and methods. It can be whole classes that make up entire features no longer used. The biggest PITA [...]]]></description>
			<content:encoded><![CDATA[<p>If there&#8217;s anything I hate more than dead code, it&#8217;s zombie code. Dead code is code that&#8217;s remained in the system even though it&#8217;s no longer really used.</p>
<p>It might be small, like unused imports, instance variables and methods. It can be whole classes that make up entire features no longer used.</p>
<p>The biggest PITA is when you&#8217;re not really aware of the fact the code&#8217;s dead and unused, sitting there and occupying precious bits, and stumble across it as part of a task, trying to understand how it influences what you want to do next.</p>
<p>Whenever I recognized something that looks like dead code but I&#8217;m not entirely sure, I find it pretty easy to delete it quickly, once I take a look in the version control logs and see when it became no longer in use and why.</p>
<p>Zombie code is code that was never alive, and so couldn&#8217;t really become dead. It&#8217;s the undead code &#8211; code that died right when it was committed. Code that never ran or never worked. The are two reasons I hate zombie code more than &#8220;plain&#8221; dead code.</p>
<p><a href="http://codelord.net/wp-content/uploads/2011/10/test_my_code.jpg"><img class="aligncenter size-medium wp-image-491" title="test_my_code" src="http://codelord.net/wp-content/uploads/2011/10/test_my_code-300x300.jpg" alt="" width="300" height="300" /></a></p>
<p>The first reason is that it simply wastes more of my time. Looking back in version control won&#8217;t help me see the commit in which the code was &#8220;decommissioned&#8221;, it would just appear to always sit there. That means I have to take extra care to verify that it, in fact, never worked.</p>
<p>The second reason is that it&#8217;s plainly someone saying he doesn&#8217;t give a damn. I mean, let&#8217;s put aside TDD. Heck, let&#8217;s put aside unit testing at all. It means the code never even ran the damn thing and saw in his own eyes it did what he claims it did.</p>
<p>Be kind to your teammates. If you&#8217;re not a good enough coder to test it, at least see that it runs <em>once</em> in your own eyes.</p>
<p>&nbsp;</p>
<p>You should <a href="http://feeds.feedburner.com/TheCodeDump">subscribe</a> to my feed 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%2F2011%2F10%2F28%2Ffight-zombie-code%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2011%2F10%2F28%2Ffight-zombie-code%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/10/28/fight-zombie-code/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Today I Got Burnt by Isolated Tests</title>
		<link>http://www.codelord.net/2011/08/07/today-i-got-burnt-by-isolated-tests/</link>
		<comments>http://www.codelord.net/2011/08/07/today-i-got-burnt-by-isolated-tests/#comments</comments>
		<pubDate>Sun, 07 Aug 2011 21:06:26 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[goos]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=440</guid>
		<description><![CDATA[Generally, I prefer the GOOS school of TDD which includes isolating my classes as much as possible, putting mocks and stubs everywhere. Even though one of its known disadvantages is that you risk testing your classes in a fake environment that won&#8217;t match the real production code using it, I&#8217;ve rarely come across a place [...]]]></description>
			<content:encoded><![CDATA[<p>Generally, I prefer the <a href="http://www.amazon.com/gp/product/0321503627/ref=as_li_tf_tl?ie=UTF8&amp;tag=thcodu02-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321503627">GOOS</a><img style="display: none; 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" alt="" width="1" height="1" border="0" /> school of TDD which includes isolating my classes as much as possible, putting mocks and stubs everywhere. Even though one of its known disadvantages is that you risk testing your classes in a fake environment that won&#8217;t match the real production code using it, I&#8217;ve rarely come across a place where I got really bitten by it.</p>
<p>Today I set out with my pair to add some functionality to a certain class. That class had about 30-40 lines of code and about 10 test cases, which seemed quite decent. We added our changes TDD style and just couldn&#8217;t get the thing working. After digging into it for a few more minutes we suddenly realized the class shouldn&#8217;t be working at all and checking in the DB showed that indeed the last time that specific feature had any effect was 3 months ago!</p>
<p>Fortunately for us, all the problems that caused this bug are solved problems, we just need to get better at implementing the solutions:</p>
<p>Isolated tests go much better hand in hand with a few <strong><em>integration tests</em></strong> (some might say the right term is acceptance tests) that execute the whole system and make sure the features are working. Had we had those, we would have caught the bug much sooner.</p>
<p>The bug was introduced in a <strong><em>huge commit</em></strong> that changes 35 files and 1500 lines of code. We usually try and go over every commit made, even if it was paired, because we believe in collective code ownership, but it&#8217;s impossible to go over such a huge diff and find these intricacies. Working in small baby steps makes it far less likely to break something and more likely that someone else will spot your mistakes. Huge refactorings give me the creeps.</p>
<p>After the change was committed, it was not <strong><em>followed-through</em></strong>: this specific feature is a feature you usually notice over a few days and we missed out on making sure it kept working. We moved on to other tasks and forgot all about it, thinking it was working all this time. Had we taken the time to make sure we were seeing, it would have been squashed by the next deployment.</p>
<p>Any of these would have helped us spot sooner that the isolated tests were actually testing the code against a scenario that never happens. These tiny changes of our workflow would have made several of our users happier over this timeframe.</p>
<p>Hopefully all is well now and the feature is back at 100%, but only time will tell whether we were able to learn from this mishap.</p>
<p>You should <a href="http://feeds.feedburner.com/TheCodeDump">subscribe</a> to my feed 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%2F2011%2F08%2F07%2Ftoday-i-got-burnt-by-isolated-tests%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2011%2F08%2F07%2Ftoday-i-got-burnt-by-isolated-tests%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/08/07/today-i-got-burnt-by-isolated-tests/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>In the Mind of a Master Programmer</title>
		<link>http://www.codelord.net/2011/07/25/in-the-mind-of-a-master-programmer/</link>
		<comments>http://www.codelord.net/2011/07/25/in-the-mind-of-a-master-programmer/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 18:39:47 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=404</guid>
		<description><![CDATA[He would probably object to me calling him that, but I&#8217;ve long ago realized Kent Beck is one of the precious few who deserve the title &#8220;a mastermind&#8221;. With Extreme Programming, Test Driven Development, Responsive Design, the Four Elements of Simple Design and more under his belt, who can claim otherwise? After attending a workshop [...]]]></description>
			<content:encoded><![CDATA[<p>He would probably object to me calling him that, but I&#8217;ve long ago realized Kent Beck is one of the precious few who deserve the title &#8220;a mastermind&#8221;. With <a href="http://www.amazon.com/gp/product/0321278658/ref=as_li_tf_tl?ie=UTF8&amp;tag=thcodu02-20&amp;linkCode=as2&amp;camp=217145&amp;creative=399381&amp;creativeASIN=0321278658">Extreme Programming</a><img style="border: none !important; margin: 0px !important; display: none;" src="http://www.assoc-amazon.com/e/ir?t=thcodu02-20&amp;l=as2&amp;o=1&amp;a=0321278658&amp;camp=217145&amp;creative=399381" alt="" width="1" height="1" border="0" />, <a href="http://www.amazon.com/gp/product/0321146530/ref=as_li_tf_tl?ie=UTF8&amp;tag=thcodu02-20&amp;linkCode=as2&amp;camp=217145&amp;creative=399381&amp;creativeASIN=0321146530">Test Driven Development</a><img style="border: none !important; margin: 0px !important; display: none;" src="http://www.assoc-amazon.com/e/ir?t=thcodu02-20&amp;l=as2&amp;o=1&amp;a=0321146530&amp;camp=217145&amp;creative=399381" alt="" width="1" height="1" border="0" />, <a href="http://www.threeriversinstitute.org/blog/?cat=6">Responsive Design</a>, the <a href="http://www.jbrains.ca/permalink/the-four-elements-of-simple-design">Four Elements of Simple Design</a> and more under his belt, who can claim otherwise?</p>
<p>After attending a workshop of his about a year ago and listening to him talk a whole day I was astonished. I tried to pick his brains to understand what makes him tick. Of course there are many factors here &#8211; reading over 10,000 books and being smarter than most would help anyone. But something a bit less common takes a major part in this in my opinion, what Kent told me he has: a &#8220;<em>habit of desperately wanting things to make sense&#8221;</em> and his ability to take things apart until they do.</p>
<p>I recently picked up another of Kent&#8217;s books, <a href="http://www.amazon.com/gp/product/0321413091/ref=as_li_tf_tl?ie=UTF8&amp;tag=thcodu02-20&amp;linkCode=as2&amp;camp=217145&amp;creative=399381&amp;creativeASIN=0321413091">Implementation Patterns</a><img style="border: none !important; margin: 0px !important; display: none;" src="http://www.assoc-amazon.com/e/ir?t=thcodu02-20&amp;l=as2&amp;o=1&amp;a=0321413091&amp;camp=217145&amp;creative=399381" alt="" width="1" height="1" border="0" />. I love this book because it shows exactly that: his process of thinking and breaking things apart in order to understand them. The book provides a rare glimpse to his method of decomposition. Since I&#8217;ve been coding for years, a lot of the patterns made sense to me or seemed trivial. But the &#8220;magic&#8221; is the fact he was able to put into words things that for me were just hunches. Actually explaining what makes you sense a method is too long or what is a proper name for a variable is something I&#8217;ve never seen done with such care to specifics.</p>
<p>Because it&#8217;s such a quick read, I think anyone will benefit by reading Implementation Patterns. More than helping you understand our craft better, it will provide a new outlook on decomposing and judging your designs and pretty much everything else.</p>
<p>&nbsp;</p>
<p>You should <a href="http://feeds.feedburner.com/TheCodeDump">subscribe</a> to my feed 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%2F2011%2F07%2F25%2Fin-the-mind-of-a-master-programmer%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2011%2F07%2F25%2Fin-the-mind-of-a-master-programmer%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/07/25/in-the-mind-of-a-master-programmer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sometimes Tests Have to Fail</title>
		<link>http://www.codelord.net/2011/04/03/sometimes-tests-have-to-fail/</link>
		<comments>http://www.codelord.net/2011/04/03/sometimes-tests-have-to-fail/#comments</comments>
		<pubDate>Sun, 03 Apr 2011 18:40:18 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=376</guid>
		<description><![CDATA[A friend asked me about a common problem that pops up in real-world projects and testing: What do you do when you test code with random properties? A simple example might be handing out jobs to a few workers. If your algorithm for doing that is random, you can usually assert that no one of [...]]]></description>
			<content:encoded><![CDATA[<p>A friend asked me about a common problem that pops up in real-world projects and testing: What do you do when you test code with random properties?</p>
<p>A simple example might be handing out jobs to a few workers. If your algorithm for doing that is random, you can usually assert that no one of 3 workers gets all 10 jobs, for example. But, being random, that assert should eventually fail. We&#8217;ll assume that with the frequency the team runs the tests, a failure is expected every few days.</p>
<p>Surely no one wants to see the tests fail a couple times a week (especially if you&#8217;re keeping score for who broke the build). On the other hand, you&#8217;d like to keep the tests. What is a pragmatic coder to do?</p>
<p>If you&#8217;re not that meticulous to your suite rarely failing, you might just leave it as it is, which, I think, sucks.</p>
<p>The mega-tester&#8217;s approach, which I&#8217;ve tried in the past, is usually to stub out the random number generator with values that make sure the failures won&#8217;t happen. This is usually cost-effective only for the simplest of cases, and the more complex ones results in brittle tests that are coupled to the implementation and that might need to be changed frequently.</p>
<p>What I rather is to postpone the problem! Say we change our test&#8217;s parameters to 10 workers and 3000 jobs. The chances of one worker getting all jobs becomes quite minor. This tweak of parameters in the test is usually simple to do and can guarantee quite a safety net.</p>
<p>And still, sometimes bad stuff happen. 64bit hash collisions are somewhere, out there in the world. If you&#8217;re one of those guys that are bugged by that chance, I give you a simple JUnit rule that will retry a specific test in case it fails, making it twice as unlikely to fail. Those 64bit collisions are now more like 128bit! woohoo!</p>
<p>The rule allows you to simply annotate a test to make it retry in case it fails:</p>
<div id="gist-897229" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">RetrierTest</span> <span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="kd">private</span> <span class="kd">static</span> <span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="nd">@Rule</span> <span class="kd">public</span> <span class="n">RetryRule</span> <span class="n">rule</span> <span class="o">=</span> <span class="k">new</span> <span class="n">RetryRule</span><span class="o">();</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="nd">@Test</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="nd">@Retry</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="kd">public</span> <span class="kt">void</span> <span class="nf">failsFirst</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">count</span><span class="o">++;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">assertEquals</span><span class="o">(</span><span class="mi">2</span><span class="o">,</span> <span class="n">count</span><span class="o">);</span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC12'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/897229/d848f470464d5426fa7482aa5473218f7aa74d8e/RetrierTest.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/897229#file_retrier_test.java" style="float:right;margin-right:10px;color:#666">RetrierTest.java</a>
            <a href="https://gist.github.com/897229">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>And the implementation is as simple as:</p>
<div id="gist-897229" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nd">@Retention</span><span class="o">(</span><span class="n">RetentionPolicy</span><span class="o">.</span><span class="na">RUNTIME</span><span class="o">)</span></div><div class='line' id='LC2'><span class="kd">public</span> <span class="nd">@interface</span> <span class="n">Retry</span> <span class="o">{}</span></div><div class='line' id='LC3'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/897229/f72b15925563a7fe8d8ef7fc7e8141a5ab3b964a/Retry.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/897229#file_retry.java" style="float:right;margin-right:10px;color:#666">Retry.java</a>
            <a href="https://gist.github.com/897229">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<div id="gist-897229" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">RetryRule</span> <span class="kd">implements</span> <span class="n">MethodRule</span> <span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="nd">@Override</span> <span class="kd">public</span> <span class="n">Statement</span> <span class="n">apply</span><span class="o">(</span><span class="kd">final</span> <span class="n">Statement</span> <span class="n">base</span><span class="o">,</span> <span class="kd">final</span> <span class="n">FrameworkMethod</span> <span class="n">method</span><span class="o">,</span> <span class="n">Object</span> <span class="n">target</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="k">new</span> <span class="nf">Statement</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nd">@Override</span> <span class="kd">public</span> <span class="kt">void</span> <span class="n">evaluate</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Throwable</span> <span class="o">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">base</span><span class="o">.</span><span class="na">evaluate</span><span class="o">();</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">Throwable</span> <span class="n">t</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Retry</span> <span class="n">retry</span> <span class="o">=</span> <span class="n">method</span><span class="o">.</span><span class="na">getAnnotation</span><span class="o">(</span><span class="n">Retry</span><span class="o">.</span><span class="na">class</span><span class="o">);</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="o">(</span><span class="n">retry</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">base</span><span class="o">.</span><span class="na">evaluate</span><span class="o">();</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">else</span> <span class="o">{</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">throw</span> <span class="n">t</span><span class="o">;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">};</span></div><div class='line' id='LC17'>&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC18'><span class="o">}</span></div><div class='line' id='LC19'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/897229/9d79a808cd1c26a9b535f7c8dfa8eb2c0f503d55/RetryRule.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/897229#file_retry_rule.java" style="float:right;margin-right:10px;color:#666">RetryRule.java</a>
            <a href="https://gist.github.com/897229">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>With the tests so unlikely to fail, I&#8217;d start a lottery at work for whoever breaks them.</p>
<p>Happy testing!</p>
<p>You should <a href="http://feeds.feedburner.com/TheCodeDump">subscribe</a> to my feed 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%2F2011%2F04%2F03%2Fsometimes-tests-have-to-fail%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2011%2F04%2F03%2Fsometimes-tests-have-to-fail%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/04/03/sometimes-tests-have-to-fail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing Techniques: Managing External Resources</title>
		<link>http://www.codelord.net/2011/04/01/testing-techniques-managing-external-resources/</link>
		<comments>http://www.codelord.net/2011/04/01/testing-techniques-managing-external-resources/#comments</comments>
		<pubDate>Fri, 01 Apr 2011 04:45:32 +0000</pubDate>
		<dc:creator>Aviv Ben-Yosef</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.codelord.net/?p=372</guid>
		<description><![CDATA[A friend approached me with one of the known problems in the testing world &#8211; How do you keep external resources under a test harness? Having heard the question a few times before, I thought I&#8217;d share my thoughts, and mainly put together the common advice that drifts around the web. The Dilemma Nowadays, it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>A friend approached me with one of the known problems in the testing world &#8211; How do you keep external resources under a test harness? Having heard the question a few times before, I thought I&#8217;d share my thoughts, and mainly put together the common advice that drifts around the web.</p>
<h3>The Dilemma</h3>
<p>Nowadays, it&#8217;s hard to get more than a 100 lines of code before adding an external resource to our code. It might be a web service to manage something, or some convoluted API to receive data from or just about anything. Usually, writing tests for code that directly talks with these resources using the resources themselves is very problematic, for numerous reasons:</p>
<ul>
<li>It significantly slows the tests, because it requires network access and processing on the service&#8217;s side.</li>
<li>It might cost you money, send emails, tweet stuff and do things you&#8217;d rather not do 300 times a day as you run your tests.</li>
<li>Making your code handle error conditions with the service is hard or impossible, as you can&#8217;t control when those occur.</li>
</ul>
<p>Basically, all of these factors usually amount up to you having crappy tests that you rarely run. That sucks.</p>
<h3>Decouple &amp; Isolate</h3>
<p>The best solution I&#8217;m aware of is simply isolating the thing. We usually strive to wrap whatever service we&#8217;re using with a single-point interface. The decoupling is great since I&#8217;ve yet to encounter a service with an API that matched my thinking of the domain problem. Wrapping it up allows us to keep using our own language and logic throughout the system.</p>
<p>A benefit of that is we now have a simple interface or facade we need to stub/mock out during tests. That&#8217;s usually relatively easy, and allows us to run our tests blazingly fast and test all those hard to reach to corner cases.</p>
<h3>But what if the service changes?</h3>
<p>That&#8217;s the finishing touch. You should still maintain a suite of tests that run against the real service. Those should be the plain tests that make sure you&#8217;re using the API right and that would break if anything you&#8217;re relying on changes. These tests won&#8217;t be part of your regular suite that gets run constantly. Instead have your CI server run them daily/weekly and let you know when something changes.</p>
<p>This puts us basically in a win-win situation, with us being able to run our tests quickly and yet have the assurance that we won&#8217;t miss API changes and the likes.</p>
<p>Happy testing!</p>
<p>You should <a href="http://feeds.feedburner.com/TheCodeDump">subscribe</a> to my feed 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%2F2011%2F04%2F01%2Ftesting-techniques-managing-external-resources%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codelord.net%2F2011%2F04%2F01%2Ftesting-techniques-managing-external-resources%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/04/01/testing-techniques-managing-external-resources/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>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>
	</channel>
</rss>

