<?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>Sun, 08 Nov 2009 20:00:08 +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: 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>5</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>2</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>
		<item>
		<title>Coding: Consistency when invariant checking</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/AUuR3WhWlE4/</link>
		<comments>http://www.markhneedham.com/blog/2009/10/29/coding-consistency-when-invariant-checking/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 13:06:35 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1771</guid>
		<description><![CDATA[I wrote a while ago about reading the ASP.NET MVC source code and noticing that it makes use of code inside its constructors to ensure that null values can't be passed in and while I'm still not convinced this is the way to go I think if we do take this approach then we need [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a while ago about <a href="http://www.markhneedham.com/blog/2009/02/14/coding-assertions-in-constructors/">reading the ASP.NET MVC source code and noticing that it makes use of code inside its constructors to ensure that null values can't be passed in</a> and while I'm still not convinced this is the way to go I think if we do take this approach then we need to ensure we do so consistently.</p>
<p>Something which happens quite often is that you'll come across code which makes use of defensive programming in one of its constructors like so:</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> SomeObject 
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> SomeObject<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>This is fine in itself but you'll often find another constructor defined close by which looks like this:</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> SomeObject 
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> SomeObject<span style="color: #009900;">&#40;</span>Dependency1 valueOne, Dependency2 valueTwo<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">valueOne</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">valueTwo</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Depending which constructor you use the outcome is quite different and you probably wouldn't expect the default constructor to allow nulls seeing as the other one prevents you from doing this.</p>
<p>This inconsistency can happen for a variety of reasons.</p>
<h4>Test only constructor</h4>
<p>It might be a <a href="http://www.markhneedham.com/blog/2009/09/12/tdd-test-only-constructors/">test only constructor</a> which is not intended to be called in production case. It's much easier to get an object under test if we don't have to supply any parameters. </p>
<p>In that case though we're testing the object constructed in a much different state than it will be when we really use it so the value of this type of testing is questionable.</p>
<p>It's probably best to look why we need that test only constructor and see if we can change the code so that we can avoid that problem.</p>
<h4>An implicit domain concept</h4>
<p>Another reason is that the original pre condition of the object might have changed and now it isn't always the case that we need to provide 'Dependency1&#8242; and 'Dependency2&#8242;.</p>
<p>In this case I think it would make more sense to make that more explicit, perhaps by making use of the <a href="http://www.markhneedham.com/blog/2008/08/16/null-handling-strategies/">null object pattern</a> or by finding another abstraction which doesn't require nulls to be passed around. </p>
<p>Either way we should probably look to ensure that both constructors either ignore nulls or both stop them from being set.</p>
<h4>Didn't realise pre conditions were being checked</h4>
<p>Although this one might seem a bit far fetched it's pretty easy for this to be the case if there are different people working on the code base.</p>
<p>Someone might glance at the class, see that it doesn't quite allow them to construct the object the way they want and create another constructor without realising the way the original one was working.</p>
<p>In this case talking more about the way that we're solving this type of problem across the code base should be enough to ensure that we get some kind of consistency.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/AUuR3WhWlE4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/10/29/coding-consistency-when-invariant-checking/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/10/29/coding-consistency-when-invariant-checking/</feedburner:origLink></item>
		<item>
		<title>Coding: Connascence – Some examples</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/lxschKolO90/</link>
		<comments>http://www.markhneedham.com/blog/2009/10/28/coding-connascence-some-examples/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 12:43:01 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1768</guid>
		<description><![CDATA[I've been reading Meilir Page Jones' 'Fundamentals of Object Oriented Design in UML' recently and one of the chapters that I found the most interesting is the one where he talks about 'connascence'.
Connascence describes the relation between two different bits of code and two bits of code are said to be connascent if a change [...]]]></description>
			<content:encoded><![CDATA[<p>I've been reading Meilir Page Jones' '<a href="http://www.amazon.co.uk/Fundamentals-Object-oriented-Design-Object-Technology/dp/020169946X/ref=sr_1_3?ie=UTF8&#038;s=books&#038;qid=1256562881&#038;sr=8-3">Fundamentals of Object Oriented Design in UML</a>' recently and one of the chapters that I found the most interesting is the one where he talks about 'connascence'.</p>
<p>Connascence describes the relation between two different bits of code and two bits of code are said to be connascent if <strong>a change to one bit of code would require a change to the other bit of the code </strong> or if <strong>some change to another piece of code would require both bits of code to change</strong> for our program to still be correct.</p>
<p>I think this principal is quite similar to the idea of <a href="http://www.markhneedham.com/blog/2009/08/25/coding-coupling-and-expressiveness/">coupling</a> which we seem to use more frequently these days but I found it quite compelling that as I was reading through the different types of connascence that Page-Jones describes I was easily able to identify mistakes I've made and seen made in code.</p>
<p>There are many different types of connascence and Jim Weirich goes through many of them in his presentation from <a href="http://mwrc2009.confreaks.com/">MountainWest RubyConf 2009</a>  titled '<a href="http://mwrc2009.confreaks.com/14-mar-2009-18-10-the-building-blocks-of-modularity-jim-weirich.html">The building blocks of modularity</a>'. </p>
<p>I'll just cover a couple of the ones that seem to cause the most pain from my experience.</p>
<h3>Connascence of execution</h3>
<p>This describes the situation where <strong>two different lines of code have to be executed in a certain order for the program to be executed correctly</strong>.</p>
<p>A typical example of this type of connascence occurs when we <a href="http://www.markhneedham.com/blog/2009/09/16/coding-watch-out-for-mutable-code/">make use of setter methods to construct objects</a>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td 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 someObject <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SomeObject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	someObject.<span style="color: #0000FF;">Property1</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Property1<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;">// some where else far far away</span>
&nbsp;
	SomeOtherMethod<span style="color: #000000;">&#40;</span>someObject<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> SomeOtherMethod<span style="color: #000000;">&#40;</span>SomeObject someObject<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	someObject.<span style="color: #0000FF;">Property1</span>.<span style="color: #0000FF;">AnotherProperty</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> AnotherProperty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>In this example we need line 2 to be executed before line 13 otherwise we'll get a null pointer exception. These two lines therefore have connascence of execution.</p>
<p>Quite often line 13 of this example would be hidden away in a chain of method calls and it wouldn't be completely obvious that it relies on a line further up being executed first.</p>
<p>We eventually end up making what we think is an innocuous reordering of the method calls and suddenly our code doesn't work any more.</p>
<p>In this case to reduce this type of connascence we might look at using a constructor to create our objects, use one of the builder/factory patterns or at least try and capture related code in the same method so that the potential for confusion is reduced.</p>
<h3>Connascence of algorithm</h3>
<p>This typically <strong>describes the situation where we are making use of a data structure in a specific way such that all the pieces of code which interact with that data structure need to know exactly how that data structure works in order to make use of it</strong>. </p>
<p>This problem seems to happen particularly when we're <a href="http://www.markhneedham.com/blog/2009/10/23/coding-the-primitive-obsession/">over using lists when perhaps another level of abstraction is required</a>.</p>
<p>One example of this might be where a piece of code assumes that another piece of code inserts a certain value into a list.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> AClassWhichPutsASpecificItemInAList
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> List<span style="color: #008000;">&lt;</span>SomeObject<span style="color: #008000;">&gt;</span> CreateTheEvil<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		var myEvilList <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>SomeObject<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		myEvilList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> SomeObject<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;SomeSpecialName&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008080; font-style: italic;">// and so on</span>
&nbsp;
		<span style="color: #0600FF;">return</span> myEvilList<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> SomeOtherClassFarFarAway
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SomeMethod<span style="color: #000000;">&#40;</span>List<span style="color: #008000;">&lt;</span>SomeObject<span style="color: #008000;">&gt;</span> someObjects<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		var speciallyNamedObject <span style="color: #008000;">=</span> someObjects.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>s.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;SomeSpecialName&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">First</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>Despite the fact that these two classes never refer to each other they have connascence of algorithm because if 'AClassWhichPutsASpecificItemInAList' decides not to put that value into the list then 'SomeOtherClassFarFarAway' may stop working so we will need to change that as well.</p>
<p>This type of connascence is much more implicit than some of the other types and it may not be immediately obvious that two pieces of code are related.</p>
<p>We could get around this problem by encapsulating this type of logic in its own type so that at least we only have to deal with it once. </p>
<p>The goal is to try and <a href="http://www.markhneedham.com/blog/2009/04/23/ddd-making-implicit-concepts-explicit/">make an implicit piece of code more explicit</a>.</p>
<h3>Connascence of convention</h3>
<p>This describes the situation where there is <strong>an implicit convention for the meaning behind the value of a certain piece of code such that every other bit of code that touches it needs to know this convention</strong>. This is quite similar to connascence of algorithm.</p>
<p>An example that I came across recently was around how we passed around the value of an option selected on a form by the user.</p>
<p>The user could either select 'Yes', 'No' or they could choose not to answer the question. If they entered 'Yes' then they would need to answer another question immediately after this.</p>
<p>Later on we needed to pass this data to the service layer. A value of 'No' if they selected that, the answer to the next question if they answered 'Yes' and 'None' if they didn't enter anything. </p>
<p>We ended up having code similar to this in the service call and then again in the binder:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TheServiceThatUsesThatValue
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> CallTheService<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> theAnswer<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		var theRequest <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ServiceRequest<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>theAnswer <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;None&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			theRequest.<span style="color: #0000FF;">TheAnswer</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;None&quot;</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>theAnswer <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;No&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			theRequest.<span style="color: #0000FF;">TheAnswer</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;No&quot;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">else</span>
		<span style="color: #000000;">&#123;</span>
			theRequest.<span style="color: #0000FF;">TheAnswer</span> <span style="color: #008000;">=</span> LookUpTheCodeFor<span style="color: #000000;">&#40;</span>theAnswer<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>	
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We eventually gave up on the idea of passing 'None' around because that was the bit causing the most confusion. Instead we stored the answer in a nullable data type and then did the conversion to 'None' when necessary in the Service class.</p>
<h3>In summary</h3>
<p>There are just a couple of the examples that Page-Jones outlines but the general idea is that we want to try and minimise the connascence in our system by creating well encapsulated objects.</p>
<p>Within those objects it makes sense to keep connascence high and in fact if it's not then it might suggest that we have another object waiting to get out. </p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/lxschKolO90" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/10/28/coding-connascence-some-examples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/10/28/coding-connascence-some-examples/</feedburner:origLink></item>
	</channel>
</rss>
