<?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"
	>

<channel>
	<title>delphiXtreme</title>
	<atom:link href="http://delphixtreme.com/wordpress/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://delphixtreme.com/wordpress</link>
	<description>A hub for Agile techniques and tools in Delphi programming</description>
	<pubDate>Fri, 13 Feb 2009 15:46:04 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>GUI testing with DUnit</title>
		<link>http://delphixtreme.com/wordpress/?p=181</link>
		<comments>http://delphixtreme.com/wordpress/?p=181#comments</comments>
		<pubDate>Mon, 02 Feb 2009 16:48:29 +0000</pubDate>
		<dc:creator>Jody Dawkins</dc:creator>
		
		<category><![CDATA[XP]]></category>

		<category><![CDATA[dunit]]></category>

		<category><![CDATA[engineering]]></category>

		<category><![CDATA[GUI]]></category>

		<category><![CDATA[TDD]]></category>

		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://delphixtreme.com/wordpress/?p=181</guid>
		<description><![CDATA[I don&#8217;t how I didn&#8217;t know this until now but it turns out there&#8217;s a whole section of DUnit designed to help you test GUI elements. What follows is small write up about how to do it. 
I started with a dead simple app. Type your text into the edit box, click the button and [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t how I didn&#8217;t know this until now but it turns out there&#8217;s a whole section of DUnit designed to help you test GUI elements. What follows is small write up about how to do it. <span id="more-181"></span></p>
<p>I started with a dead simple app. Type your text into the edit box, click the button and the text shown in a label&#8217;s caption:</p>
<div align="center"><div id="attachment_186" class="wp-caption alignnone" style="width: 310px"><a href="http://delphixtreme.com/wordpress/wp-content/uploads/2009/01/picture-6.png"><img src="http://delphixtreme.com/wordpress/wp-content/uploads/2009/01/picture-6-300x87.png" alt="GUI for testing" title="GUI for testing" width="300" height="87" class="size-medium wp-image-186" /></a><p class="wp-caption-text">GUI for testing</p></div></div>
<p>So I started with a TGUITestCase descendant:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">type</span>
  MainFormTests <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #000066;">&#40;</span>TGUITestCase<span style="color: #000066;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">protected</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> SetUp<span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">override</span><span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> TearDown<span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">override</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">published</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> Hookup<span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>DUnit runs the SetUp and TearDown methods before and after each test method. <del datetime="2009-02-13T15:45:23+00:00">This is different than NUnit and other unit testing frameworks which call SetUp and TearDown once per test suite execution.</del> So if in the SetUp we start the application and in the TearDown we close it then we will be starting and stopping the app for every test. This is undesirable in this type of testing since we will be changing state in application. Additionally it might slow the tests down to have to perform the application startup and shutdown over and over again. </p>
<p>To deal with this problem we&#8217;ll use a class from the TestExtensions unit called TTestSetup. TTestSetup is a decorator for a TTestCase. This means that you implement behavior before and after tests run &#8230; like setup and teardown code that should only run once. To get this done you need two things: 1) a subclass of TTestSetup in which you&#8217;ll do your one time setup and teardown and 2) do a little different test registration. Here&#8217;s the TTestSetup class:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">uses</span> TestFramework<span style="color: #000066;">,</span> TestExtensions<span style="color: #000066;">,</span> GUITesting<span style="color: #000066;">,</span> fMain<span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">type</span>
  MainFormSetup <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #000066;">&#40;</span>TTestSetup<span style="color: #000066;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span>
    FMainForm <span style="color: #000066;">:</span> TForm1<span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">protected</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> SetUp<span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">override</span><span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> TearDown<span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">override</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>You can see here the main form reference for our test app and the SetUp and TearDown that will happen once. Next let&#8217;s see the TGUITestCase and the implementation code:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;">  MainFormTests <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #000066;">&#40;</span>TGUITestCase<span style="color: #000066;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">protected</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> TearDown<span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">override</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">published</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> Hookup<span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> TestChangeEditText<span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> TestButtonClickWithText<span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> TestButtonClickWithNoText<span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> TestButtonClickWithOnlyWhiteSpace<span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">implementation</span>
&nbsp;
<span style="color: #808080; font-style: italic;">{ MainFormSetUp }</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> MainFormSetUp<span style="color: #000066;">.</span><span style="color: #006600;">SetUp</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  FMainForm <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TForm1<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span>Application<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  FMainForm<span style="color: #000066;">.</span><span style="color: #006600;">Show</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">inherited</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> MainFormSetUp<span style="color: #000066;">.</span><span style="color: #006600;">TearDown</span><span style="color: #000066;">;</span> 
<span style="color: #000000; font-weight: bold;">begin</span> 
  FMainForm<span style="color: #000066;">.</span><span style="color: #006600;">Release</span><span style="color: #000066;">;</span>
  FMainForm<span style="color: #000066;">.</span><span style="color: #006600;">Free</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">inherited</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">{ MainFormTests }</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> MainFormTests<span style="color: #000066;">.</span><span style="color: #006600;">Hookup</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  Check<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">True</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> MainFormTests<span style="color: #000066;">.</span><span style="color: #006600;">TearDown</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  GUI <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">inherited</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> MainFormTests<span style="color: #000066;">.</span><span style="color: #006600;">TestButtonClickWithNoText</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  lbl<span style="color: #000066;">:</span> TLabel<span style="color: #000066;">;</span>
  LabelText<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  RunGUITestSequence<span style="color: #000066;">&#40;</span>LabelText<span style="color: #000066;">,</span> lbl<span style="color: #000066;">,</span> <span style="color: #ff0000;">''</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  CheckEquals<span style="color: #000066;">&#40;</span>LabelText<span style="color: #000066;">,</span> lbl<span style="color: #000066;">.</span><span style="color: #006600;">Caption</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> MainFormTests<span style="color: #000066;">.</span><span style="color: #006600;">TestButtonClickWithOnlyWhiteSpace</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  lbl <span style="color: #000066;">:</span> TLabel<span style="color: #000066;">;</span>
  LabelText <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  RunGUITestSequence<span style="color: #000066;">&#40;</span>LabelText<span style="color: #000066;">,</span> lbl<span style="color: #000066;">,</span> <span style="color: #ff0000;">'    '</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  CheckEquals<span style="color: #000066;">&#40;</span>LabelText<span style="color: #000066;">,</span> lbl<span style="color: #000066;">.</span><span style="color: #006600;">Caption</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> MainFormTests<span style="color: #000066;">.</span><span style="color: #006600;">RunGUITestSequence</span><span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> LabelText<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">var</span> lbl<span style="color: #000066;">:</span> TLabel<span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">const</span> TestValue <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  Edit<span style="color: #000066;">:</span> TEdit<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  lbl <span style="color: #000066;">:</span><span style="color: #000066;">=</span> FindControl<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Label1'</span><span style="color: #000066;">&#41;</span> <span style="color: #000000; font-weight: bold;">as</span> TLabel<span style="color: #000066;">;</span>
  LabelText <span style="color: #000066;">:</span><span style="color: #000066;">=</span> lbl<span style="color: #000066;">.</span><span style="color: #006600;">Caption</span><span style="color: #000066;">;</span>
  Edit <span style="color: #000066;">:</span><span style="color: #000066;">=</span> FindControl<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Edit1'</span><span style="color: #000066;">&#41;</span> <span style="color: #000000; font-weight: bold;">as</span> TEdit<span style="color: #000066;">;</span>
  Edit<span style="color: #000066;">.</span><span style="color: #006600;">Text</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TestValue<span style="color: #000066;">;</span>
  Click<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Button1'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> MainFormTests<span style="color: #000066;">.</span><span style="color: #006600;">TestChangeEditText</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  Edit<span style="color: #000066;">:</span> TEdit<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  Edit <span style="color: #000066;">:</span><span style="color: #000066;">=</span> FindControl<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Edit1'</span><span style="color: #000066;">&#41;</span> <span style="color: #000000; font-weight: bold;">as</span> TEdit<span style="color: #000066;">;</span>
  Edit<span style="color: #000066;">.</span><span style="color: #006600;">Text</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #ff0000;">'I like pizza'</span><span style="color: #000066;">;</span>
  CheckEquals<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'I like pizza'</span><span style="color: #000066;">,</span> Edit<span style="color: #000066;">.</span><span style="color: #006600;">Text</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> MainFormTests<span style="color: #000066;">.</span><span style="color: #006600;">TestButtonClickWithText</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  lbl<span style="color: #000066;">:</span> TLabel<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  Click<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Button1'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  lbl <span style="color: #000066;">:</span><span style="color: #000066;">=</span> FindControl<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Label1'</span><span style="color: #000066;">&#41;</span> <span style="color: #000000; font-weight: bold;">as</span> TLabel<span style="color: #000066;">;</span>
  CheckEquals<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'I like pizza'</span><span style="color: #000066;">,</span> lbl<span style="color: #000066;">.</span><span style="color: #006600;">Caption</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">initialization</span>
  RegisterTest<span style="color: #000066;">&#40;</span>MainFormSetUp<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span>MainFormTests<span style="color: #000066;">.</span><span style="color: #006600;">Suite</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span></pre></div></div>

<p>With the test registration above, the DUnit test tree looks a little different:</p>
<div align="center"><div id="attachment_190" class="wp-caption alignnone" style="width: 310px"><a href="http://delphixtreme.com/wordpress/wp-content/uploads/2009/01/picture-1.png"><img src="http://delphixtreme.com/wordpress/wp-content/uploads/2009/01/picture-1-300x171.png" alt="DUnit test tree with setup decorator" title="DUnit test tree with setup decorator" width="300" height="171" class="size-medium wp-image-190" /></a><p class="wp-caption-text">DUnit test tree with setup decorator</p></div></div>
<p><strong>Now for the fun part.</strong></p>
<p>You take these tests and recreate my test application. Comment below about how it went and what questions, if any, you had.</p>
]]></content:encoded>
			<wfw:commentRss>http://delphixtreme.com/wordpress/?feed=rss2&amp;p=181</wfw:commentRss>
		</item>
		<item>
		<title>The value of knowing value</title>
		<link>http://delphixtreme.com/wordpress/?p=171</link>
		<comments>http://delphixtreme.com/wordpress/?p=171#comments</comments>
		<pubDate>Mon, 19 Jan 2009 17:43:36 +0000</pubDate>
		<dc:creator>Jody Dawkins</dc:creator>
		
		<category><![CDATA[XP]]></category>

		<category><![CDATA[philosophy]]></category>

		<guid isPermaLink="false">http://delphixtreme.com/wordpress/?p=171</guid>
		<description><![CDATA[Figuring out what to do next or even where to start is alway a challenge. 
Scrum and XP both have an iteration planning method in which the team gives each unit of work (story) an estimation of effort. We typically call these story points. Story points are essentially an order of complexity against a baseline [...]]]></description>
			<content:encoded><![CDATA[<p>Figuring out what to do next or even where to start is alway a challenge. </p>
<p>Scrum and XP both have an iteration planning method in which the team gives each unit of work (story) an estimation of effort. We typically call these story points. Story points are essentially an order of complexity against a baseline story.</p>
<p>The typical format is the Product Owner brings stories to the Team for estimation. The Product Owner and Team talk about each story until the Team feels comfortable giving an estimate. In the beginning of the iteration the Team decides how many story points they can get done and the Product Owner lines up the stories in priority order/ The team then pulls into the iteration as many stories as will fit based on the sum of the story points.</p>
<p>Teams will carry on like this using the sum of the story points they complete in each iteration as a measure of their productivity. Increasing this measure of productivity, called Velocity, is supposed to be a sign of a team getting better and delivering more business value each iteration. This is where I think we mess up: equating velocity with business value.</p>
<p><span id="more-171"></span></p>
<p>Why is equating Velocity with Business Value bad? I think we can draw the same distinction here as we draw between Activity and Action. Velocity is a measure of effort or cost. I say it will cost 5 times as many story points to complete story B as it does story A. So story B is more complex and will take more time to complete than story A. We presume that the stories on which we&#8217;re working are of the highest business value as compared to the stories remaining. As such we presume that if our Velocity goes up then we&#8217;re providing more Business Value.</p>
<p>This may be the case, but what if we could do even better? What if we could say that story A has a certain amount of Business Value points and story B also has a certain amount. By this I mean a number: 4 Business Value points or 10 Business Value Points. Lets look at that.</p>
<table>
<tr>
<td></td>
<td>Story Points</td>
<td>Business Value Points</td>
</tr>
<tr>
<td>Story A</td>
<td>5</td>
<td>9</td>
</tr>
<tr>
<td>Story B</td>
<td>8</td>
<td>7</td>
</tr>
<tr>
<td>Story C</td>
<td>1</td>
<td>6</td>
</tr>
<tr>
<td>Story D</td>
<td>3</td>
<td>5</td>
</tr>
</table>
<p>Here we have four stories ordered by Business Value points. This might seem like a reasonable ordering. In fact it seem like there&#8217;s no point in the Business Value column if all you&#8217;re doing is prioritizing by it.</p>
<p>For some time the world has had a method for calculating ones return on investment. We divide the value by the cost to get the return on investment. It&#8217;s a simple calculation used to bring order out of chaos. Let&#8217;s apply it to the above table and order by the resulting ROI numbers.</p>
<table>
<tr>
<td></td>
<td>Story Points</td>
<td>Business Value Points</td>
<td>ROI</td>
</tr>
<tr>
<td>Story C</td>
<td>1</td>
<td>6</td>
<td>6</td>
</tr>
<tr>
<td>Story A</td>
<td>5</td>
<td>9</td>
<td>1.8</td>
</tr>
<tr>
<td>Story D</td>
<td>3</td>
<td>5</td>
<td>1.67</td>
</tr>
<tr>
<td>Story B</td>
<td>8</td>
<td>7</td>
<td>0.88</td>
</tr>
</table>
<p>Now we get a slightly different ordering. Why would you care about this? Using the ROI calculation you minimize your effort and maximize your value delivery. </p>
<p>You might be looking at this thinking &#8220;Well, it&#8217;s all got to get done anyway so the order isn&#8217;t really important&#8221;. To that I would say you&#8217;ve taken your first step on the <a target="_blank" href="http://en.wikipedia.org/wiki/Death_march_(software_development)">death march</a> and I don&#8217;t envy that sort of thinking. Now in this time of economic trouble more than ever we need to concentrate on delivering more business value faster and with less effort than ever before.</p>
<p>On that note I&#8217;ll wrap up with a link to a wonderful techbiz parable: <strong><a target="_blank" href="http://www.rocksintogold.com/">Rocks into gold</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://delphixtreme.com/wordpress/?feed=rss2&amp;p=171</wfw:commentRss>
		</item>
		<item>
		<title>RE: Architectural Hedges</title>
		<link>http://delphixtreme.com/wordpress/?p=165</link>
		<comments>http://delphixtreme.com/wordpress/?p=165#comments</comments>
		<pubDate>Wed, 07 Jan 2009 18:54:21 +0000</pubDate>
		<dc:creator>Jody Dawkins</dc:creator>
		
		<category><![CDATA[XP]]></category>

		<category><![CDATA[engineering]]></category>

		<category><![CDATA[philosophy]]></category>

		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://delphixtreme.com/wordpress/?p=165</guid>
		<description><![CDATA[Craig Stuntz recently posed the question of how to build hedges into our products and processes.
If I understand the problem correctly what we want is a way to mitigate the risk of choosing the wrong technology or technique. This is to say if we do choose the wrong technology or technique (for whatever reason) how [...]]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" href="http://blogs.teamb.com/craigstuntz/">Craig Stuntz</a> recently posed the question of <a target="_blank" href="http://blogs.teamb.com/craigstuntz/2009/01/06/37897/">how to build hedges into our products and processes</a>.</p>
<p>If I understand the problem correctly what we want is a way to mitigate the risk of choosing the wrong technology or technique. This is to say if we do choose the wrong technology or technique (for whatever reason) how do we recover and change direction with a minimal amount of fuss and cost. </p>
<p>I think the biggest guiding princple we have for this is Deferred Implmentation. Deferred Implementation demands implementation agnosticism from the client code. We&#8217;ll use the User Repository code from the post <a target="_blank" href="http://delphixtreme.com/wordpress/?p=85">Keep you code lazy and ignorant</a> as an example. When we wrote the code that needed to look up a user by the user name the client code was ignorant of how the data got there. This was good because it meant we could focus on the business  logic of logging in and leave the details of how retrieve user information for later. All we needed to supply was an API for the client code to use. Later, <a target="_blank" href="http://delphixtreme.com/wordpress/?p=139">in the next post</a>, we wrote the code to retrieve the user data using an ADO connection to a SQL database. The truth is we could have used any storage medium like an XML file or web service. Either way the client code didn&#8217;t change because of the implementation details in the User Repository. Doing this created a hedge in both the barrier sense and the risk mitigation sense.</p>
<p>TDD played a big role in getting to the deferred implementation of the User Repository. We were able to quickly put together a test bed for our ideas and keep it to be sure we didn&#8217;t inadvertently break anything. TDD also gave us a formula for the incremental improvement of the code.</p>
<p>TDD by itself <a target="_blank" href="http://delphixtreme.com/wordpress/?p=57">didn&#8217;t get the job done</a>. We needed to be conscious of separation of concerns, not mixing object creation with business logic and design patterns that help us solve these sorts of problems. </p>
<p>I hope this addressed the question Craig posed. If not, I hope this was at least informative.</p>
]]></content:encoded>
			<wfw:commentRss>http://delphixtreme.com/wordpress/?feed=rss2&amp;p=165</wfw:commentRss>
		</item>
		<item>
		<title>The big cup problem</title>
		<link>http://delphixtreme.com/wordpress/?p=161</link>
		<comments>http://delphixtreme.com/wordpress/?p=161#comments</comments>
		<pubDate>Mon, 29 Dec 2008 16:52:04 +0000</pubDate>
		<dc:creator>Jody Dawkins</dc:creator>
		
		<category><![CDATA[What we need]]></category>

		<category><![CDATA[XP]]></category>

		<category><![CDATA[philosophy]]></category>

		<guid isPermaLink="false">http://delphixtreme.com/wordpress/?p=161</guid>
		<description><![CDATA[It&#8217;s a strange thing about a big cup &#8230; or any large container for that matter. When you see it empty your immediate response is to fill it up. What can I put in here? There&#8217;s still some room in here, how can I use it? When you pull out a cup from your cupboard [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s a strange thing about a big cup &#8230; or any large container for that matter. When you see it empty your immediate response is to fill it up. What can I put in here? There&#8217;s still some room in here, how can I use it? When you pull out a cup from your cupboard to take a drink you almost always fill it up.</p>
<p>I&#8217;ve found myself lately writing code to solve a problem, looking at the volume of code and being upset over how little of it there is. I think, &#8220;Wow, I really wanted to solve a big problem. This code works but I can&#8217;t use it because there&#8217;s not enough here to match up with the magnitude of the problem!&#8221;. It&#8217;s like buying expensive software that comes in just a CD case. We shell out big bucks and only get a CD in a jewel case? No, no, no; I want to feel like I got my moneys worth!</p>
<p>How often do we do that? We start with this big cup and try to fill it up. We write more code than necessary,more tables than needed, more infrastructure, more text in the blog post, more, more, more. </p>
<p>The problem isn&#8217;t the work or the stuff; it&#8217;s the cup. We start with a big cup and try to fill it. Start with a small cup. If it overflows then great! Get a bigger cup.</p>
<p>It&#8217;s okay to get stuff done with less. Less code, less effort, fewer hours. Scale it back. Pare it down. Do just the bare essentials and do them exceeding well.</p>
<p>Heres to a new year of smaller cups. Happy new year, all!</p>
]]></content:encoded>
			<wfw:commentRss>http://delphixtreme.com/wordpress/?feed=rss2&amp;p=161</wfw:commentRss>
		</item>
		<item>
		<title>Get faster with FitNesse</title>
		<link>http://delphixtreme.com/wordpress/?p=139</link>
		<comments>http://delphixtreme.com/wordpress/?p=139#comments</comments>
		<pubDate>Mon, 01 Dec 2008 22:51:27 +0000</pubDate>
		<dc:creator>Jody Dawkins</dc:creator>
		
		<category><![CDATA[XP]]></category>

		<category><![CDATA[acceptance testing]]></category>

		<category><![CDATA[fitnesse]]></category>

		<guid isPermaLink="false">http://delphixtreme.com/wordpress/?p=139</guid>
		<description><![CDATA[This is the third article in a series beginning with Busting the great TDD myth and Keep your code ignorant and lazy. If you haven&#8217;t already read these articles I recommend checking them out as I will continue to use the same code as an example.
In the last article we looked at using the Provider [...]]]></description>
			<content:encoded><![CDATA[<p>This is the third article in a series beginning with <strong><a href="http://delphixtreme.com/wordpress/?p=57">Busting the great TDD myth</a></strong> and <strong><a href="http://delphixtreme.com/wordpress/?p=85">Keep your code ignorant and lazy</a></strong>. If you haven&#8217;t already read these articles I recommend checking them out as I will continue to use the same code as an example.</p>
<p>In the last article we looked at using the Provider pattern to make a flexible data access layer. It allowed us to test our TUserRepository in isolation even though it is dependent on data. Lets breifly review that code:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">type</span>
  ILoadable <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">interface</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> AddRecord<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName<span style="color: #000066;">,</span> Password <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
  ILoader <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">interface</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> Load<span style="color: #000066;">&#40;</span>Target <span style="color: #000066;">:</span> ILoadable<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span> 
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
  TUserRepository <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #000066;">&#40;</span>TInterfacedObject<span style="color: #000066;">,</span> ILoadable<span style="color: #000066;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span>
    FUsers <span style="color: #000066;">:</span> TStringList<span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">protected</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> AddRecord<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName<span style="color: #000066;">,</span> Password <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span>
    <span style="color: #000000; font-weight: bold;">constructor</span> Create<span style="color: #000066;">&#40;</span>Loader <span style="color: #000066;">:</span> ILoader<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">destructor</span> Destroy<span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">override</span><span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">function</span> Lookup<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> TUser<span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
  TTestData <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #000066;">&#40;</span>TInterfacedObject<span style="color: #000066;">,</span> ILoader<span style="color: #000066;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">protected</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> Load<span style="color: #000066;">&#40;</span>Target <span style="color: #000066;">:</span> ILoadable<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>By having TUserRepository implement ILoadable and creating a stub for loading test data called TTestData we were able to write a test like this:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">procedure</span> TUserRepositoryTests<span style="color: #000066;">.</span><span style="color: #006600;">SetUp</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  TestData <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TTestData<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">;</span>
  FRepo <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TUserRepository<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span>TestData<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> TUserRepositoryTests<span style="color: #000066;">.</span><span style="color: #006600;">TestValidLookup</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  user <span style="color: #000066;">:</span> TUser<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  user <span style="color: #000066;">:</span><span style="color: #000066;">=</span> FRepo<span style="color: #000066;">.</span><span style="color: #006600;">Lookup</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'fred'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  CheckEquals<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'flinstone'</span><span style="color: #000066;">,</span> user<span style="color: #000066;">.</span><span style="color: #006600;">password</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>What&#8217;s good about this arrangement is that we&#8217;ve tested the business logic of TUserRepository independent of a data source. However we&#8217;re left the question of how to write and test the actual production code that will provide live data to TUserRepository. We could test-drive this code in a unit test but the rules of TDD tell us that a test should not need external dependencies. Also, accessing a database in a unit test would slow down the test suite. So how do we test this code?</p>
<p><span id="more-139"></span></p>
<p><strong>FitNesse</strong></p>
<p>FitNesse is a framework for automated acceptance testing. It has two faces: the outward (or customer facing) wiki and the internal (or developer facing) framework. We&#8217;ll tackle the task of creating and test-driving the live data provider for TUserRepository using FitNesse.</p>
<p>Before taking on this task, the first thing you&#8217;ll need is a copy of FitNesse. You can download the latest version at the website <a target="_blank" href="http://fitnesse.org">fitnesse.org</a>.</p>
<p>The first step will be to write the test. This takes shape as a page on your FitNesse wiki:</p>
<blockquote><p>
|UserRepoDataFixture|<br />
|UserName|Password|<br />
|fred|flinstone|
</p></blockquote>
<p>This will be rendered on the wiki page like this:</p>
<div id="attachment_145" class="wp-caption alignnone" style="width: 310px"><a href="http://delphixtreme.com/wordpress/wp-content/uploads/2008/12/picture-3.png"><img src="http://delphixtreme.com/wordpress/wp-content/uploads/2008/12/picture-3-300x125.png" alt="User repo data fixture table" title="UserRepoDataFixtureTable.png" width="300" height="125" class="size-medium wp-image-145" /></a><p class="wp-caption-text">User repo data fixture table</p></div>
<p><strong><em>NOTE</em></strong>: I won&#8217;t cover how to create wiki pages in FitNesse. For this article I created a suite called SuiteExample and added a test page called TestUserRepoDataSource.</p>
<p>Clicking the Test link on the left side of the page will result in an error telling you that UserRepoDataFixture could not be found. So, you guessed it, the next step is to write the UserRepoDataFixture. </p>
<p>You need to grab the Delphi implementation of the FitNesse framework from the website <a target="_blank" href="http://code.google.com/p/fit4delphi/">http://code.google.com/p/fit4delphi/</a>.</p>
<p>The test above is based on the Row Fixture. This sort of fixture is good for testing data sets. We&#8217;ll inherit our UserRepoDataFixture from TRowFixture.</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">type</span>
  UserRepoDataFixture <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #000066;">&#40;</span>TRowFixture<span style="color: #000066;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">public</span>
    <span style="color: #000000; font-weight: bold;">function</span> query <span style="color: #000066;">:</span> TList<span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">override</span><span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">function</span> getTargetClass <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">TClass</span><span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">override</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>The Row Fixture requires that you do two things: (1) provide a list of objects that will represent the data set and (2) provide the class type of that object.</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> UserRepoDataFixture<span style="color: #000066;">.</span><span style="color: #006600;">query</span> <span style="color: #000066;">:</span> TList<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  user <span style="color: #000066;">:</span> TUser<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  user<span style="color: #000066;">.</span><span style="color: #006600;">UserName</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #ff0000;">'fred'</span><span style="color: #000066;">;</span>
  user<span style="color: #000066;">.</span><span style="color: #006600;">Password</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #ff0000;">'flinstone'</span><span style="color: #000066;">;</span>
  Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TList<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">;</span>
  Result<span style="color: #000066;">.</span><span style="color: #006600;">Add</span><span style="color: #000066;">&#40;</span>user<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> UserRepoDataFixture<span style="color: #000066;">.</span><span style="color: #006600;">getTargetClass</span> <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">TClass</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TUser<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>You&#8217;ll see that we finally needed to add the UserName field to TUser. I know that some of you have been secretly screaming that  TUser should have had this field from the start. Feel better? <img src='http://delphixtreme.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>We can build the fixture code and run the test and see that it passes. This is good because it tells us that our fixture code is being found and executed by the Delphi fit server. We still need to do the actual work:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> UserRepoDataFixture<span style="color: #000066;">.</span><span style="color: #006600;">query</span> <span style="color: #000066;">:</span> TList<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  user <span style="color: #000066;">:</span> TUser<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TList<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">;</span>
  UserDS <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TUserRepoDataSource<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">;</span>
  Repo <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TUserRepository<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span>UserDS<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">for</span> user <span style="color: #000000; font-weight: bold;">in</span> Repo <span style="color: #000000; font-weight: bold;">do</span>
    Result<span style="color: #000066;">.</span><span style="color: #006600;">Add</span><span style="color: #000066;">&#40;</span>user<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>What&#8217;s cool about this is that we didn&#8217;t have to instantiate our TUserRepository any differently than we did in our unit test. All we needed was to hand it a different implementation of ILoader. </p>
<p>Now some code for the TUserRepoDataSource:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">type</span>
  TUserRepoDataSource <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #000066;">&#40;</span>TInterfacedObject<span style="color: #000066;">,</span> ILoader<span style="color: #000066;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">private</span>
    <span style="color: #000000; font-weight: bold;">function</span> Connect <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">Boolean</span><span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">function</span> MoreData <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">Boolean</span><span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">function</span> Current <span style="color: #000066;">:</span> TUser<span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> MoveNext<span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">protected</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> Load<span style="color: #000066;">&#40;</span>Target <span style="color: #000066;">:</span> ILoadable<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">implementation</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> TUserRepoDataSource<span style="color: #000066;">.</span><span style="color: #006600;">Load</span><span style="color: #000066;">&#40;</span>Target <span style="color: #000066;">:</span> ILoadable<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  <span style="color: #000000; font-weight: bold;">if</span> Connect <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #000000; font-weight: bold;">begin</span>
    <span style="color: #000000; font-weight: bold;">while</span> MoreData <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #000000; font-weight: bold;">begin</span>
      Taget<span style="color: #000066;">.</span><span style="color: #006600;">AddRecord</span><span style="color: #000066;">&#40;</span>Current<span style="color: #000066;">.</span><span style="color: #006600;">UserName</span><span style="color: #000066;">,</span> Current<span style="color: #000066;">.</span><span style="color: #006600;">Password</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
      MoveNext<span style="color: #000066;">;</span>
    <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">{ SNIP }</span></pre></div></div>

<p>I&#8217;ve omitted the rest of the implementation for the sake of brevity. You can tell from the Load method that this class would connect to a data source (such as a database) and loop over the result set to add each record to the ILoadable. I&#8217;ve also implied that we should add an enumerator to TUserRepository. This is one solution to the problem of how to pull the data out for the user repository. You might choose another based on your needs or your particular version of Delphi. The crux of the code is the same: load data into TUserRepository and read it back out.</p>
<p>Now we face the same problem as in the first article: our test relies on data. We can run this test and it might pass on our development box but fail on the build machine because our expected data is not present. One of the ways we can solve this problem is using the SetUp and TearDown facilities of FitNesse. We&#8217;ll add these pages to our SuiteExampe wiki page:</p>
<blockquote><p>
^SetUp<br />
^TearDown
</p></blockquote>
<p>In the SetUp page we&#8217;ll add:</p>
<blockquote><p>
!|ExecSqlFixture|<br />
|INSERT INTO User (UserName, Password) VALUES(&#8217;fred&#8217;, &#8216;flinstone&#8217;)|
</p></blockquote>
<p>In the TearDown page we&#8217;ll add:</p>
<blockquote><p>
!|ExecSqlFixture|<br />
|DELETE FROM User WHERE UserName = &#8216;fred&#8217;|
</p></blockquote>
<p>Once that&#8217;s done out test page will render like this:</p>
<div id="attachment_147" class="wp-caption alignnone" style="width: 310px"><a href="http://delphixtreme.com/wordpress/wp-content/uploads/2008/12/picture-4.png"><img src="http://delphixtreme.com/wordpress/wp-content/uploads/2008/12/picture-4-300x262.png" alt="Test page with SetUp and TearDown" title="TestUserRepoDataSourcePage" width="300" height="262" class="size-medium wp-image-147" /></a><p class="wp-caption-text">Test page with SetUp and TearDown</p></div>
<p><strong><em>NOTE</em></strong>: At the time of this writing there are no database FitNesse fixtures written for Delphi. In this example I&#8217;ve used a fictional fixture called ExecSqlFixture. While it does not currently exist, writting this fixture shouldn&#8217;t be too much trouble.</p>
<p>Now our test assures our test data will be present before the test fixture is run and cleans up after itself at the end. This example presumes to use a SQL database as its data store. You might choose a different implementation and write different setup and tear down code.</p>
<p>Running the test now should find everything in order. Take some time and fiddle around with the test. Row Fixture does some neat things when you specify data that is not present and when you don&#8217;t specify data that is present.</p>
<p><strong>Why is this better than what we did in the first article?</strong></p>
<p>TDD prescribes that the tests should execute very fast. If they&#8217;re slow to execute then you won&#8217;t run then as often. So we move tests that are dependent on external dependencies out into the acceptance testing realm. We like this because it forces us to use a method that makes our code data source agnostic which in turn means we can test business logic in isolation. Using patterns like Factory or Builder we can take another step and write a number of different data access components and switch them in or out as we see fit.</p>
<p>FitNesse has considerably more benefits than what I&#8217;ve shown here. Because FitNesse is a wiki it&#8217;s easily accessible by non-tech types such as product owners and customers. In a perfect world the product owner and customer would have a suite of acceptance tests written before you started coding. These would act as executable product specifications &#8230; no more lengthy specs that get out of date and irrelevant!</p>
<p>I hope you enjoyed this three part series. </p>
]]></content:encoded>
			<wfw:commentRss>http://delphixtreme.com/wordpress/?feed=rss2&amp;p=139</wfw:commentRss>
		</item>
		<item>
		<title>Keep your code ignorant and lazy!</title>
		<link>http://delphixtreme.com/wordpress/?p=85</link>
		<comments>http://delphixtreme.com/wordpress/?p=85#comments</comments>
		<pubDate>Wed, 26 Nov 2008 17:53:42 +0000</pubDate>
		<dc:creator>Jody Dawkins</dc:creator>
		
		<category><![CDATA[XP]]></category>

		<category><![CDATA[engineering]]></category>

		<category><![CDATA[patterns]]></category>

		<category><![CDATA[refactoring]]></category>

		<category><![CDATA[TDD]]></category>

		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://delphixtreme.com/wordpress/?p=85</guid>
		<description><![CDATA[This post is a follow up to Busting the great TDD Myth. If you haven&#8217;t already read it, please check it out.
Lets review the code from the last post:

function TLogin.GetConnectionString : string;
var
  reg : TRegIniFile;
begin
  reg := TRegIniFile.Create&#40;'Software\MyApp'&#41;;
  Result := reg.ReadString&#40;'Database', 'ConnectionString', ''&#41;;
  FreeAndNil&#40;reg&#41;;
end;
&#160;
function TLogin.Execute&#40;const UserName, Password : string&#41; : Boolean;
var
 [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a follow up to <a target="_blank" href="http://delphixtreme.com/wordpress/?p=57"><strong>Busting the great TDD Myth</strong></a>. If you haven&#8217;t already read it, please check it out.</p>
<p>Lets review the code from the <a target="_blank" href="http://delphixtreme.com/wordpress/?p=57">last post</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> TLogin<span style="color: #000066;">.</span><span style="color: #006600;">GetConnectionString</span> <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  reg <span style="color: #000066;">:</span> TRegIniFile<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  reg <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TRegIniFile<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Software\MyApp'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> reg<span style="color: #000066;">.</span><span style="color: #006600;">ReadString</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Database'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'ConnectionString'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">''</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000066;">FreeAndNil</span><span style="color: #000066;">&#40;</span>reg<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> TLogin<span style="color: #000066;">.</span><span style="color: #006600;">Execute</span><span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName<span style="color: #000066;">,</span> Password <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">Boolean</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  UserQuery <span style="color: #000066;">:</span> TADOQuery<span style="color: #000066;">;</span>
  DBUserPassword <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  UserQuery <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TADOQuery<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">try</span>
    UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">ConnectionString</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> GetConnectionString<span style="color: #000066;">;</span>
    UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">SQL</span><span style="color: #000066;">.</span><span style="color: #006600;">Text</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #ff0000;">'SELECT Password FROM Users WHERE UserName = '</span> <span style="color: #000066;">+</span> 
                         <span style="color: #000066;">QuotedStr</span><span style="color: #000066;">&#40;</span>UserName<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">Open</span><span style="color: #000066;">;</span>
    DBUserPassword <span style="color: #000066;">:</span><span style="color: #000066;">=</span> UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">FieldByName</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Password'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">.</span><span style="color: #006600;">AsString</span><span style="color: #000066;">;</span>
    UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">Free</span><span style="color: #000066;">;</span>
    Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> DBUserPassword <span style="color: #000066;">=</span> Password<span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">except</span>
    Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">False</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>Now lets review the problems:</p>
<ul>
<li>Hidden dependencies - TLogin does not publish the fact that it relies on a connection to the database and needs a connection string from the registry.</li>
<li>Mixing concerns - Business logic and object creation occur in the same block of code. This means you can&#8217;t test business logic without creating the dependencies.</li>
</ul>
<p><br/><br />
All of these problems can be summed up as: Too much ceremony and too little essence. What is this class trying to do? Does creating a connection to the database, querying a table and handling exceptions have much to do with the purpose of logging in to the system?</p>
<p><span id="more-85"></span><br />
Before we start solving problems lets think about the elements involved with our task of logging in to the system. We have someone who wants to log in, we have a system for authenticating this person and we have system for providing the data against which this person will be authenticated. These are our domain objects: User, Login and User Repository. </p>
<p><em><strong>NOTE:</strong> I don&#8217;t say Database because I don&#8217;t want my vocabulary to influence the implementation. I might use a database and I might not. For now I want to keep my options open.</em></p>
<p>We will begin by stripping away all the stuff in the login code that does not directly relate to the login business rules. That leaves us with:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> TLogin<span style="color: #000066;">.</span><span style="color: #006600;">Execute</span><span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName<span style="color: #000066;">,</span> Password <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">Boolean</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> Password <span style="color: #000066;">=</span> DBPassword<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>That&#8217;s it, really. All the other stuff has nothing to do with the business of logging in to the system. The old code looked for everything. So lets do the opposite. Lets demand everything we need:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> TLogin<span style="color: #000066;">.</span><span style="color: #006600;">Execute</span><span style="color: #000066;">&#40;</span>User <span style="color: #000066;">:</span> TUser<span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">const</span> Password <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">boolean</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> Password <span style="color: #000066;">=</span> User<span style="color: #000066;">.</span><span style="color: #006600;">Password</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>That&#8217;s considerably simpler. We even eliminated a test. We no longer have to test the TLogin object for a failure to connect to the database. We also eliminated two dependencies: the database and the registry.</p>
<p>What did we demand? We asked for something called TUser that includes a field called Password. The expectation is that this TUser construct should be the user in question and should include their saved password from the database. </p>
<p>This makes the tests very simple as well. All I need to do is supply a TUser (which I could easily create by hand) and a password against which to compare. So the question is: when this code is in production from where will the TUser come? Lets start with what TUser looks like right now:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">type</span>
  TUser <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">record</span>
    Password <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>All we need right now is the password field. Now lets look at the tests:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">procedure</span> TLoginTests<span style="color: #000066;">.</span><span style="color: #006600;">SetUp</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  FUser<span style="color: #000066;">.</span><span style="color: #006600;">Password</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #ff0000;">'flinstone'</span><span style="color: #000066;">;</span>
  FLogin <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TLogin<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> TLoginTests<span style="color: #000066;">.</span><span style="color: #006600;">TearDown</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  <span style="color: #000066;">FreeAndNil</span><span style="color: #000066;">&#40;</span>FLogin<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> TLoginTests<span style="color: #000066;">.</span><span style="color: #006600;">TestGoodLogin</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  CheckEquals<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">True</span><span style="color: #000066;">,</span> FLogin<span style="color: #000066;">.</span><span style="color: #006600;">Execute</span><span style="color: #000066;">&#40;</span>FUser<span style="color: #000066;">,</span> <span style="color: #ff0000;">'flinstone'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> TLoginTests<span style="color: #000066;">.</span><span style="color: #006600;">TestBadLogin</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  CheckEquals<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">False</span><span style="color: #000066;">,</span> FLogin<span style="color: #000066;">.</span><span style="color: #006600;">Execute</span><span style="color: #000066;">&#40;</span>FUser<span style="color: #000066;">,</span> <span style="color: #ff0000;">'rubble'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>That&#8217;s considerably less code. It&#8217;s even pretty easy to read. So why is this better than the code from the previous post?</p>
<ul>
<li>There&#8217;s less of it</li>
<li>It only performs the task at hand: logging in to the system</li>
<li>It&#8217;s honest about what it does - there are no hidden dependencies</li>
<li>You can now test the business logic in isolation</li>
</ul>
<p></br></p>
<p>Now lets anwser the other burning question: In production, from where will we get the TUser that TLogin demands. Good question. Lets manifest some code:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">procedure</span> TUserRepositoryTests<span style="color: #000066;">.</span><span style="color: #006600;">SetUp</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  FRepo <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TUserRepository<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> TUserRepositoryTests<span style="color: #000066;">.</span><span style="color: #006600;">TearDown</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  <span style="color: #000066;">FreeAndNil</span><span style="color: #000066;">&#40;</span>FRepo<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> TUserRepositoryTests<span style="color: #000066;">.</span><span style="color: #006600;">TestValidUserLookup</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  user <span style="color: #000066;">:</span><span style="color: #000066;">=</span> FRepo<span style="color: #000066;">.</span><span style="color: #006600;">Lookup</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'fred'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  CheckEquals<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'flinstone'</span><span style="color: #000066;">,</span> user<span style="color: #000066;">.</span><span style="color: #006600;">Password</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>The <a target="_blank" href="http://books.google.com/books?id=gFgnde_vwMAC&#038;pg=PA151&#038;lpg=PA151&#038;dq=tdd+fake+it+til+you+make+it&#038;source=web&#038;ots=enFvrqSrnD&#038;sig=zoPPTr5zcANnPvjlWdqol2OWP4c&#038;hl=en&#038;sa=X&#038;oi=book_result&#038;resnum=4&#038;ct=result">fake-it-til-you-make-it</a> principle tells us we should do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> TUserRepository<span style="color: #000066;">.</span><span style="color: #006600;">Lookup</span><span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> TUser<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  Result<span style="color: #000066;">.</span><span style="color: #006600;">Password</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #ff0000;">'flinstone'</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>That&#8217;s enough code to make the test pass. Let&#8217;s add another test:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">procedure</span> TUserRepositoryTests<span style="color: #000066;">.</span><span style="color: #006600;">TestInvalidUserLookup</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  user <span style="color: #000066;">:</span><span style="color: #000066;">=</span> FRepo<span style="color: #000066;">.</span><span style="color: #006600;">Lookup</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'barney'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  CheckEquals<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #000066;">,</span> user<span style="color: #000066;">.</span><span style="color: #006600;">Password</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>Our test tells us two things about the user repository business logic:</p>
<ol>
<li>The user repository returns a blank password when a user cannot be found</li>
<li>A blank password is never valid for a user</li>
</ol>
<p><br/><br />
Running the test confirms that it fails because of our fake implementation. Now we get solve the real problem: how to make TUserRepository flexible so that we can test drive it. TUserRespository needs to get its data from somewhere both in the test suite and in production.</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;">ILoadable <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">interface</span>
  <span style="color: #000000; font-weight: bold;">procedure</span> AddRecord<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName<span style="color: #000066;">,</span> Password <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
ILoader <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">interface</span>
  <span style="color: #000000; font-weight: bold;">procedure</span> Load<span style="color: #000066;">&#40;</span>Target <span style="color: #000066;">:</span> ILoadable<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p><em><strong>NOTE</strong>: This seems to be referred to as the Data Provider or just Provider pattern.</em></p>
<p>An object implementing ILoader will be responsible for providing data to an object implementing ILoadable. An object implementing ILoadable will be responsible for asking ILoader to load it with data and storing that data. We&#8217;ll have TUserRepository implement ILoadable to ensure that it is data source agnostic. This will meet our goal of keeping TUserRepository test-drivable and production worthy. Let&#8217;s look at the implementation code:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;">TUserRepository <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #000066;">&#40;</span>TInterfacedObject<span style="color: #000066;">,</span> ILoadable<span style="color: #000066;">&#41;</span>
<span style="color: #000000; font-weight: bold;">private</span>
  FUsers <span style="color: #000066;">:</span> TStringList<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">protected</span>
  <span style="color: #000000; font-weight: bold;">procedure</span> AddRecord<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName<span style="color: #000066;">,</span> Password <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">public</span>
  <span style="color: #000000; font-weight: bold;">constructor</span> Create<span style="color: #000066;">&#40;</span>Loader <span style="color: #000066;">:</span> ILoader<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">destructor</span> Destroy<span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">override</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">function</span> Lookup<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> TUser<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">implementation</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">constructor</span> TUserRepository<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span>Loader <span style="color: #000066;">:</span> ILoader<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span> 
  FUsers <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TStringList<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">;</span>
  Loader<span style="color: #000066;">.</span><span style="color: #006600;">Load</span><span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">Self</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">destructor</span> TUserRepository<span style="color: #000066;">.</span><span style="color: #006600;">Destroy</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  <span style="color: #000066;">FreeAndNil</span><span style="color: #000066;">&#40;</span>FUsers<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> TUserRepository<span style="color: #000066;">.</span><span style="color: #006600;">AddRecord</span><span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName<span style="color: #000066;">,</span> Password <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  FUsers<span style="color: #000066;">.</span><span style="color: #006600;">Add</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">Format</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'%s=%s'</span><span style="color: #000066;">,</span> <span style="color: #000066;">&#91;</span>UserName<span style="color: #000066;">,</span> Password<span style="color: #000066;">&#93;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> TUserRepository<span style="color: #000066;">.</span><span style="color: #006600;">Lookup</span><span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> TUser<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  Result<span style="color: #000066;">.</span><span style="color: #006600;">Password</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> FUsers<span style="color: #000066;">.</span><span style="color: #006600;">Values</span><span style="color: #000066;">&#91;</span>UserName<span style="color: #000066;">&#93;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>I think you&#8217;ll agree that we&#8217;ve done the minimal amount of work here. Right now all we care about is looking up a user by their user name and providing the associated password. As such, a TStringList will do.</p>
<p>Now we&#8217;ve introduced a dependency. TUserRespository depends on an ILoader to provide its data. How is that any different than the code from the previous post? First, TUserRepository is honest about its dependency; you can&#8217;t create it without providing an ILoader. Second, rather than have TUserRepository demand a concrete object it asks for an interface. This is good because TUserRepository has to know nothing about the actual implementation of ILoader or even from where the data is being provided.</p>
<p>Since we&#8217;ve added this dependency we need to update our test:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">implementation</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">type</span>
  TTestData <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #000066;">&#40;</span>TInterfacedObject<span style="color: #000066;">,</span> ILoader<span style="color: #000066;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">protected</span>
    <span style="color: #000000; font-weight: bold;">procedure</span> Load<span style="color: #000066;">&#40;</span>Target <span style="color: #000066;">:</span> ILoadable<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> TTestData<span style="color: #000066;">.</span><span style="color: #006600;">Load</span><span style="color: #000066;">&#40;</span>Target <span style="color: #000066;">:</span> ILoadable<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  Target<span style="color: #000066;">.</span><span style="color: #006600;">AddRecord</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'fred'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'flinstone'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> TUserRepositoryTests<span style="color: #000066;">.</span><span style="color: #006600;">SetUp</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  TestData <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TTestData<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">;</span>
  FRepo <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TUserRepository<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span>TestData<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> TUserRepositoryTests<span style="color: #000066;">.</span><span style="color: #006600;">TearDown</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  FRepo <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>We needed to provide TUserRepository with an ILoader. Because we want to control the data we create a stub ILoader with TTestData that only loads the ILoadable with one record. In production we would need to create TUserRepository with an ILoader that provides data from the production data source. This is where we might connect to a database and query a table. I&#8217;ll leave how to create and test this portion of the code for another post.</p>
<p><strong>Take aways</strong></p>
<p>How did we solve our problems?</p>
<ul>
<li>Dependency injection. Rather than have our code look for or create what it needed we demanded everything upfront. We kept our code as ignorant as possible. This helped us separate the business logic from object creation and allowed us to test the business logic in isolation.</li>
<p><br/></p>
<li>Inversion of control. Each piece of code asked its dependencies to do all the work. Our code was not only ignorant, it was also as lazy as possible.</li>
<p><br/></p>
<li>Separation of concerns. Each piece of code only implemented the business logic relevant to its task. Each of the three domain objects did only what was needed and left the details to its dependencies. None of them meddled in the affairs of the others.</li>
</ul>
<p><br/><br />
<strong>The wrap-up</strong></p>
<p>In the next post I&#8217;ll cover how to use <a target="blank" href="http://www.fitnesse.org">FitNesse</a> to test the code that will provide live data to the TUserRepository.</p>
<p>I hope you enjoyed the article.</p>
<p><strong>Additional information</strong></p>
<p>Check out this Google talk: <a target="_blank" href="http://googletesting.blogspot.com/2008/11/clean-code-talks-dependency-injection.html">Clean Code Talks - Dependency Injection</a><br />
Get the slides here: <a target="_blank" href="http://docs.google.com/Presentation?id=d449gch_58dtrzqtgv">Link</a></p>
<p>Check this article: <a target="_blank" href="http://blog.thinkrelevance.com/2008/4/1/ending-legacy-code-in-our-lifetime">Ending Legacy Code in our lifetime</a></p>
]]></content:encoded>
			<wfw:commentRss>http://delphixtreme.com/wordpress/?feed=rss2&amp;p=85</wfw:commentRss>
		</item>
		<item>
		<title>Busting the great TDD myth</title>
		<link>http://delphixtreme.com/wordpress/?p=57</link>
		<comments>http://delphixtreme.com/wordpress/?p=57#comments</comments>
		<pubDate>Thu, 20 Nov 2008 14:51:25 +0000</pubDate>
		<dc:creator>Jody Dawkins</dc:creator>
		
		<category><![CDATA[XP]]></category>

		<category><![CDATA[engineering]]></category>

		<category><![CDATA[TDD]]></category>

		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://delphixtreme.com/wordpress/?p=57</guid>
		<description><![CDATA[WARNING
There&#8217;s a lot going on in this article. It is intended to be humorous and slightly inflammatory. It&#8217;s also the first of a series. I intend to show you how the average Joe digs himself into a hole and then how to get out. I hope you enjoy the read.
On with the show &#8230;
You will [...]]]></description>
			<content:encoded><![CDATA[<p><strong>WARNING</strong><br />
There&#8217;s a lot going on in this article. It is intended to be humorous and slightly inflammatory. It&#8217;s also the first of a series. I intend to show you how the average Joe digs himself into a hole and then how to get out. I hope you enjoy the read.</p>
<p><strong>On with the show &#8230;</strong></p>
<p>You will not learn what you need to know from a TDD course/book/lecture. When you walk away you will not be able to write better software. You will not be able to test drive all your code in a fully automated test suite. Your expectations will not be met and you will quickly dump the whole concept of Test Driven Development.</p>
<p>It&#8217;s true.</p>
<p>The problem has nothing to do with TDD. The problem is that you don&#8217;t <strong>know</strong> how to write better software. TDD will not make you a coding rock star. You will not be the envy of your peers. Your face will not be printed on a t-shirt (I don&#8217;t think that&#8217;s ever happened but it would be cool).</p>
<p><span id="more-57"></span><br />
So what&#8217;s the hype with TDD, BDD, ATDD, LMNOP, QRS, ETC? The truth is to be successful with TDD you must either 1) already be good at writing great software or 2) get good at writing great software. If you&#8217;re already good at writing great software and/or already being successful with TDD then you&#8217;re probably reading this for entertainment. As such I&#8217;ll forgo any further exposition on that point. <img src='http://delphixtreme.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>So how do you get good and writing great software? Examine the aspects of code that make software difficult or impossible to test. Software that is testable has a far greater chance at being great software than code that is not testable. </p>
<p>What are the aspects of code that software difficult to test? While there are many the primary culprit is Dependancies. How is the expressed as a symptom of non-testablity in code? It comes primarily from the mixing of object creation and business logic.</p>
<p>Let&#8217;s see an example of this:</p>
<p>You&#8217;re fresh out of TDD school (or whatever) and you sit back down at your desk at work to tackle your first code task. It&#8217;s a simple task - log in to the system. You think &#8220;No problem!&#8221; and create your first test case:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">procedure</span> TLoginTests<span style="color: #000066;">.</span><span style="color: #006600;">TestGoodLogin</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  login <span style="color: #000066;">:</span> TLogin<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  login <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TLogin<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">;</span>
  CheckEquals<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">True</span><span style="color: #000066;">,</span> login<span style="color: #000066;">.</span><span style="color: #006600;">Execute</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'fred'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'flinstone'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'Login should have succeeded!'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000066;">FreeAndNil</span><span style="color: #000066;">&#40;</span>login<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>Looks easy enough, right? You run the test, it fails and all seems right with the world! Now you add some code to make the test pass:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">type</span>
  TLogin <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #000066;">&#40;</span><span style="color: #000066; font-weight: bold;">TObject</span><span style="color: #000066;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">public</span>
    <span style="color: #000000; font-weight: bold;">function</span> Execute<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName<span style="color: #000066;">,</span> Password <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">Boolean</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">implementation</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> TLogin<span style="color: #000066;">.</span><span style="color: #006600;">Execute</span><span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName<span style="color: #000066;">,</span> Password <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">Boolean</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">True</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>You run the test and see it pass! You&#8217;re happy that you followed the method, did the simplest thing possible to make the test pass and now you&#8217;re ready for the next step: Refactor. You examine the code and decide you don&#8217;t really need to do any refactoring right now. Go Team!!</p>
<p>Now you write the next test:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">procedure</span> TLoginTests<span style="color: #000066;">.</span><span style="color: #006600;">TestBadLogin</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  login <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TLogin<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">;</span>
  CheckEquals<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">False</span><span style="color: #000066;">,</span> login<span style="color: #000066;">.</span><span style="color: #006600;">Execute</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'fred'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'rubble'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'Login should have failed!'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>You run the test and see it fail. Huzzah! Now it&#8217;s time to add the code to make the test work. You think for a second about simply making the function return false but you know better than that. So what is the simplest thing? You need to query the database, retrieve the record for this user and validate the password, right?</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> TLogin<span style="color: #000066;">.</span><span style="color: #006600;">Execute</span><span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName<span style="color: #000066;">,</span> Password <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">Boolean</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  UserQuery <span style="color: #000066;">:</span> TADOQuery<span style="color: #000066;">;</span>
  DBUserPassword <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  UserQuery <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TADOQuery<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">ConnectionString</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #ff0000;">'Driver={SQL Native Client};'</span> <span style="color: #000066;">+</span>
                                <span style="color: #ff0000;">'Server=localhost;'</span> <span style="color: #000066;">+</span>
                                <span style="color: #ff0000;">'Database=SecurityDatabase;'</span> <span style="color: #000066;">+</span>
                                <span style="color: #ff0000;">'Uid=myUsername;'</span> <span style="color: #000066;">+</span>
                                <span style="color: #ff0000;">'Pwd=myPassword;'</span><span style="color: #000066;">;</span>
  UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">SQL</span><span style="color: #000066;">.</span><span style="color: #006600;">Text</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #ff0000;">'SELECT Password FROM Users WHERE UserName = '</span> <span style="color: #000066;">+</span> 
                         <span style="color: #000066;">QuotedStr</span><span style="color: #000066;">&#40;</span>UserName<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">Open</span><span style="color: #000066;">;</span>
  DBUserPassword <span style="color: #000066;">:</span><span style="color: #000066;">=</span> UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">FieldByName</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Password'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">.</span><span style="color: #006600;">AsString</span><span style="color: #000066;">;</span>
  UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">Free</span><span style="color: #000066;">;</span>
  Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> DBUserPassword <span style="color: #000066;">=</span> Password<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p><em>(You&#8217;re welcome to beat the SQL injection horse to death in the comments)</em></p>
<p>Whew! You run the tests and find both tests fail. Ouch. Feeling silly, you add the user &#8220;fred&#8221; into the database with a password of &#8220;flinstone&#8221; and run the tests again. Success! The tests pass. Now it&#8217;s time for refactoring.</p>
<p>The most obvious problem is that your production code can&#8217;t be hard coded to talk to your local database with a bad set of login credentials. Looks like the hard coded connection string had got to go:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;">  <span style="color: #000066;">...</span>
  <span style="color: #006600;">UserQuery</span><span style="color: #000066;">.</span><span style="color: #006600;">ConnectionString</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> GetConnectionString<span style="color: #000066;">;</span>
  <span style="color: #000066;">...</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> TLogin<span style="color: #000066;">.</span><span style="color: #006600;">GetConnectionString</span> <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  reg <span style="color: #000066;">:</span> TRegIniFile<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  reg <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TRegIniFile<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Software\MyApp'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> reg<span style="color: #000066;">.</span><span style="color: #006600;">ReadString</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Database'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'ConnectionString'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">''</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000066;">FreeAndNil</span><span style="color: #000066;">&#40;</span>reg<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>This time you were a smarty pants and added the registry entry before running the tests! You find the tests still pass. Sweet! TDD rocks!</p>
<p>Any more refactoring needed? Nothing stands out. Any more tests to write? We&#8217;ve a test for a good login and a bad login &#8230; that seems sufficient. Let&#8217;s check it in and get the tests added to the build system.</p>
<p>You arrive the next morning ready to tackle your next challenge but find an email from the build system politely informing you that you broke the build. Damn. The tests passed on my machine. Stupid build system. So what&#8217;s the problem? The test failed with an exception - cannot connect to the database. Damn, guess we needed another test after all. You know that your code reads the connection string from the registry and you know that the connection in your registry is correct. That means you need to save your good connection string, write a bad (or blank) connection string, execute the code and put the good connection string back:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">procedure</span> TLoginTests<span style="color: #000066;">.</span><span style="color: #006600;">TestDatabaseConnectionFailure</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  reg <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TRegIniFile<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Software\MyApp'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  OldConnectionString <span style="color: #000066;">:</span><span style="color: #000066;">=</span> reg<span style="color: #000066;">.</span><span style="color: #006600;">ReadString</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Database'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'ConnectionString'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">''</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  reg<span style="color: #000066;">.</span><span style="color: #006600;">WriteString</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Database'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'ConnectionString'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'NO CONNECTION FOR YOU!!'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  login <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TLogin<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">;</span>
  CheckEquals<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">False</span><span style="color: #000066;">,</span> login<span style="color: #000066;">.</span><span style="color: #006600;">Execute</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'This'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'Does Not Matter'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  reg<span style="color: #000066;">.</span><span style="color: #006600;">WriteString</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Database'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'ConnectionString'</span><span style="color: #000066;">,</span> OldConnectionString<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000066;">FreeAndNil</span><span style="color: #000066;">&#40;</span>login<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000066;">FreeAndNil</span><span style="color: #000066;">&#40;</span>reg<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>Hmm, that was alot trouble to go to simulate no connection to the database. You run the tests and they blow up. Cool. Now you can fix the problem:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> TLogin<span style="color: #000066;">.</span><span style="color: #006600;">Execute</span><span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> UserName<span style="color: #000066;">,</span> Password <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">Boolean</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  UserQuery <span style="color: #000066;">:</span> TADOQuery<span style="color: #000066;">;</span>
  DBUserPassword <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  UserQuery <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TADOQuery<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">try</span>
    UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">ConnectionString</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> GetConnectionString<span style="color: #000066;">;</span>
    UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">SQL</span><span style="color: #000066;">.</span><span style="color: #006600;">Text</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #ff0000;">'SELECT Password FROM Users WHERE UserName = '</span> <span style="color: #000066;">+</span> 
                         <span style="color: #000066;">QuotedStr</span><span style="color: #000066;">&#40;</span>UserName<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">Open</span><span style="color: #000066;">;</span>
    DBUserPassword <span style="color: #000066;">:</span><span style="color: #000066;">=</span> UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">FieldByName</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Password'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">.</span><span style="color: #006600;">AsString</span><span style="color: #000066;">;</span>
    UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">Free</span><span style="color: #000066;">;</span>
    Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> DBUserPassword <span style="color: #000066;">=</span> Password<span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">except</span>
    Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">False</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>That ought to do it. Running the tests confirms it - the code now gracefully handles no connection to the database. The tests pass so let&#8217;s check it in. Hmmm, what a bunch of work that was. Seems like the test code was a little complicated.</p>
<p>Another build cycle completes and you get another broken build email. Yikes, that&#8217;s two! The test for the bad login passed while the test for the good login failed. You know that you&#8217;re handling the no-connection scenario so it can&#8217;t be that. The test database must be missing the user with which you&#8217;re driving your test. Inspecting the database proves your theory. You add the user &#8220;fred&#8221; with password &#8220;flinstone&#8221;. So now, provided you can connect to the database you should be golden &#8230; hmmm, connect to the database. Better check that the build machine has the connection string in the registry. You don&#8217;t want a third broken build email. Sure enough, no connection string in the registry. You quietly add it and pray that nothing else goes wrong. </p>
<p>You sure have to jump though a lot of hoops to make this TDD stuff work. To have gotten this all right the first time you needed to have:</p>
<ol>
<li>Checked in the code</li>
<li>Added the user into the test database</li>
<li>Added the connection string to the registry on the build machine</li>
</ol>
<p>You come in the next day to another broken build email. What the ?!?!?!!! Again, the test for the bad login passed while the test for the good login failed. How can that possibly be? After some investigation you find that another test is clearing the Users table and leaves it empty before your test runs. Wow. So in your test you have to guarantee that the user you need in your test exists:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">procedure</span> TLoginTests<span style="color: #000066;">.</span><span style="color: #006600;">TestGoodLogin</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  login <span style="color: #000066;">:</span> TLogin<span style="color: #000066;">;</span>
  reg <span style="color: #000066;">:</span> TRegIniFile<span style="color: #000066;">;</span>
  UserQuery <span style="color: #000066;">:</span> TADOQuery<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  UserQuery <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TADOQuery<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  reg <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TRegIniFile<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Software\MyApp'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">ConnectionString</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> reg<span style="color: #000066;">.</span><span style="color: #006600;">ReadString</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Database'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'ConnectionString'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">''</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  reg<span style="color: #000066;">.</span><span style="color: #006600;">Free</span><span style="color: #000066;">;</span>
  UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">SQL</span><span style="color: #000066;">.</span><span style="color: #006600;">Text</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #ff0000;">'INSERT INTO Users VALUES('</span> <span style="color: #000066;">+</span>
    <span style="color: #000066;">QuotedStr</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'fred'</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">+</span> <span style="color: #ff0000;">','</span> <span style="color: #000066;">+</span> <span style="color: #000066;">QuotedStr</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'flinstone'</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">+</span> <span style="color: #ff0000;">')'</span><span style="color: #000066;">;</span>
  UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">ExecSQL</span><span style="color: #000066;">;</span>
  UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">Free</span><span style="color: #000066;">;</span>
  login <span style="color: #000066;">:</span><span style="color: #000066;">=</span> TLogin<span style="color: #000066;">.</span><span style="color: #006600;">Create</span><span style="color: #000066;">;</span>
  CheckEquals<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">True</span><span style="color: #000066;">,</span> login<span style="color: #000066;">.</span><span style="color: #006600;">Execute</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'fred'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'flinstone'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'Login should have succeeded!'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000066;">FreeAndNil</span><span style="color: #000066;">&#40;</span>login<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>You run your test and it blows up - the query is complaining that the user &#8220;fred&#8221; already exists. Grrr:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;">  <span style="color: #000066;">...</span>
  <span style="color: #000000; font-weight: bold;">try</span>
    UserQuery<span style="color: #000066;">.</span><span style="color: #006600;">ExecSQL</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">except</span>
    <span style="color: #808080; font-style: italic;">// shhhh.</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
  <span style="color: #000066;">...</span></pre></div></div>

<p>Now your test passes. Just to be sure, you delete the user &#8220;fred&#8221; from your database and test again: success. Good grief!</p>
<p>You sure had to do a lot of work to make that simple set of tests passing. Let me give you a glimpse into the future of what those tests will look like:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">procedure</span> TLoginTests<span style="color: #000066;">.</span><span style="color: #006600;">TestGoodLogin</span><span style="color: #000066;">;</span>
<span style="color: #808080; font-style: italic;">{
var
  login : TLogin;
  reg : TRegIniFile;
  UserQuery : TADOQuery;
}</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  Check<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">True</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #808080; font-style: italic;">{
  UserQuery := TADOQuery.Create(nil);
  reg := TRegIniFile.Create('Software\MyApp');
  UserQuery.ConnectionString := reg.ReadString('Database', 'ConnectionString', '');
  reg.Free;
  UserQuery.SQL.Text := 'INSERT INTO Users VALUES(' +
    QuotedStr('fred') + ',' + QuotedStr('flinstone') + ')';
  try
    UserQuery.ExecSQL;
  except
    // shhhh.
  end;
  UserQuery.Free;
  login := TLogin.Create;
  CheckEquals(True, login.Execute('fred', 'flinstone'), 'Login should have succeeded!');
  FreeAndNil(login);
}</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> TLoginTests<span style="color: #000066;">.</span><span style="color: #006600;">TestBadLogin</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  Check<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">True</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #808080; font-style: italic;">{
  login := TLogin.Create;
  CheckEquals(False, login.Execute('fred', 'rubble'), 'Login should have failed!');
}</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p><strong>The punch-line</strong></p>
<p>So how did this turn into such a nightmare? Shouldn&#8217;t this have been an easy proposition? Is TDD to blame for the trouble and the extended completion time-line?</p>
<p>Lets look at the problems:</p>
<p>The dependencies: the data in the database and the connection string in the registry. That little bit of code that read a user from the database and a connection string from the registry created a testing nightmare. </p>
<p>The mixing of concerns: the login code had to be concerned with business logic as well as where to find the needed pieces of data.  </p>
<p>How do we solve these problems? In the next article we&#8217;ll look at some techniques to help refactor this code into something that&#8217;s test-drivable, flexible and robust. I also hope that you&#8217;ll find something to help you avoid this type of nightmare in the future.</p>
<p><strong>Notes</strong><br />
<em><br />
I know the first test would not compile until adding the implementation code.<br />
I know this login code is susceptible to SQL injection.<br />
I know this login code is not even remotely secure.<br />
I know that you should compare hashed passwords; not plain text passwords.<br />
I know that the example gets a little long-winded. Thanks for making it this far.<br />
There are a number of recent blog posts that have inspired this article. I&#8217;ll list them in the next part.<br />
</em></p>
]]></content:encoded>
			<wfw:commentRss>http://delphixtreme.com/wordpress/?feed=rss2&amp;p=57</wfw:commentRss>
		</item>
		<item>
		<title>Science without art is no fun</title>
		<link>http://delphixtreme.com/wordpress/?p=43</link>
		<comments>http://delphixtreme.com/wordpress/?p=43#comments</comments>
		<pubDate>Thu, 09 Oct 2008 19:12:00 +0000</pubDate>
		<dc:creator>Jody Dawkins</dc:creator>
		
		<category><![CDATA[XP]]></category>

		<category><![CDATA[agile]]></category>

		<category><![CDATA[engineering]]></category>

		<category><![CDATA[philosophy]]></category>

		<category><![CDATA[waterfall]]></category>

		<guid isPermaLink="false">http://delphixtreme.com/wordpress/?p=43</guid>
		<description><![CDATA[I just encountered a great article on Ward Cummingham&#8217;s wiki. It&#8217;s called Programming Is Not Fun. The section that I enjoyed is called &#8220;Programming Is Not Fun without Philosophy&#8221;. You can find it near the bottom of the page.
I&#8217;ve always considered programming/engineering/development to be more art than science. I guess you can chalk it up [...]]]></description>
			<content:encoded><![CDATA[<p>I just encountered a great article on Ward Cummingham&#8217;s wiki. It&#8217;s called <a target="_blank" href="http://c2.com/cgi/wiki?ProgrammingIsNotFun">Programming Is Not Fun</a>. The section that I enjoyed is called &#8220;Programming Is Not Fun without Philosophy&#8221;. You can find it near the bottom of the page.</p>
<p>I&#8217;ve always considered programming/engineering/development to be more art than science. I guess you can chalk it up to my background in the arts. What hit home for me was the assertion that any kind of work done without an underlying philosophy is wicked boring and unfulfilling. </p>
<p>The article notes that other arts have established and accepted schools of thought and philosophy. For instance, painting has Cubism, Impressionism, etc. Do we have schools of thought and philosophy? Do we have Waterfall, Agile, Do-Whatever-It-Takes-To-Ship, TDD, BDD?</p>
<p>What about you? Do you employ any sort of philosophy in your work?</p>
]]></content:encoded>
			<wfw:commentRss>http://delphixtreme.com/wordpress/?feed=rss2&amp;p=43</wfw:commentRss>
		</item>
		<item>
		<title>FitNesse for Delphi updated</title>
		<link>http://delphixtreme.com/wordpress/?p=32</link>
		<comments>http://delphixtreme.com/wordpress/?p=32#comments</comments>
		<pubDate>Thu, 25 Sep 2008 18:25:58 +0000</pubDate>
		<dc:creator>Jody Dawkins</dc:creator>
		
		<category><![CDATA[XP]]></category>

		<category><![CDATA[acceptance testing]]></category>

		<category><![CDATA[delphi]]></category>

		<category><![CDATA[fitnesse]]></category>

		<guid isPermaLink="false">http://delphixtreme.com/wordpress/?p=32</guid>
		<description><![CDATA[Thanks to Michal Wojcik for completing the port of FitServer to Delphi. Going forward you can get the code from http://code.google.com/p/fit4delphi/.
All the major fixtures have been ported (Column Fixture, Row Fixture, Action Fixture, Table Fixture, Row Entry Fixture).
What does this mean to you? Check out The Need for Automated Acceptance Testing. FitNesse is a tool [...]]]></description>
			<content:encoded><![CDATA[<p>Thanks to Michal Wojcik for completing the port of FitServer to Delphi. Going forward you can get the code from <a target="_blank" href="http://code.google.com/p/fit4delphi/">http://code.google.com/p/fit4delphi/</a>.</p>
<p>All the major fixtures have been ported (Column Fixture, Row Fixture, Action Fixture, Table Fixture, Row Entry Fixture).</p>
<p>What does this mean to you? Check out <a target="_blank" href="http://www.developer.com/tech/article.php/3643611">The Need for Automated Acceptance Testing</a>. FitNesse is a tool created to fit this need (no pun intended). </p>
<p>What&#8217;s great about automated acceptance testing? In short: concrete examples straight from the customer. FitNesse is also a way to get executable specifications. FitNesse does its work in the form of a wiki. You can organize tests into hierarchal test suites that are editable the way any wiki is. This provides a familiar and friendly interface for non-technical types. All of this adds up to an environment in which tests can be written and organized in such a way that they specify desired functionality before the code is written and serve as documentation after the fact. No more having to interpret written specs into tests and production code.</p>
<p>It takes some training both on the developer&#8217;s part and the customer&#8217;s part but it&#8217;s well worth the effort.</p>
]]></content:encoded>
			<wfw:commentRss>http://delphixtreme.com/wordpress/?feed=rss2&amp;p=32</wfw:commentRss>
		</item>
		<item>
		<title>Think your design is good?</title>
		<link>http://delphixtreme.com/wordpress/?p=23</link>
		<comments>http://delphixtreme.com/wordpress/?p=23#comments</comments>
		<pubDate>Thu, 18 Sep 2008 03:38:13 +0000</pubDate>
		<dc:creator>Jody Dawkins</dc:creator>
		
		<category><![CDATA[XP]]></category>

		<category><![CDATA[design]]></category>

		<category><![CDATA[engineering]]></category>

		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://delphixtreme.com/wordpress/?p=23</guid>
		<description><![CDATA[Take the following test:
1. Take a class, any class and instantiate it in a test harness.
2. Find a coworker, let them read your code and tell you what it does.
3. Count the instances of duplication in your code.
Why take this test?
Number 1.
The ability to instantiate any class in a test harness demonstrates the level of [...]]]></description>
			<content:encoded><![CDATA[<p>Take the following test:</p>
<p>1. Take a class, any class and instantiate it in a test harness.<br />
2. Find a coworker, let them read your code and tell you what it does.<br />
3. Count the instances of duplication in your code.</p>
<p>Why take this test?</p>
<p>Number 1.<br />
The ability to instantiate any class in a test harness demonstrates the level of decoupling and cohesion of your code. Why is that good? Highly decoupled cohesive code promotes: seperation of concerns, low duplication and extensiblity. You end up with only as much code as you need. You also get the power to duplicate and isolate customer found defects before fixing them, giving you a collection of tests you can rely on to prove the integrity of your code and design.</p>
<p>Number 2.<br />
To often we write code that is write-once/read-never. Your code should be expressive of it&#8217;s intent and easy to read. It should do what it says and say what it does. Code spends more time being read and maintained than being originated. Additionally, when you&#8217;re writing highly decoupled code the more readable your code the more likely someone can come behind you and maintain/extend your work.</p>
<p>Number 3.<br />
Duplication is the bane of maintainability. You (and I) have copy-and-pasted code from one unit to another to tweak it for some slightly different functionality. Stop it. Refactor that code into a superclass or into seperate object and use a subclass for specific functionality (see the <a target="_blank" href="http://en.wikipedia.org/wiki/Strategy_pattern">Strategy Pattern</a>). Duplication has more than one face: copy-and-paste (as I just talked about), and reinventing the wheel. On a larger team it easy to reinvent functionality that another team member wrote prior to you. Don&#8217;t be afraid to query your peers when you get that &#8220;someone must have done this before&#8221; feeling. You&#8217;ll reduce the complexity and size of your project by eliminating functional duplication.</p>
<p>How do you get there?</p>
<p>Check out:
<ul>
<li>Eric Evans&#8217;s Domain Driven Design (don&#8217;t be put off by this rather large volume, it&#8217;s worth the read).</li>
<li>Dan North&#8217;s thoughts on Behavior Driven Development - especially the precept of working from the outside in (which lines up with the <a target="_blank" href="http://en.wikipedia.org/wiki/Eat_one%27s_own_dog_food">&#8220;eat your own dog food&#8221;</a> principle).</li>
<li>Martin Fowler&#8217;s Refactoring (great book)</li>
<li>Design Patterns - I really liked Head First Design Patterns by Eric Freeman, et al.</li>
<li>Refactoring to Patterns by Joshua Kerievsky</li>
</ul>
<p>So how did it go? Post your test experience in the comments.</p>
<p><strong><em>Full Disclosure</em></strong><br />
I stood on the shoulders of the following giants to write this post:<br />
<a target="_blank" href="http://www.artima.com/weblogs/viewpost.jsp?thread=71730">Dave Astels</a><br />
<a target="_blank" href="http://www.artima.com/weblogs/viewpost.jsp?thread=42486">Michael Feathers</a></p>
]]></content:encoded>
			<wfw:commentRss>http://delphixtreme.com/wordpress/?feed=rss2&amp;p=23</wfw:commentRss>
		</item>
	</channel>
</rss>
