<?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>Agile at Work &#187; TDD</title>
	<atom:link href="http://www.agileatwork.com/tag/tdd/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.agileatwork.com</link>
	<description>by Michael Valenty</description>
	<lastBuildDate>Sat, 10 Sep 2011 14:35:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Maybe Uncle Bob is Wrong About Testing</title>
		<link>http://www.agileatwork.com/maybe-uncle-bob-is-wrong-about-testing/</link>
		<comments>http://www.agileatwork.com/maybe-uncle-bob-is-wrong-about-testing/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 06:09:46 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.agileatwork.com/?p=218</guid>
		<description><![CDATA[As pressure increases, testing decreases. As the testing decreases, errors increase and as errors increase, pressure increases more. This is what’s known as a positive feedback loop, and this was how I spent the better part of last week.
&#160;
Figure A.5 Not enough time to test reduces the available time     Test-Driven Development: [...]]]></description>
			<content:encoded><![CDATA[<p>As pressure increases, testing decreases. As the testing decreases, errors increase and as errors increase, pressure increases more. This is what’s known as a positive feedback loop, and this was how I spent the better part of last week.</p>
<p>&#160;<img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="feedback2" border="0" alt="feedback2" src="http://www.agileatwork.com/wp-content/uploads/2009/11/feedback2_thumb.png" width="165" height="123" /></p>
<p><strong>Figure A.5</strong> <em>Not enough time to test reduces the available time </em>    <br /><font size="2"><a href="http://www.amazon.com/Test-Driven-Development-Kent-Beck/dp/0321146530">Test-Driven Development: By Example</a> by Kent Beck</font></p>
<p>It didn’t start out that way. In fact the first few lines of code I wrote were tests. The problem was I could only get about an hour or two in each night and every day that passed we were leaving money on the table.</p>
<p>Since I didn’t have much time at night, I didn’t want to spend it writing tests. I just wanted to get the app done. All I needed to do was sync business listings from an affiliate with <a href="http://www.garagecommerce.com/">GarageCommerce.com</a>, the project I’ve been working on for the last few months. There were only a handful of business rules to deal with and I thought I could just bang it out.</p>
<p><img style="border-right-width: 0px; margin: 0px 15px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="district9-poster" border="0" alt="district9-poster" align="left" src="http://www.agileatwork.com/wp-content/uploads/2009/12/district9poster_thumb.jpg" width="160" height="240" /></p>
<p>I know what you’re thinking. You already know where this is headed and you might as well just skip to the end. It’s like in that movie District 9. It started out pretty good with some edgy social commentary but by the end of the movie it just phoned in a cliché Hollywood plot. You know, like “fish out of water”, “buddy movie”, or “trading places” as was the case with District 9.</p>
<p>Anyway, I finished the app and since I only had a few tests I figured I should trace through things a few times to look for obvious problems. After doing that for a few minutes I was ready to press F5 and start working the kinks out. So after hours of zero feedback coding, I took a deep breath and the started the app for the first time.</p>
<p>It promptly bombed out as expected. I worked through a handful of errors and then it was good to go. As I watched it run, I thought to myself that <a href="http://blog.stackoverflow.com/2009/02/podcast-41/">maybe Uncle Bob is wrong about testing and Spolsky isn’t such a douche bag after all</a>. However after processing a couple hundred jobs, it started throwing crazy errors in a tight loop. Crap!</p>
<p>Fast forward a few hours and I’ve got a hunch as to what is causing the problem. The way I wired up the app, I was creating a single NHibernate session for the entire batch of jobs. I knew this could be trouble so I at least made a point to commit after each job and clear the session so it wouldn’t get bogged down with 10K+ entities. I knew the single session was smelly and the errors I was seeing prompted me to refactor.</p>
<p><img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="MASH-tv-show-11" border="0" alt="MASH-tv-show-11" src="http://www.agileatwork.com/wp-content/uploads/2009/12/MASHtvshow11_thumb.jpg" width="240" height="163" /></p>
<p>This is where it got ugly. I needed to do a pretty major overhaul and I was mostly test-less. My blood pressure went way up with each change and the fact that I didn’t have tests just made me take larger steps with no safety net. I felt like a M.A.S.H. surgeon with guts all over the operating table. Part of me thought I should take a few steps back and regroup, but another part of me was reminded of this quote:</p>
<blockquote><p>If you’re going through hell, keep going. – Winston Churchill</p>
</blockquote>
<p>So I stuck with it and got ‘er done, but it wasn’t much fun. What did I take from this experience? No revelations really, just some common sense reinforcement. It’s like rediscovering that diet and exercise is the key to losing weight, except that I rediscovered that not having tests really sucks when you have to make changes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/maybe-uncle-bob-is-wrong-about-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Auto-Mocking Unity Container Extension</title>
		<link>http://www.agileatwork.com/auto-mocking-unity-container-extension/</link>
		<comments>http://www.agileatwork.com/auto-mocking-unity-container-extension/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 21:20:58 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.agileatwork.com/?p=119</guid>
		<description><![CDATA[Using a container for unit testing is a good way to insulate your tests from changes in object construction. Typically my first test will be something pretty boring just to get the process started. A few tests later, the real behavior comes out along with new dependencies. Rather than having to go back and add [...]]]></description>
			<content:encoded><![CDATA[<p>Using a container for unit testing is a good way to insulate your tests from changes in object construction. Typically my first test will be something pretty boring just to get the process started. A few tests later, the real behavior comes out along with new dependencies. Rather than having to go back and add the dependencies to all the tests I’ve already written, I like to use a container to build up the object I’m testing.</p>
<p>To streamline this process, I thought it would be handy to use a container extension to auto generate mocks for any required interface that wasn’t explicitly registered. The best way I can explain this is with code, so here it is:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">[<span style="color: #2b91af">SetUp</span>]</pre>
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">void</span> SetUp()</pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; container = <span style="color: blue">new</span> <span style="color: #2b91af">UnityContainer</span>()</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; .AddNewExtension&lt;<span style="color: #2b91af">AutoMockingContainerExtension</span>&gt;();</pre>
<pre style="margin: 0px">}</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">[<span style="color: #2b91af">Test</span>]</pre>
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">void</span> Should_be_really_easy_to_test()</pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; container</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; .RegisterMock&lt;<span style="color: #2b91af">IDependencyThatNeedsExplicitMocking</span>&gt;()</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Setup(d =&gt; d.MyMethod(<span style="color: #2b91af">It</span>.IsAny&lt;<span style="color: blue">int</span>&gt;()))</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Returns(<span style="color: #a31515">&quot;I want to specify the return value&quot;</span>);</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">var</span> service = container.Resolve&lt;<span style="color: #2b91af">ServiceWithThreeDependencies</span>&gt;();</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">var</span> result = service.DoSomething();</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: #2b91af">Assert</span>.That(result, <span style="color: #2b91af">Is</span>.EqualTo(<span style="color: #a31515">&quot;I didn't have to mock the other 2 dependencies!&quot;</span>));</pre>
<pre style="margin: 0px">}</pre>
<pre style="margin: 0px">&#160;</pre>
</div>
<p>It really reduces the noise level in tests and lets you focus on the interesting parts of your application. Here’s the code for the container extension:</p>
<p><img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="luigi" border="0" alt="luigi" src="http://www.agileatwork.com/wp-content/uploads/2009/06/luigi1.jpg" width="150" height="115" /></p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">AutoMockingContainerExtension</span> : <span style="color: #2b91af">UnityContainerExtension</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">void</span> Initialize()</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> strategy = <span style="color: blue">new</span> <span style="color: #2b91af">AutoMockingBuilderStrategy</span>(Container);</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; Context.Strategies.Add(strategy, <span style="color: #2b91af">UnityBuildStage</span>.PreCreation);</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">AutoMockingBuilderStrategy</span> : <span style="color: #2b91af">BuilderStrategy</span></pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IUnityContainer</span> container;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> AutoMockingBuilderStrategy(<span style="color: #2b91af">IUnityContainer</span> container)</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.container = container;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">void</span> PreBuildUp(<span style="color: #2b91af">IBuilderContext</span> context)</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> key = context.OriginalBuildKey;</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (key.Type.IsInterface &amp;&amp; !container.IsRegistered(key.Type))</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; context.Existing = CreateDynamicMock(key.Type);</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">object</span> CreateDynamicMock(<span style="color: #2b91af">Type</span> type)</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> genericMockType = <span style="color: blue">typeof</span>(<span style="color: #2b91af">Mock</span>&lt;&gt;).MakeGenericType(type);</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> mock = (<span style="color: #2b91af">Mock</span>)<span style="color: #2b91af">Activator</span>.CreateInstance(genericMockType);</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> mock.Object;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
<pre style="margin: 0px">&#160;</pre>
</div>
<p>In case you’re wondering about <font size="2" face="Courier New">container.RegisterMock&lt;&gt;</font>, it’s just extension method that you can read about <a href="http://www.agileatwork.com/moq-extension-methods-for-unity/">here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/auto-mocking-unity-container-extension/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Captcha and Inversion of Control</title>
		<link>http://www.agileatwork.com/captcha-and-inversion-of-control/</link>
		<comments>http://www.agileatwork.com/captcha-and-inversion-of-control/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 06:09:18 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Inversion of Control]]></category>
		<category><![CDATA[Single Responsibility Principle]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.agileatwork.com/?p=98</guid>
		<description><![CDATA[
I made a few tweaks to a captcha library I found here and basically wrapped their CaptchaImage object in a service interface to use in my application. Pretty easy stuff, but I didn’t get it right the first time.
What’s wrong with this class? (hint: it’s highlighted in yellow)
public class HttpContextCacheCaptchaProvider : ICaptchaProvider
{
    [...]]]></description>
			<content:encoded><![CDATA[<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Inversion of Control" border="0" alt="Inversion of Control" src="http://www.agileatwork.com/wp-content/uploads/2009/06/lorenzoflip.jpg" width="240" height="159" /></p>
<p>I made a few tweaks to a captcha library I found <a href="http://www.codeproject.com/KB/aspnet/CaptchaImage.aspx">here</a> and basically wrapped their <font size="2" face="Courier New">CaptchaImage</font> object in a service interface to use in my application. Pretty easy stuff, but I didn’t get it right the first time.</p>
<p>What’s wrong with this class? <font size="2"><em>(hint: it’s highlighted in yellow)</em></font></p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> HttpContextCacheCaptchaProvider : ICaptchaProvider
{
    <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">string</span> httpContextCacheKey = <span class="str">&quot;4D795A45-8015-475C-A6C4-765B09EB9955&quot;</span>;
    <span class="kwrd">private</span> ICaptchaImageFactory factory;
    <span class="kwrd">private</span> HttpContextBase context;

    <span class="kwrd">public</span> HttpContextCacheCaptchaProvider(ICaptchaImageFactory factory, HttpContextBase context)
    {
        <span class="kwrd">this</span>.factory = factory;
        <span class="kwrd">this</span>.context = context;
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> Render(Stream stream)
    {
        CaptchaImage captchaImage = factory.Create();

        <span style="background: #ffffc0">context.Cache[httpContextCacheKey] = captchaImage.Text;</span>

        <span class="kwrd">using</span> (Bitmap bitmap = captchaImage.RenderImage())
        {
            bitmap.Save(stream, ImageFormat.Jpeg);
        }
    }

    <span class="kwrd">public</span> <span class="kwrd">bool</span> Verify(<span class="kwrd">string</span> text)
    {
        <span style="background: #ffffc0"><span class="kwrd">string</span> captchaText = (<span class="kwrd">string</span>)context.Cache[httpContextCacheKey];</span>

        <span class="kwrd">if</span> (text == <span class="kwrd">null</span> || captchaText == <span class="kwrd">null</span>)
            <span class="kwrd">return</span> <span class="kwrd">false</span>;
        <span class="kwrd">else</span>
            <span class="kwrd">return</span> text.ToLower().Equals(captchaText.ToLower());
    }
}</pre>
<p>Perhaps your nose can lead you in the right direction. The smell is <a href="http://c2.com/cgi/wiki?CodeSmellMetrics">hard to test</a>, and it’s hard to test because it requires mocking the <font size="2" face="Courier New">HttpContextBase</font>. You might say “no problem, I can blast out a stub with <a href="http://code.google.com/p/moq/">Moq</a> in no time” but you’re missing the real problem. That would be like taking aspirin for a brain tumor.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="arnold" border="0" alt="arnold" src="http://www.agileatwork.com/wp-content/uploads/2009/06/arnold.jpg" width="208" height="241" /></p>
<blockquote>
<p>It’s not a tumor</p>
</blockquote>
<p>The real problem is this class is violating the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a> (SRP) by doing more than one thing. I don’t mean the two methods <font size="2" face="Courier New">Render()</font> and <font size="2" face="Courier New">Verify()</font>, those are a cohesive unit operating on the same data. The other thing is <em>lifetime management</em>. Look how simple the class gets when you invert control and take out the notion of lifetime management:</p>
<pre class="csharpcode">[Serializable]
<span class="kwrd">public</span> <span class="kwrd">class</span> LifetimeManagedCaptchaProvider : ICaptchaProvider
{
    <span class="kwrd">private</span> ICaptchaImageFactory factory;
    <span class="kwrd">private</span> <span class="kwrd">string</span> captchaText;

    <span class="kwrd">public</span> LifetimeManagedCaptchaProvider(ICaptchaImageFactory factory)
    {
        <span class="kwrd">this</span>.factory = factory;
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> Render(Stream stream)
    {
        CaptchaImage captchaImage = factory.Create();

        <span style="background: #ffffc0">captchaText = captchaImage.Text;</span>

        <span class="kwrd">using</span> (Bitmap bitmap = captchaImage.RenderImage())
        {
            bitmap.Save(stream, ImageFormat.Jpeg);
        }
    }

    <span class="kwrd">public</span> <span class="kwrd">bool</span> Verify(<span class="kwrd">string</span> text)
    {
        <span class="kwrd">if</span> (text == <span class="kwrd">null</span> || captchaText == <span class="kwrd">null</span>)
            <span class="kwrd">return</span> <span class="kwrd">false</span>;
        <span class="kwrd">else</span>
            <span class="kwrd">return</span> text.ToLower().Equals(captchaText.ToLower());
    }
}</pre>
<p>Now this class is a breeze to test and all you have to do is register it like this:</p>
<pre class="csharpcode">Container.RegisterType&lt;ICaptchaProvider, LifetimeManagedCaptchaProvider&gt;(
    <span class="kwrd">new</span> HttpSessionStateLifetimeManager());</pre>
<p>The moral of the story is let your IoC container do things it’s good at.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/captcha-and-inversion-of-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don’t Eat a Donut on the Way Home From the Gym</title>
		<link>http://www.agileatwork.com/dont-eat-a-donut-on-the-way-home-from-the-gym/</link>
		<comments>http://www.agileatwork.com/dont-eat-a-donut-on-the-way-home-from-the-gym/#comments</comments>
		<pubDate>Mon, 18 May 2009 07:59:52 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://blog.agileatwork.com/?p=19</guid>
		<description><![CDATA[
I must warn you, this post is about unit testing software, not donuts. With that said, I’ve been pondering the dilemma of how much time to spend writing unit tests and today I had a moment of clarity that I couldn’t help but share.
I was pairing with a coworker on Friday and we were both [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.agileatwork.com/wp-content/uploads/2009/05/homer-donut.jpg"><img style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin-left: 0px; margin-right: 0px; border-right-width: 0px" title="homer_donut" src="http://blog.agileatwork.com/wp-content/uploads/2009/05/homer-donut-thumb.jpg" border="0" alt="homer_donut" width="240" height="240" align="right" /></a></p>
<p>I must warn you, this post is about unit testing software, not donuts. With that said, I’ve been pondering the dilemma of how much time to spend writing unit tests and today I had a moment of clarity that I couldn’t help but share.</p>
<p>I was pairing with a coworker on Friday and we were both feeling a bit unproductive because we had spent so much time writing unit tests for a relatively small bit of code. In fact we spent <em>more</em> time writing tests than we spent writing the code to make the tests pass. To make matters worse, we spent<em> more time refactoring</em> the unit tests than we did refactoring the code that made the tests pass. We were pining over the readability of the tests as if the test were more important than the code and that left me with an uneasy feeling over the weekend.</p>
<p>Today I found a bit of inner peace as I embraced the the notion that the tests <em>are</em> more important than the code that makes them pass. Think about that for a moment. The challenge in writing software is not the implementation. Syntax, structures and algorithms are the easy parts. The real hard earned knowledge won through experience comes in the form of specifications; Understanding the intricate complexities and desired behavior of your software in the wild.</p>
<p>Writing software can be like swimming in the ocean when a thick fog rolls in. The mechanics of swimming are learned easily, but the real challenge is knowing which direction to swim so that you get back to the shore before you run out of energy. After a day of programming, the real asset is not the implementation of your feature, it’ s the increased understanding of your problem domain. This understanding is captured in the form of executable requirements.</p>
<p>When there is a bug in your system, chances are it’s a bug in your understanding of how your system should behave under a particular set of conditions. Burying an innocuous if statement in the middle of some method deep in the stack is a horrible way to reap the reward of day spent spelunking through code. It’s like eating a donut on the way home from the gym.</p>
<p>The legacy you leave is the the unit test, it tells the story of the hard fought knowledge and the readability of your test is more important than the readability of your implementation. Tell the next developer what’s important through an executable specification that reads like one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/dont-eat-a-donut-on-the-way-home-from-the-gym/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>5 Ways to Fail at Pair Programming</title>
		<link>http://www.agileatwork.com/5-ways-to-fail-at-pair-programming-2/</link>
		<comments>http://www.agileatwork.com/5-ways-to-fail-at-pair-programming-2/#comments</comments>
		<pubDate>Sun, 10 May 2009 23:57:00 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Pair Programming]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://wordpress.localhost/5-ways-to-fail-at-pair-programming-2/</guid>
		<description><![CDATA[&#160;
The only hard part is convincing your boss that it’s not a colossal waste of time, right? Well that’s the first hurdle and yes it’s a doosie, but it’s just the price of admission. The real fun starts when you sit next to your partner and try to prove your boss wrong. After a year [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://lh6.ggpht.com/_-EvyswebbLw/ShJEibBWfmI/AAAAAAAAB-I/YMyPMtF41cw/s1600-h/shoes%5B3%5D.jpg"><img title="shoes" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin-left: 0px; margin-right: 0px; border-right-width: 0px" height="240" alt="shoes" src="http://lh5.ggpht.com/_-EvyswebbLw/ShJEixabpEI/AAAAAAAAB-M/_Bb4BTtx-x4/shoes_thumb%5B2%5D.jpg?imgmax=800" width="160" border="0" /></a>&#160;</p>
<p>The only hard part is convincing your boss that it’s not a colossal waste of time, right? Well that’s the first hurdle and yes it’s a doosie, but it’s just the price of admission. The real fun starts when you sit next to your partner and try to prove your boss wrong. After a year or so in the trenches, I’ve learned a thing or two on how to mess it up.</p>
<p><strong>5. Researching new technology</strong></p>
<p>“Click on that link, no wait scroll up, over there, wait I wasn’t done reading that…” That’s not pair programming, that’s insanity. Recently we were working on a project that involved a custom Firefox browser skin. Neither of us had experience with browser skins, so there was a lot of Googling and tutorial reading. As soon as we caught ourselves reading blogs and whatnot, we would split up for 15 minutes and then compare notes. </p>
<p><strong>4. Getting your environment going</strong></p>
<p>“Hey buddy, are you ready to start pairing on that project? Let’s see, now where is the source code for the project we’re working on… Hmm, this doesn’t compile – I think I need to install the latest version of that library.” Kill me now.</p>
<p><strong>3. Paralysis</strong></p>
<p><a href="http://lh6.ggpht.com/_-EvyswebbLw/ShJF_VXWl7I/AAAAAAAAB-U/Y3RQUp11Gvk/s1600-h/FrightenedMan400%5B2%5D.jpg"><img title="FrightenedMan400" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin-left: 0px; margin-right: 0px; border-right-width: 0px" height="240" alt="FrightenedMan400" src="http://lh5.ggpht.com/_-EvyswebbLw/ShJF_00DQrI/AAAAAAAAB-Y/kpkPxVz00FU/FrightenedMan400_thumb%5B1%5D.jpg?imgmax=800" width="172" align="right" border="0" /></a> </p>
<p>One of my favorite authors, Kurt Vonnegut would write clever things like “The man looked… guilty isn’t the right word, but it’s the first one that comes to mind.”</p>
<p>With pair programming, you can’t just sit there because you don’t know where to start or you can’t think of the right variable name. Just type something. You have to get the problem solving out of your head and into a medium that two people can have a conversation about it. Maybe people are afraid to type something in front of a peer if it’s not perfect. Borrow a technique from Kurt Vonnegut and as you are type, just say “I don’t want the code to look like this…” and write some awful switch case statement.</p>
<p>Once there is something on the screen, you and your partner are instantly aligned and solving the same problem. Now you can discuss factories and polymorphism and all kinds of heady solutions.</p>
<p><strong>2. Not doing TDD</strong></p>
<p>Have you ever watched an artist paint a picture and thought to yourself “what the heck is that”. Then with the next brush stroke you realize you’re looking at the profile of someone’s face. Well watching someone write code can be horribly worse than that. </p>
<p>Pair programming is a conversation, not a seminar or window into someone’s brain. The best way to keep things at a conversational level is to work top down which is exactly what TDD forces you to do. You must consider your code from the client perspective and challenge yourself to keep your code on purpose.</p>
<p>Even more importantly, TDD gives you and your buddy an “out” at regular intervals. You write a test, you make a test pass. Either way, you have many small victories over the course of a few hours and you can switch pairs at well defined feel-good stopping points.</p>
<p><strong>1. Not using a timer</strong></p>
<p><a href="http://lh5.ggpht.com/_-EvyswebbLw/ShJOiD26ccI/AAAAAAAAB-w/CGsxfum9Pf8/s1600-h/pprobot%5B11%5D.jpg"><img title="pprobot" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 10px 0px 0px; border-right-width: 0px" height="200" alt="pprobot" src="http://lh3.ggpht.com/_-EvyswebbLw/ShJOin5m-rI/AAAAAAAAB-0/q4MrukbBVTY/pprobot_thumb%5B7%5D.jpg?imgmax=800" width="162" align="left" border="0" /></a> I used to work with a guy that was always competing with something. A mutual friend at the office was doing the 3-day breast cancer walk. If anyone in the office donated money, he would up his donation $1 higher. On the softball team, his jersey number was #1. When we started playing ping-pong at lunch, he bought a $600 ping-pong robot to practice with at home. So when he started using a timer while programming I just figured it was good old Paul racing against time.</p>
<p>Then I paired with him on a project. Right after wanting to strangle him for not having his environment ready, we set his timer for 30 minutes and got going. When the timer went off, we stopped mid keystroke and switched seats. This did a few things. First, it kept Paul engaged the whole time because he knew he would be in the hot seat in a few minutes, and I can be a bit of a bully so it was easier for me to back off knowing I’d get my chance soon enough. One of the more surprising benefits, however, was simply the fact that we would sit for a minute and agree exactly what we were doing before starting the timer which really got us in gear and drastically cut down on tangents.</p>
<p>Using a timer creates a magical dynamic that I cannot do justice with words. It’s like TDD, it’s not about the tests – it’s about the code you write to make it testable. You have to try it. Period.</p>
<blockquote><p>“If you can do a half-assed job of anything, you&#8217;re a one-eyed man in a kingdom of the blind.” – Kurt Vonnegut</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/5-ways-to-fail-at-pair-programming-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

