<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Mark Needham</title>
	
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Wed, 11 Nov 2009 10:30:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/MarkNeedham" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Coding: Pushing the logic back</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/YQWSJ-DYBXY/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/11/coding-pushing-the-logic-back/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 10:30:08 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1813</guid>
		<description><![CDATA[I was reading a post on the law of demeter by Richard Hart recently and it reminded me that a lot of the refactorings that we typically do on code bases are about pushing the logic back into objects instead of exposing data and performing calculations elsewhere.
An example that I spotted where we did this [...]]]></description>
			<content:encoded><![CDATA[<p>I was reading <a href="http://www.ur-ban.com/blog/2009/10/25/law-of-demeter-and-the-delegate-method/">a post on the law of demeter by Richard Hart</a> recently and it reminded me that a lot of the refactorings that we typically do on code bases are about pushing the logic back into objects instead of exposing data and performing calculations elsewhere.</p>
<p>An example that I spotted where we did this recently was while building a 'BusinessSummary' object whose state was based on the state of a collection of other objects.</p>
<p>The code was keeping a count of the various states of the business objects:</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> BusinessSummary buildSummaryObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	BusinessSummary businessSummary <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BusinessSummary<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>BusinessObject businessObject <span style="color: #339933;">:</span> businessObjects<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		State state <span style="color: #339933;">=</span> businessObject.<span style="color: #006633;">getState</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>State.<span style="color: #006633;">STATE_1</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>state<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			businessSummary.<span style="color: #006633;">incrementState1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>State.<span style="color: #006633;">STATE_2</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>state<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			businessSummary.<span style="color: #006633;">incrementState2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// and so on</span>
		<span style="color: #009900;">&#125;</span>					
	<span style="color: #009900;">&#125;</span>	
	<span style="color: #000000; font-weight: bold;">return</span> businessSummary<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this example 'State' is an enum representing the state the 'BusinessObject' is currently in.</p>
<p>Our first change to the code was to push the logic around state back into the 'BusinessObject' which results in the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> BusinessSummary buildSummaryObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	BusinessSummary businessSummary <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BusinessSummary<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>BusinessObject businessObject <span style="color: #339933;">:</span> businessObjects<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>businessObject.<span style="color: #006633;">hasState1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			businessSummary.<span style="color: #006633;">incrementState1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>businessObject.<span style="color: #006633;">hasState2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			businessSummary.<span style="color: #006633;">incrementState2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// and so on</span>
		<span style="color: #009900;">&#125;</span>					
	<span style="color: #009900;">&#125;</span>	
	<span style="color: #000000; font-weight: bold;">return</span> businessSummary<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BusinessObject <span style="color: #009900;">&#123;</span>
	...
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> hasState1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> State.<span style="color: #006633;">STATE_1</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>state<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I think this is an improvement but it still isn't following the <a href="http://www.pragprog.com/articles/tell-dont-ask">tell don't ask</a> principle, we're just asking to be told about the data instead of asking for the data itself.</p>
<p>We didn't have the time to do the next change where we would have got rid of the 'hasState' methods and just got the 'BusinessObject' to update the 'BusinessSummary' itself:</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> BusinessSummary buildSummaryObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	BusinessSummary businessSummary <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BusinessSummary<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>BusinessObject businessObject <span style="color: #339933;">:</span> businessObjects<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		businessObject.<span style="color: #006633;">writeTo</span><span style="color: #009900;">&#40;</span>businessSummary<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>					
	<span style="color: #009900;">&#125;</span>	
	<span style="color: #000000; font-weight: bold;">return</span> businessSummary<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BusinessObject <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> State state<span style="color: #339933;">;</span>
	...
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> writeTo<span style="color: #009900;">&#40;</span>BusinessSummary businessSummary<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>State.<span style="color: #006633;">STATE_1</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>state<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			businessSummary.<span style="color: #006633;">incrementState1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>State.<span style="color: #006633;">STATE_2</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>state<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			businessSummary.<span style="color: #006633;">incrementState2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// and so on</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Looking back on this code having written this post I wonder whether we need to do the loop or whether it would be better to pass the collection of 'businessObjects' to the 'BusinessSummary' and let it take care of that logic instead.</p>
<p>If we did that then we would need to expose 'hasState1&#8242; and 'hasState2&#8242; methods on 'BusinessObject' like on the first refactoring so that we could work out how to populate 'BusinessSummary'.</p>
<p>I prefer the solution where 'BusinessObject' writes to the 'BusinessSummary' although it seems like 'BusinessObject' now has more than one reason to change &#8211; if it changes or if 'BusinessSummary' changes. This would violate the <a href="http://blogs.agilefaqs.com/2009/10/19/single-responsibility-principle-demystified/">single responsibility principle</a></p>
<p>I discussed this with <a href="http://intwoplacesatonce.com/">Dave</a> and he pointed out that as long as the contract that 'BusinessSummary' and 'BusinessObject' have &#8211; i.e. the 'incrementState1&#8242; and 'incrementState2&#8242; methods &#8211; stays the same then it wouldn't really be a problem.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/YQWSJ-DYBXY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/11/coding-pushing-the-logic-back/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/11/11/coding-pushing-the-logic-back/</feedburner:origLink></item>
		<item>
		<title>Legacy Code: Sensing</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/7bO5AFwq4DA/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/10/legacy-code-sensing/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 20:33:22 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[legacy-code]]></category>
		<category><![CDATA[michael-feathers]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1808</guid>
		<description><![CDATA[In 'Working Effectively With Legacy Code' Michael Feathers describes two reasons for wanting to break dependencies in our code &#8211; to allow separation and sensing.
The former describes the need to get a piece of code into a test harness while the latter describes the need to assert whether that piece of code is doing what [...]]]></description>
			<content:encoded><![CDATA[<p>In '<a href="http://www.amazon.com/gp/product/0131177052?ie=UTF8&#038;tag=marneesblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0131177052">Working Effectively With Legacy Code</a>' Michael Feathers describes two reasons for wanting to break dependencies in our code &#8211; to allow <a href="http://www.markhneedham.com/blog/2009/10/20/book-club-working-effectively-with-legacy-code-chapters-34-5-michael-feathers/">separation and sensing</a>.</p>
<p>The former describes the need to get a piece of code into a test harness while the latter describes the need to assert whether that piece of code is doing what we want it to.</p>
<p>On the projects I've worked on we've tended to run into problems with the latter more frequently and Matt and I actually ran into this problem when we were <a href="http://www.markhneedham.com/blog/2009/10/18/coding-role-based-interfaces/">refactoring some code into a role based interface approach</a>.</p>
<p>We started with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ApplicationController <span style="color: #008000;">:</span> Controller
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> BusinessType
	<span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> GetType<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Controller&quot;</span>, <span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnActionExecuting<span style="color: #000000;">&#40;</span>ActionExecutingContext context<span style="color: #000000;">&#41;</span> 
	<span style="color: #000000;">&#123;</span>
    		<span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnActionExecuting</span><span style="color: #000000;">&#40;</span>context<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		ViewData<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;SomeViewDataKey&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> CreateThatViewDataStuff<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">private</span> ViewDataStuff CreateThatViewDataStuff<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> ViewDataStuff 
		<span style="color: #000000;">&#123;</span>
			Content <span style="color: #008000;">=</span> BuildContent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #008080; font-style: italic;">// and so on</span>
		<span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">private</span> IContent BuildContent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>BusinessType <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;BusinessType1&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">return</span> CreateContentForBusinessType1<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>BusinessType <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;BusinessType2&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">return</span> CreateContentForBusinessType2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">else</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">return</span> CreateContentForEveryOtherBusinessType<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The 'BuildContent' method is the one that we're really interested in here but it's a private method on the controller so we currently don't have an easy way to assert that it's being created correctly.</p>
<p>In order to test that functionality more easily we decided to make the method public so we could just call it directly. </p>
<p>We wanted to do this so that we could write some tests around this bit of functionality before we refactored it so that we could be sure we hadn't broken the way it worked while doing so.</p>
<p>As <a href="http://hamletdarcy.blogspot.com/2009/06/forgotten-refactorings.html">Hamlet D'Arcy points out, if we're going to call it refactoring then we need to make sure that we're protected by tests</a>. Otherwise we're just changing stuff.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ShouldCreateContent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	var someRepository <span style="color: #008000;">=</span> MockRepository.<span style="color: #0000FF;">CreateMock</span><span style="color: #008000;">&lt;</span>ISomeRepository<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var controller <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BusinessType1Controller<span style="color: #000000;">&#40;</span>someRepository<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	someRepository.<span style="color: #0000FF;">Expect</span><span style="color: #000000;">&#40;</span>s <span style="color: #008000;">=&gt;</span> s.<span style="color: #0000FF;">GetBusinessType1Message</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">Return</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;someValue&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var content <span style="color: #008000;">=</span> controller.<span style="color: #0000FF;">BuildContent</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// and so on	</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We did the same for the other business types as well.</p>
<p>I normally don't like making private methods public but we didn't intend to checkin our code until the refactoring was complete at which point the method would be made private again. </p>
<p>The code ended up something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> BusinessType1Controller <span style="color: #008000;">:</span> ApplicationController
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> IContent CreateContent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> BusinessType1Content<span style="color: #000000;">&#40;</span>someRepository<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> BusinessType1Content <span style="color: #008000;">:</span> IContent
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> ISomeRepository someRepository<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> BusinessType1Content<span style="color: #000000;">&#40;</span>ISomeRepository someRepository<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">someRepository</span> <span style="color: #008000;">=</span> someRepository<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> GetMessage<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// this would be a different repository call for other business types</span>
		<span style="color: #0600FF;">return</span> someRepository. <span style="color: #0000FF;">GetBusinessType1Message</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We were able to create a test against the business types directly instead of having to write a test against the controller:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ShouldCreateContent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	var someRepository <span style="color: #008000;">=</span> MockRepository.<span style="color: #0000FF;">CreateMock</span><span style="color: #008000;">&lt;</span>ISomeRepository<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var content <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Content<span style="color: #000000;">&#40;</span>someRepository<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	someRepository.<span style="color: #0000FF;">Expect</span><span style="color: #000000;">&#40;</span>s <span style="color: #008000;">=&gt;</span> s.<span style="color: #0000FF;">GetBusinessType1Message</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">Return</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;someValue&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var message <span style="color: #008000;">=</span> controller.<span style="color: #0000FF;">GetMessage</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// and so on	</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Having got these new tests passing the ones we'd written against 'BuildContent' now also worked and since they were effectively testing the same thing we were able to get rid of them and make 'BuildContent' private again.</p>
<p>Despite the fact that I was initially reluctant to expose the 'BuildContent' method this approach seemed to work out reasonably well.</p>
<p>An alternative approach would have been to create a test only ApplicationController and then create a method on that which called the 'OnActionExecuting' method.</p>
<p>We've done that to test some other things and it would have allowed us to test the creation of 'Content' in a more roundabout way without needing to make 'BuildContent' public.</p>
<p>The additional effort involved in doing that for not much gain means that given a similar situation in the future I'd probably use just make the method public again!</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/7bO5AFwq4DA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/10/legacy-code-sensing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/11/10/legacy-code-sensing/</feedburner:origLink></item>
		<item>
		<title>Coding: The agent noun class</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/WT5Gn_SwPG8/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/08/coding-the-agent-noun-class/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 10:44:18 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1803</guid>
		<description><![CDATA[I refer quite frequently to a post written by my colleague Peter Gillard Moss where he describes the agent noun code smell for class names.
An agent noun is defined by Wikipedia as:

In linguistics, an agent noun (or nomen agentis) is a word that is derived from another word denoting an action, and that identifies an [...]]]></description>
			<content:encoded><![CDATA[<p>I refer quite frequently to a post written by my colleague Peter Gillard Moss where he describes the <a href="http://jupitermoonbeam.blogspot.com/2008/09/agent-nouns-are-code-smells.html">agent noun code smell for class names</a>.</p>
<p>An agent noun is <a href="http://en.wikipedia.org/wiki/Agent_noun">defined by Wikipedia</a> as:</p>
<blockquote><p>
In linguistics, an agent noun (or nomen agentis) is a word that is derived from another word denoting an action, and that identifies an entity that does that action.
</p></blockquote>
<p>Some typical examples of this are classes which end in the name 'Manager', 'Retriever', 'Helper' or even 'Controller' <a href="http://www.lixo.org/archives/2008/09/12/opportunity-makes-the-thief/">as Carlos points out</a>.</p>
<p>It's often the case that these classes better describe a method which belongs on another object and I quite like Peter's idea that agent nouns are useful for describing the <a href="http://www.markhneedham.com/blog/2009/10/18/coding-role-based-interfaces/">roles that our objects carry out</a>. </p>
<p>We came across an interesting problem related to this a while ago where we wanted to calculate the age of a given customer and then send that data to another web page where it would be displayed.</p>
<p>The date of birth was an attribute on the 'Customer' object and we didn't need any of the other attributes of a customer on this web page so my first thought was that we needed an 'AgeCalculator' class.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> AgeCalculator 
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IClock clock<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> AgeCalculator<span style="color: #000000;">&#40;</span>IClock clock<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">clock</span> <span style="color: #008000;">=</span> clock<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> CalculateAge<span style="color: #000000;">&#40;</span>DateTime dataOfBirth<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// do some calculation with clock &amp; date of birth here</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SomeMethod<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	var age <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> AgeCalculator<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> Clock<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">CalculateAge</span><span style="color: #000000;">&#40;</span>customer.<span style="color: #0000FF;">DateOfBirth</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">// and so on </span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This approach worked quite well as it allowed us to easily test the age calculation logic in isolation and we could then pass on the age returned to the web page.</p>
<p>The thing I didn't like about this solution is that it seems a lot like an agent noun class. </p>
<p>The giveaway in this case is that the method name is just a variation on the class name. This suggests that this behaviour belongs on another object instead.</p>
<p>It's not as object oriented as it could be but it doesn't seem as bad as some of the other examples such as classes ending in 'Manager' since this object is only solving one problem and we do have calculators in real life.</p>
<p>Some colleagues suggested that perhaps age should be an attribute on the customer which does seem to be a better place for this logic to reside.</p>
<p>On the other hand age isn't used anywhere else in the application and we don't need any of the other attributes of customer as I mentioned earlier.</p>
<p>I'm not sure what the 'right' solution is but there seem to be a few different potential approaches:</p>
<ul>
<li>Should age be an attribute of the customer? There could be a tiny type 'Age' which does the above logic.</li>
<li>Should we have a role/interface 'ICalculateAges' which the customer implements?</li>
<li>Should we just keep the 'AgeCalculator'?</li>
<li>Is there some other solution that I'm not seeing?!</li>
</ul>
<p>&#8211;</p>
<p>In reality after we'd discussed all these alternatives it turned out that the requirement was slightly wrong and that we actually needed to send down the date of birth and not the age. </p>
<p>I still think it's an interesting problem though and one that I'm bound to come across again!</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/WT5Gn_SwPG8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/08/coding-the-agent-noun-class/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/11/08/coding-the-agent-noun-class/</feedburner:origLink></item>
		<item>
		<title>Knowing when to persevere and when to change approach</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/J6lCw6dpv_s/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/08/knowing-when-to-persevere-and-when-to-change-approach/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 23:57:41 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1797</guid>
		<description><![CDATA[It strikes me that one of the most important skills to develop in software development is knowing when to keep going with an approach to a problem and when we should stop and try something else.
This situation doesn't always happen because if we have two people available and realise before we start on the task [...]]]></description>
			<content:encoded><![CDATA[<p>It strikes me that one of the most important skills to develop in software development is knowing when to keep going with an approach to a problem and when we should stop and try something else.</p>
<p>This situation doesn't always happen because if we have two people available and realise before we start on the task that there is some doubt as to which solution is the most appropriate then we can adopt a <a href="http://www.markhneedham.com/blog/2009/09/19/set-based-concurrent-engineering-a-simple-example/">set based approach</a> whereby we try out multiple potential solutions in parallel.</p>
<p>However I've found that there are times when we might think that one solution is much better than the alternatives and we will start working on that and discard the alternatives for the moment.</p>
<p>Frequently the approach we choose will solve our problem but on other occasions we can end up getting a bit bogged down when it doesn't work out as expected.</p>
<p>At this stage I often find that my instinct is to try and solve these problems and while I think this is certainly a valid approach it's also very easy to end up <a href="http://www.markhneedham.com/blog/2008/10/25/dont-shave-the-yak-ask-why-are-we-doing-this/">shaving the yak</a>.</p>
<p>This seems to happen much more frequently when working alone and even when we're getting feedback which suggests that what we're trying isn't working we can be reluctant to back track.</p>
<p>I think there is some pride involved as I frequently find myself really wanting to make the current approach work as I believe that a better developer than myself would be able to do so. </p>
<p>Sometimes that is the case but more often than not when I work through the problem with a colleague their suggestion is to try another approach rather than continuing to struggle with the current one.</p>
<p>It doesn't make a lot of sense to spend all our time going down one avenue when our goal is solve a particular problem and the solution is a means to that end.</p>
<p>When discussing this with <a href="http://pilchardfriendly.wordpress.com/">Nick</a> he pointed out that we also need to recognise when we should persevere with the approach.</p>
<p>One way to avoid too much yak shaving is to set a <a href="http://en.wikipedia.org/wiki/Timeboxing">timebox</a> by when we should either have a better idea of whether we are going to be able to solve the problem with this solution or if we need to consider an alternative.</p>
<p>If there's some light at the end of the tunnel with an approach after this period then I would be inclined to keep on going but it's sometimes difficult to tell how close we actually are and how much is wishful thinking!</p>
<p>I still sometimes find myself struggling to decide whether to keep going or change direction so it'd be interesting to know if anyone has ideas around doing so more effectively.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/J6lCw6dpv_s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/08/knowing-when-to-persevere-and-when-to-change-approach/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/11/08/knowing-when-to-persevere-and-when-to-change-approach/</feedburner:origLink></item>
		<item>
		<title>TDD: Useful when new on a project</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/0x3QkU9w7Sw/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/06/tdd-useful-when-new-on-a-project/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 11:57:10 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1794</guid>
		<description><![CDATA[Something which I've noticed over the last few projects that I've worked on is that at the beginning when I don't know very much at all about the code base, domain and so on is that pairing with someone to TDD something seems to make it significantly easier for me to follow what's going on [...]]]></description>
			<content:encoded><![CDATA[<p>Something which I've noticed over the last few projects that I've worked on is that at the beginning when I don't know very much at all about the code base, domain and so on is that pairing with someone to TDD something seems to make it significantly easier for me to follow what's going on than other approaches I've seen.</p>
<p>I thought that it was probably because I'm more used to that approach than any other but in Michael Feathers' description of TDD in '<a href="http://www.amazon.com/gp/product/0131177052?ie=UTF8&#038;tag=marneesblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0131177052">Working Effectively With Legacy Code</a>' he points out the following:</p>
<blockquote><p>
One of the most valuable things about TDD is that it <strong>lets us concentrate on one thing at a time</strong>. We are either writing code or refactoring, we are never doing both at once.
</p></blockquote>
<p>It also temporarily removes us from the feeling of drowning in all the new information which is typical at the start of projects. In a way it reminds me of the '<a href="http://apprenticeship.oreilly.com/wiki/retreat_into_competence">Retreat Into Competence</a>' pattern from '<a href="http://www.amazon.com/gp/product/0596518382?ie=UTF8&#038;tag=marneesblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0596518382">Apprenticeship Patterns</a>' as it gives us a chance to regain composure and take in all the new information.</p>
<p>We can probably make a much more useful contribution if we only need to understand one small piece of the system rather than having to understand everything which can often be the case with what Feathers coins the edit &#038; pray approach to development.</p>
<p>It's still necessary to spend some time trawling around the code base working out how everything fits together but I'm now more aware that it's also really useful to take some time to just focus on one much smaller class or piece of functionality.</p>
<p>Perhaps also something to keep in mind the next time I'm pairing with someone who's new to a project that I've been working on for a while.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/0x3QkU9w7Sw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/06/tdd-useful-when-new-on-a-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/11/06/tdd-useful-when-new-on-a-project/</feedburner:origLink></item>
		<item>
		<title>Consistency in the code base</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/5YtVFPTOXfo/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/04/consistency-in-the-code-base/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 11:39:28 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1791</guid>
		<description><![CDATA[I've had quite a few discussions with various different colleagues about coding consistency over the last year or so and Pat Kua and Frank Trindade have both written posts suggesting that we should look to have coding standards on projects in order to avoid the type of pain that having an inconsistent approach can lead [...]]]></description>
			<content:encoded><![CDATA[<p>I've had quite a few discussions with various different colleagues about coding consistency over the last year or so and <a href="http://www.thekua.com/atwork/2008/11/death-by-a-thousand-differences/">Pat</a> <a href="http://www.thekua.com/atwork/2009/02/what-differences-cause-your-project-death/">Kua </a>and <a href="http://blog.franktrindade.com/2008/10/17/we-need-standards/">Frank Trindade</a> have both written posts suggesting that we should look to have coding standards on projects in order to avoid the type of pain that having an inconsistent approach can lead to.</p>
<p>From what I've noticed there seem to be two reasons that we end up with inconsistent code on projects:</p>
<ol>
<li>People's personal preferences for how certain bits of code should be written vary.</li>
<li>The team is trying to incrementally move towards an improved solution to a problem encountered but isn't completely there yet.</li>
</ol>
<p>I think the first of these is the area which Frank and Pat are addressing in their posts.</p>
<p>However I think there is some overlap between the two. For example someone may rewrite a bit of code in their own style and while they would suggest that what they are doing is improving the code base their team mates may think that what they are doing is merely creating more inconsistency.</p>
<p>Potential coding inconsistency falls into three categories as I see it:</p>
<h3>Personal Preferences</h3>
<p>A simple example of this that we had on the last project I worked on was around how we should write simple conditional statements when used as <a href="http://c2.com/cgi/wiki?GuardClause">guard clauses</a>.</p>
<p>These were the 3 different ways that people preferred and we had all the different styles across the code base!</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> SomeMethod<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>SomeCondition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;hello&quot;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> SomeMethod<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>SomeCondition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> 
		<span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;hello&quot;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> SomeMethod<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>SomeCondition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> 
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;hello&quot;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We eventually agreed to use the last one because it was the least controversial choice and although people who preferred the other two approaches thought it was too verbose they were fine with using that as a compromise.</p>
<p><a href="http://www.thekua.com/atwork/2009/02/what-differences-cause-your-project-death/">Pat covers a lot of the other types of code</a> that I would classify as being linked to personal preference and I think that it would be beneficial to have coding standards/agreement in the team for the patterns we favour/names we're going to use in this area.</p>
<p>It's way easier to just go and write code in your own personal style and I do this way too frequently but it's much better for the team if we follow a common style.</p>
<h3>Improving the code but perhaps not significantly</h3>
<p>I've written previously about the way that we've been making use of a '<a href="http://www.markhneedham.com/blog/2009/06/02/coding-putting-code-where-people-can-find-it/">GetBuilderFor</a>' class to hang our test builders off and while this worked quite well <a href="http://www.markhneedham.com/blog/2009/08/15/builders-hanging-off-class-vs-builders-in-same-namespace/">there is a better way</a> to do this which requires less code being written overall.</p>
<p>We can make use of C#'s object initializer syntax to setup fields with values instead of having to create methods to do that and we don't need to write the code to make the builder hang off 'GetBuilderFor'.</p>
<p>As long as we put all the builders in the same namespace we can just type 'new BuilderNameSpace.' and then the IDE will show us the choices of builder that we have.</p>
<p>The problem we experienced was that that we already had 30-40 builders written using the 'GetBuilderFor' approach and there wasn't a significant advantage to be gained by going and rewriting all that code to use the new approach.</p>
<p>In the interests of consistency we therefore decided to keep using the 'GetBuilderFor' approach even though the next time I come across this problem I'd definitely favour the other approach.</p>
<p>Reg Braithwaite has a really interesting post in which he talks about '<a href="http://weblog.raganwald.com/2008/05/narcissism-of-small-code-differences.html">the narcissism of small code differences</a>' and how different authors 'improve' the code by adding their own personal touch.</p>
<p>I think he describes the type of code changes that I would classify in this and the previous category although it does make me wonder where the line between refactoring and adding your own personal touch to a piece of code lies. </p>
<h3>Improving the code significantly</h3>
<p>These are the code changes that may create inconsistency but help us to drive a design improvement which will have a big impact on the way we work.</p>
<p>An example of this that we had on my last project was moving <a href="http://www.markhneedham.com/blog/2008/12/28/internalexternal-domain-models/">our 'domain' model from being defined in WCF messages</a> to being defined independently of other layers in the system.</p>
<p>We wanted to make this change to make the dependency between our code base and the service layer more loosely coupled and we started making that change around about February.</p>
<p>It took maybe a month to move the most awkward parts across since we did it bit by bit in an incremental way rather than stopping everything and making that change.</p>
<p>Along the way the code in these areas was in an inconsistent state and anyone looking at the code base who didn't know the reason why it was like that would think it was truly insane! </p>
<p>Even now there are still bits of code which are more reminiscent of the old approach than the new and these tend to get changed when people are working in that area.</p>
<p>I think this type of inconsistency is somewhat inevitable if we're incrementally making improvements to our code base or <a href="http://www.informit.com/articles/article.aspx?p=1235624&#038;seqNum=6">following the boy scout rule</a> as Uncle Bob puts it.</p>
<h3>In Summary</h3>
<p>These are my current observations on coding inconsistency but I'm sure there are other factors which can affect this as well.</p>
<p>I think we should look to reduce the inconsistencies in the first two categories as much as possible and ensure that everyone is driving towards the same outcome with respect to the last category.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/5YtVFPTOXfo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/04/consistency-in-the-code-base/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/11/04/consistency-in-the-code-base/</feedburner:origLink></item>
		<item>
		<title>Reading Code: Unity</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/WjKvBuuIisw/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/04/reading-code-unity/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 15:22:56 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Reading Code]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1785</guid>
		<description><![CDATA[I spent a bit of time reading some of the Unity code base recently and I decided to try out a variation of Michael Feathers 'Effect Sketching' which my colleague Dave Cameron showed me.
'Effect Sketching' is a technique Feathers describes in 'Working Effectively With Legacy Code' and the idea is that we  sketch a [...]]]></description>
			<content:encoded><![CDATA[<p>I spent a bit of time reading some of the <a href="http://www.codeplex.com/unity/">Unity</a> code base recently and I decided to try out a variation of Michael Feathers 'Effect Sketching' which my colleague <a href="http://intwoplacesatonce.com/">Dave Cameron</a> showed me.</p>
<p>'Effect Sketching' is a technique Feathers describes in '<a href="http://www.amazon.co.uk/Working-Effectively-Legacy-Robert-Martin/dp/0131177052/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1256852443&#038;sr=8-1">Working Effectively With Legacy Code</a>' and the idea is that we  sketch a diagram showing the interactions between the fields and methods in a specific class while browsing through the code.</p>
<p>Dave suggested doing a similar thing but with methods and classes instead while stepping through the code with the debugger.</p>
<p>I set up this code to step my way through:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">var container <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UnityContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
container.<span style="color: #0000FF;">AddNewExtension</span><span style="color: #008000;">&lt;</span>Interception<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
container.<span style="color: #0000FF;">RegisterType</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>IIDProvider<span style="color: #000000;">&#41;</span>, <span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>HttpContextIDProvider<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
container.<span style="color: #0000FF;">Configure</span><span style="color: #008000;">&lt;</span>Interception<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">SetDefaultInterceptorFor</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>GetPaymentBreakdownsService<span style="color: #000000;">&#41;</span>, <span style="color: #008000;">new</span> VirtualMethodInterceptor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #FF0000;">object</span> resolve <span style="color: #008000;">=</span> container.<span style="color: #0000FF;">Resolve</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>GetPaymentBreakdownsService<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>These are some of my observations from this exercise:</p>
<ul>
<li>I found it was <strong>much easier for me to remember where I was in the call stack</strong> then normal.
<p>I often get quite engrossed in the individual method calls on objects that I forget where the code actually started before it ended up in the current location. Whenever this happened I was able to refer to my sketch to remind myself of where the code had started from. </li>
<li>Despite having the drawing I still got a bit lost when the <a href="http://unity.codeplex.com/sourcecontrol/changeset/view/38085?projectName=unity#616462">PolicyInjectionBehaviourDescriptor</a> made a call back to the container's Resolve method which meant that the code went through the same path that I'd just followed:

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> IInterceptionBehavior GetInterceptionBehavior<span style="color: #000000;">&#40;</span>
            IInterceptor interceptor,
            Type interceptedType,
            Type implementationType,
            IUnityContainer container<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            InjectionPolicy<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> policies <span style="color: #008000;">=</span> container.<span style="color: #0000FF;">Resolve</span><span style="color: #008000;">&lt;</span>InjectionPolicy<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>I was getting really confused watching various different injection policies being resolved instead of the type I was trying to resolve!</li>
<li>I've previously tried drawing diagrams which just contained the classes in a code base but I've found that including the methods that connect them makes it easier for me to understand what's going on.
<p>I've been drawing these diagrams out by hand but I thought I'd translate some of it into dot notation so that I could create a version using <a href="http://www.graphviz.org/">graphviz</a> to show on here.</p>
<p><img src="http://www.markhneedham.com/blog/wp-content/uploads/2009/11/unity.png" alt="unity.png" border="0" width="653" height="613" /></p>
<p>The nodes in orange represent classes and the dotted line represents where an event was fired.
</li>
<li>I realised that I still need to spend more time reading code so that I know <strong>when to dive into an object and when the details are probably not important</strong>. At the moment I'm too prone to getting distracted by wanting to see how a specific method works instead of looking at those details later on when I actually need to know. </li>
<li>I felt as I was reading the code that in a lot of places the <a href="http://www.markhneedham.com/blog/2009/01/02/f-option-types/">option type</a> from functional programming would have come in quite useful. There is often code similar to this bit from LifeTimeStrategy:

<div class="wp_syntax"><div class="code"><pre>object existing = lifetimePolicy.GetValue()<SEMI>
if (existing != null)
{
    context.Existing = existing<SEMI>
    context.BuildComplete = true<SEMI>
}</pre></div></div>

<p>Instead of existing returning a null it could return 'None' to indicate it hasn't been resolved yet.</li>
<li>I've read about the '<a href="http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx">yield</a>' construct before but I've never seen a need to use it yet in any code I've written so it was interesting to see it being used quite frequently inside <a href="http://unity.codeplex.com/sourcecontrol/changeset/view/38085?projectName=unity#616459">PolicySet</a>:

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> IEnumerable<span style="color: #008000;">&lt;</span>InjectionPolicy<span style="color: #008000;">&gt;</span> GetPoliciesFor<span style="color: #000000;">&#40;</span>MethodImplementationInfo member<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>InjectionPolicy policy <span style="color: #0600FF;">in</span> <span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>policy.<span style="color: #0000FF;">Matches</span><span style="color: #000000;">&#40;</span>member<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            yield <span style="color: #0600FF;">return</span> policy<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>From my understanding of this construct it seems like it acts in a similar way to a stream i.e. it's only evaluated when it's actually needed. </li>
<li>In the '<a href="http://www.amazon.co.uk/Fundamentals-Object-oriented-Design-Object-Technology/dp/020169946X/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1257261364&#038;sr=8-1">Fundamentals of Object Oriented Design in Uml</a>' Meilir Page Jones suggests that we might want to avoid replicated behaviour in our public APIs since it leads to greater complexity.
<p>This would therefore seem to suggest that having overloads of methods on an object is something to be avoided. Interestingly in this code base the overloads for 'UnityContainer' are actually defined as extension methods which then delegate back to 'UnityContainer' and pass in default values for unspecified parameters. </p>
<p>This seems like quite a neat way of getting around the problem since we keep the API clean while also providing users of the code an easy way to do so.</li>
</ul>
<p>On the subject of reading code I recently came across an <a href="http://designbygravity.wordpress.com/2009/10/23/how-to-read-other-peoples-code-and-why/">interesting post by designbygravity which describes some approaches for reading code more effectively</a>.</p>
<p>In particular I really liked the section about not hating the code :</p>
<blockquote><p>
You can get sucked into hating the code, merely because it is not yours. Software people tend to be equipped with ample egos, and other people’s code can offend. But realize, their working code is better than your imagined code, because their working code exists right now. So put your ego aside and learn the code in front of you.
</p></blockquote>
<p>It's so easy to drift into this mindset but it's rarely helpful so if we can stop ourselves it's almost certainly beneficial.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/WjKvBuuIisw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/04/reading-code-unity/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/11/04/reading-code-unity/</feedburner:origLink></item>
		<item>
		<title>Book Club: Working Effectively With Legacy Code – Chapter 8 (Michael Feathers)</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/QYRDJNvoicI/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/03/book-club-working-effectively-with-legacy-code-chapter-8-michael-feathers/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 14:16:32 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Book Club]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1781</guid>
		<description><![CDATA[In our latest technical book club we discussed chapter 8 &#8211; 'How do I add a feature?' &#8211; of Michael Feather's 'Working Effectively With Legacy Code'.
This chapter covers Test Driven Development and a technique I hadn't come across before called Programming By Difference.
These are some of my thoughts and our discussion of the chapter:

In the [...]]]></description>
			<content:encoded><![CDATA[<p>In our latest technical book club we discussed chapter 8 &#8211; 'How do I add a feature?' &#8211; of Michael Feather's '<a href="http://www.amazon.co.uk/Working-Effectively-Legacy-Robert-Martin/dp/0131177052/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1257168892&#038;sr=8-1">Working Effectively With Legacy Code</a>'.</p>
<p>This chapter covers Test Driven Development and a technique I hadn't come across before called <a href="http://www.springerlink.com/content/f87155k53852761j/">Programming By Difference</a>.</p>
<p>These are some of my thoughts and our discussion of the chapter:</p>
<ul>
<li>In the section on TDD Feathers mentions the <a href="http://www.markhneedham.com/blog/2009/10/31/coding-copypaste-then-refactor/">copy/paste/refactor pattern</a> which I wrote about a few days ago. <a href="http://camswords.wordpress.com/">Cam</a> pointed out that this technique allows us to get to the green bar more quickly after which we can decide how to clean up the code. It seems like perhaps this could be added to Kent Beck's 'Green Bar Patterns' that he describes in '<a href="http://www.markhneedham.com/blog/2008/10/07/test-driven-development-by-example-book-review/">Test Driven Development by Example</a>'.
<p>I've noticed a few times recently that delaying our desire to remove duplication until we can properly see where the duplication lies might be a better strategy than prematurely removing duplication and creating a meaningless abstraction which can be difficult to remove.</li>
<li>From the examples in the book, <strong>programming by difference</strong> is used to describe an approach where we create a sub class and override some aspect of the super class. It allows us to quickly add new features although it does increase the complexity of the system since we have to add a new class to achieve this.
<p>The next step after this is to refactor the code into a state where the new class' behaviour is moved elsewhere or the new class is shaped into something that makes more sense.</p>
<p>Feathers also points out that while programming by difference may initially lead to our code being in worse shape it can also allow us to see new abstractions which we can then work towards. </li>
<li>I like the term '<strong>normalised hierarchy</strong>' with respect to the way that we create inheritance hierarchies. Feathers suggests that we should look to create a hierarchy where no class has more than one implementation of a method. i.e. <a href="http://www.markhneedham.com/blog/2009/05/06/c-using-virtual-leads-to-confusion/">if a class has a concrete implementation of a method then that shouldn't be overriden</a>.</li>
<li>Towards the end of the chapter Feathers asks whether it is overkill to create a particular class which is just acting like a properties collection at the time. We discussed this and Cam pointed out that we seem to either end up with 'complex interactions and simple objects' or 'more complicated objects and simple interactions'.
<p>From my observations it seems that <a href="http://www.markhneedham.com/blog/2009/10/23/coding-the-primitive-obsession/">we err on the side of not creating enough objects</a> but <a href="http://blogs.agilefaqs.com/2009/10/26/goodbye-simplicity-im-object-obsessed/">as Naresh Jain points out it is possible to go too far the other way and end up creating objects just for the sake of it</a>. </li>
</ul>
<p>Michael Feathers recently linked to a paper titled '<a href="http://bit.ly/1gFXr6">Testable Java</a>' that he's currently working on which covers some of the ideas from the book but with examples in Java. Worth a look.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/QYRDJNvoicI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/03/book-club-working-effectively-with-legacy-code-chapter-8-michael-feathers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/11/03/book-club-working-effectively-with-legacy-code-chapter-8-michael-feathers/</feedburner:origLink></item>
		<item>
		<title>Coding: Copy/Paste then refactor</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/dvk3DaZlOq0/</link>
		<comments>http://www.markhneedham.com/blog/2009/10/31/coding-copypaste-then-refactor/#comments</comments>
		<pubDate>Sat, 31 Oct 2009 07:54:31 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1777</guid>
		<description><![CDATA[We're currently reading Michael Feathers 'Working Effectively With Legacy Code' in our technical book club and one interesting technique which he describes in the Test Driven Development section is copying and pasting some existing code, changing the appropriate part to make the test pass before refactoring to remove the duplication we just created.
I can't remember [...]]]></description>
			<content:encoded><![CDATA[<p>We're currently reading Michael Feathers '<a href="http://www.amazon.co.uk/Working-Effectively-Legacy-Robert-Martin/dp/0131177052/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1256973419&#038;sr=8-1">Working Effectively With Legacy Code</a>' in our <a href="http://www.markhneedham.com/blog/category/books/">technical book club</a> and one interesting technique which he describes in the Test Driven Development section is copying and pasting some existing code, changing the appropriate part to make the test pass before refactoring to remove the duplication we just created.</p>
<p>I can't remember coming across this approach previously but I found myself using it to solve a Scala problem last week.</p>
<p>I wanted to find the smallest number in a list having previously written the code to find the largest number in a list. </p>
<p>This was the code I already had:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="scala">  <span style="color: #0000ff; font-weight: bold;">def</span> max<span style="color: #F78811;">&#40;</span>values <span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span><span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> inner<span style="color: #F78811;">&#40;</span>remainingValues <span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span><span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#93;</span>, currentMax <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span> <span style="color: #000080;">=</span> remainingValues <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> Nil <span style="color: #000080;">=&gt;</span> currentMax
      <span style="color: #0000ff; font-weight: bold;">case</span> head<span style="color: #000080;">::</span>tail <span style="color: #0000ff; font-weight: bold;">if</span> head <span style="color: #000080;">&gt;</span> currentMax <span style="color: #000080;">=&gt;</span> inner<span style="color: #F78811;">&#40;</span>tail, head<span style="color: #F78811;">&#41;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #000080;">_::</span>tail <span style="color: #000080;">=&gt;</span> inner<span style="color: #F78811;">&#40;</span>tail, currentMax<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
    inner<span style="color: #F78811;">&#40;</span>values, Math.<span style="color: #000000;">MIN_INT</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>I figured finding the smallest value would be quite similar to that but I couldn't quite see where the duplication between the two functions was going to be.</p>
<p>I somewhat reluctantly found myself copying and pasting my way to the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="scala">  <span style="color: #0000ff; font-weight: bold;">def</span> min<span style="color: #F78811;">&#40;</span>values <span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span><span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
     <span style="color: #0000ff; font-weight: bold;">def</span> inner<span style="color: #F78811;">&#40;</span>remainingValues <span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span><span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#93;</span>, currentMin <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span> <span style="color: #000080;">=</span> remainingValues <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> Nil <span style="color: #000080;">=&gt;</span> currentMin
      <span style="color: #0000ff; font-weight: bold;">case</span> head<span style="color: #000080;">::</span>tail <span style="color: #0000ff; font-weight: bold;">if</span> head <span style="color: #000080;">&lt;</span> currentMin <span style="color: #000080;">=&gt;</span> inner<span style="color: #F78811;">&#40;</span>tail, head<span style="color: #F78811;">&#41;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #000080;">_::</span>tail <span style="color: #000080;">=&gt;</span> inner<span style="color: #F78811;">&#40;</span>tail, currentMin<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
    inner<span style="color: #F78811;">&#40;</span>values, Math.<span style="color: #000000;">MAX_INT</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Having done that it became clearer that the duplication between the two functions was on line 4 where we do the comparison of values and on line 7 where we choose the initial max/min value.</p>
<p>My first attempt at refactoring was to try and pull out the comparator function on line 4:</p>

<div class="wp_syntax"><div class="code"><pre class="scala"><span style="color: #0000ff; font-weight: bold;">def</span> comparator<span style="color: #F78811;">&#40;</span>x <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span>, y <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span>, f <span style="color: #000080;">:</span> Function2<span style="color: #F78811;">&#91;</span><span style="color: #9999cc; font-weight: bold;">Int</span>, <span style="color: #9999cc; font-weight: bold;">Int</span>, <span style="color: #9999cc; font-weight: bold;">Boolean</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Boolean</span> <span style="color: #000080;">=</span> f<span style="color: #F78811;">&#40;</span>x,y<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>Which resulted in the 'max' function looking like this:</p>

<div class="wp_syntax"><div class="code"><pre class="scala">  <span style="color: #0000ff; font-weight: bold;">def</span> max<span style="color: #F78811;">&#40;</span>values <span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span><span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> inner<span style="color: #F78811;">&#40;</span>remainingValues <span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span><span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#93;</span>, currentMax <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span> <span style="color: #000080;">=</span> remainingValues <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> Nil <span style="color: #000080;">=&gt;</span> currentMax
      <span style="color: #0000ff; font-weight: bold;">case</span> head<span style="color: #000080;">::</span>tail <span style="color: #0000ff; font-weight: bold;">if</span> comparator<span style="color: #F78811;">&#40;</span>head, currentMax, <span style="color: #F78811;">&#40;</span>x,y<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> x <span style="color: #000080;">&gt;</span> y<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> inner<span style="color: #F78811;">&#40;</span>tail, head<span style="color: #F78811;">&#41;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #000080;">_::</span>tail <span style="color: #000080;">=&gt;</span> inner<span style="color: #F78811;">&#40;</span>tail, currentMax<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
    inner<span style="color: #F78811;">&#40;</span>values, Math.<span style="color: #000000;">MIN_INT</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span></pre></div></div>

<p>At this point I realised that I was doing the refactoring the wrong way around and that I should be trying to make 'max' and 'min' call another function while passing in the differences as parameters to that function.</p>
<p>I ended up with this common function:</p>

<div class="wp_syntax"><div class="code"><pre class="scala">   <span style="color: #0000ff; font-weight: bold;">def</span> findValue<span style="color: #F78811;">&#40;</span>values <span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span><span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#93;</span>, comparisonFunction <span style="color: #000080;">:</span> Function2<span style="color: #F78811;">&#91;</span><span style="color: #9999cc; font-weight: bold;">Int</span>, <span style="color: #9999cc; font-weight: bold;">Int</span>, <span style="color: #9999cc; font-weight: bold;">Boolean</span><span style="color: #F78811;">&#93;</span>, startingValue <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> inner<span style="color: #F78811;">&#40;</span>remainingValues <span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span><span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#93;</span>, current <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span> <span style="color: #000080;">=</span> remainingValues <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> Nil <span style="color: #000080;">=&gt;</span> current
      <span style="color: #0000ff; font-weight: bold;">case</span> head<span style="color: #000080;">::</span>tail <span style="color: #0000ff; font-weight: bold;">if</span> comparisonFunction<span style="color: #F78811;">&#40;</span>head, current<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> inner<span style="color: #F78811;">&#40;</span>tail, head<span style="color: #F78811;">&#41;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #000080;">_::</span>tail <span style="color: #000080;">=&gt;</span> inner<span style="color: #F78811;">&#40;</span>tail, current<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
    inner<span style="color: #F78811;">&#40;</span>values, startingValue<span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="scala">  <span style="color: #0000ff; font-weight: bold;">def</span> max<span style="color: #F78811;">&#40;</span>values <span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span><span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span> <span style="color: #000080;">=</span> findValue<span style="color: #F78811;">&#40;</span>values, <span style="color: #F78811;">&#40;</span>x,y<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> x<span style="color: #000080;">&gt;</span>y, Math.<span style="color: #000000;">MIN_INT</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> min<span style="color: #F78811;">&#40;</span>values <span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span><span style="color: #9999cc; font-weight: bold;">Int</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> <span style="color: #9999cc; font-weight: bold;">Int</span> <span style="color: #000080;">=</span> findValue<span style="color: #F78811;">&#40;</span>values, <span style="color: #F78811;">&#40;</span>x,y<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> x<span style="color: #000080;">&lt;</span>y, Math.<span style="color: #000000;">MAX_INT</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>I'm not sure whether the name 'findValue' is a very good one for the common function but I can't think of a better one at the moment.</p>
<p>The neat thing about this approach is that it made it much easier to <a href="http://www.markhneedham.com/blog/2009/08/30/coding-group-the-duplication-then-remove-it/">see where the duplication was</a> and I was able to have both the functions working before trying to do that refactoring. </p>
<p>I'm not sure if there are built in functions to do these calculations. I looked quickly and couldn't find any and thought it would be an interesting exercise to write the code anyway.</p>
<p>It worked out much better than I thought it would.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/dvk3DaZlOq0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/10/31/coding-copypaste-then-refactor/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/10/31/coding-copypaste-then-refactor/</feedburner:origLink></item>
		<item>
		<title>Coding: Invariant checking on dependency injected components</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/X3vSpJbEuok/</link>
		<comments>http://www.markhneedham.com/blog/2009/10/31/coding-invariant-checking-on-dependency-injected-components/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 17:00:40 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1774</guid>
		<description><![CDATA[I've written a couple of times previously about invariant checking in constructors and I had an interesting discussion with some colleagues recently around doing this type of defensive programming when the object in question has its dependencies injected by a container.
Quite often we would see code similar to this in a controller:

public class SomeController 
&#123;
	public [...]]]></description>
			<content:encoded><![CDATA[<p>I've written a couple of times previously about <a href="http://www.markhneedham.com/blog/2009/10/29/coding-consistency-when-invariant-checking/">invariant checking</a> <a href="http://www.markhneedham.com/blog/2009/02/14/coding-assertions-in-constructors/">in constructors</a> and I had an interesting discussion with some colleagues recently around doing this type of defensive programming when the object in question has its dependencies injected by a container.</p>
<p>Quite often we would see code similar to this in a controller:</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeController 
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> SomeController<span style="color: #009900;">&#40;</span>Dependency1 valueOne, Dependency2 valueTwo<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		AssertThat.<span style="color: #006633;">isNotNull</span><span style="color: #009900;">&#40;</span>valueOne<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		AssertThat.<span style="color: #006633;">isNotNull</span><span style="color: #009900;">&#40;</span>valueTwo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// and so on</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Where 'SomeController' would have 'Dependency1&#8242; and 'Dependency2&#8242; set up in a Spring configuration file in this example.</p>
<p>I can't really see much benefit in this type of pre condition checking although <a href="http://twitter.com/raphscallion">Raph</a> pointed out that if we got the configuration for this bean wrong then having these assertions would allow us to get quicker feedback.</p>
<p>While that is true it seems to me that we would maybe achieve quicker feedback by a few seconds in return for the clutter that we end up creating in our code. We have to read those assertions every time we come to this class. </p>
<p>I would prefer to have some <a href="http://www.markhneedham.com/blog/2009/09/18/tdd-testing-with-generic-abstract-classes/">specific tests for the dependency injection container</a> if we're interested in getting quick feedback in that area. </p>
<p>That seems to be a cleaner solution than having these types of checks in production code or am I missing something?</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/X3vSpJbEuok" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/10/31/coding-invariant-checking-on-dependency-injected-components/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/10/31/coding-invariant-checking-on-dependency-injected-components/</feedburner:origLink></item>
	</channel>
</rss>
