<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Managed Chaos &#187; Programming</title>
	<atom:link href="http://blogs.agilefaqs.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.agilefaqs.com</link>
	<description>Naresh Jain&#039;s Random Thoughts on Software Development and Adventure Sports</description>
	<lastBuildDate>Tue, 07 Feb 2012 10:21:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Deliberate Practice: The Expressway to becoming an Expert</title>
		<link>http://blogs.agilefaqs.com/2012/01/02/the-expressway-to-becoming-an-expert-deliberated-practice/</link>
		<comments>http://blogs.agilefaqs.com/2012/01/02/the-expressway-to-becoming-an-expert-deliberated-practice/#comments</comments>
		<pubDate>Mon, 02 Jan 2012 02:46:31 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Coaching]]></category>
		<category><![CDATA[Organizational]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Self Help]]></category>
		<category><![CDATA[Apprenticeship]]></category>
		<category><![CDATA[Deliberate Practice]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=1903</guid>
		<description><![CDATA[&#8220;Its God&#8217;s gift&#8221; or &#8220;S/he was born talented&#8221; or &#8220;S/He just lucky&#8221; is a common myth that undermines the relentless hard-work experts put to attain mastery in their respect work. Benjamin Bloom, a pioneer who broke this myth found out that: &#8220;All the superb performers, he investigated, had practiced intensively, had studied with devoted teachers, [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;Its God&#8217;s gift&#8221; or &#8220;S/he was born talented&#8221; or &#8220;S/He just lucky&#8221; is a <span style="text-decoration: underline;">common myth</span> that <em>undermines</em> the <strong>relentless hard-work</strong> experts put to attain mastery in their respect work.</p>
<p>Benjamin Bloom, a pioneer who broke this myth found out that:</p>
<blockquote><p>&#8220;All the superb performers, he investigated, had practiced intensively, had studied with devoted teachers, and had been supported enthusiastically by their families throughout their developing years.&#8221;</p></blockquote>
<p>Later research, building on Bloom’s study revealed that the <strong><span style="text-decoration: underline;">amount and quality of practice were key factors</span></strong> in the level of expertise people achieved.</p>
<p>Consistently and overwhelmingly, the evidence showed that:</p>
<blockquote><p>&#8220;Experts are always made, not born.&#8221;</p></blockquote>
<p>The journey to truly superior performance is neither for the faint hearted nor for the impatient. The development of genuine expertise requires struggle, sacrifice, and honest, often painful self-assessment. <strong>There are no shortcuts.</strong> It will take many years if not decades to achieve expertise, and you will need to invest that time wisely, by engaging in “deliberate” practice; practice that focuses on tasks beyond your current level of competence and comfort. You will need a well-informed coach not only to guide you through deliberate practice but also to help you learn how to coach yourself.</p>
<p>One study showed that psychotherapists with advanced degrees and decades of experience aren’t reliably more successful in their treatment of randomly assigned patients than novice therapists with just three months of training are. There are even examples of expertise seeming to decline with experience. The longer physicians have been out of training, for example, the less able they are to identify unusual diseases of the lungs or heart. Because they encounter these illnesses so rarely, doctors quickly forget their characteristic features and have difficulty diagnosing them.</p>
<p><span style="text-decoration: underline;"><strong>Practice Deliberately</strong></span>: Not all practice makes you perfect. You need a particular kind of practice &#8211; &#8220;deliberate practice&#8221; &#8211; to develop expertise. When most people practice, they focus on the things they already know how to do. Deliberate practice is different. It entails considerable, specific, and sustained efforts to do something you can’t do well &#8211; or even at all.</p>
<p>Let’s imagine you are learning to play golf for the first time. In the early phases, you try to understand the basic strokes and focus on avoiding gross mistakes (like driving the ball into another player). You practice on the putting green, hit balls at a driving range, and play rounds with others who are most likely novices like you. In a surprisingly short time (perhaps 50 hours), you will develop better control and your game will improve. From then on, you will work on your skills by driving and putting more balls and engaging in more games, until your strokes become automatic: You’ll think less about each shot and play more from intuition. Your golf game now is a social outing, in which you occasionally concentrate on your shot. From this point on, additional time on the course will not substantially improve your performance, which may remain at the same level for decades.</p>
<blockquote><p>Why does this happen?</p></blockquote>
<p>You don’t improve because when you are playing a game, you get only a single chance to make a shot from any given location. You don’t get to figure out how you can correct mistakes. If you were allowed to take five to ten shots from the exact same location on the course, you would get more feedback on your technique and start to adjust your playing style to improve your control. In fact, professionals often take multiple shots from the same location when they train and when they check out a course before a tournament.</p>
<p><span style="text-decoration: underline;">Computer gaming</span> is an excellent example where I&#8217;ve seen people practice deliberately to get better. They focus on what they can do well, but they also focus on what they can&#8217;t do well. Most importantly, when practicing, the gamer is not just mindlessly playing. It’s a very thoughtful, deep, dedicated practice session.</p>
<p><span style="text-decoration: underline;">War games</span> serve a similar training function at military academies. So do <span style="text-decoration: underline;">flight simulators</span> for pilots. Unfortunately in software development, very few people practice deliberately.</p>
<p>Genuine experts not only practice deliberately but also think deliberately. The golfer Ben Hogan once explained, “While I am practicing I am also trying to develop my powers of concentration. I never just walk up and hit the ball.”</p>
<p>Deliberate practice involves <strong><span style="text-decoration: underline;">two kinds of learning</span></strong>:</p>
<ol>
<li>Improving the skills you already have</li>
<li>Extending the reach and range of your skills.</li>
</ol>
<blockquote><p>“Practice puts brains in your muscles” &#8211; Golf champion Sam Snead</p></blockquote>
<p>The enormous concentration required undertaking these twin tasks limits the amount of time you can spend doing them.</p>
<p>How long should you do deliberate practice each day?</p>
<blockquote><p>&#8220;It really doesn’t matter how long. If you practice with your fingers, no amount is enough. If you practice with your head, two hours is plenty.&#8221;</p></blockquote>
<p><em><span style="text-decoration: underline;"><strong>It’s very easy to neglect deliberate practice</strong></span></em>. Experts who reach a high level of performance often find themselves responding automatically to specific situations and may come to rely exclusively on their intuition. This leads to difficulties when they deal with atypical or rare cases, because they’ve lost the ability to analyze a situation and work through the right response. Experts may not recognize this creeping intuition bias, of course, because there is no penalty until they encounter a situation in which a habitual response fails and maybe even causes damage.</p>
<p>Many research show the <em><span style="text-decoration: underline;">importance of a coach/mentor</span></em> in deliberate practice. Some strongly favor an apprenticeship model. However one needs to be aware of the limitation of just following a coach or working alongside an &#8220;expert.&#8221;</p>
<p>Statistics show that radiologists correctly diagnose breast cancer from X-rays about 70% of the time. Typically, young radiologists learn the skill of interpreting X-rays by working alongside an “expert.” So it’s hardly surprising that the success rate has stuck at 70% for a long time. Imagine how much better radiology might get if radiologists practiced instead by making diagnostic judgments using X-rays in a library of old verified cases, where they could immediately determine their accuracy.</p>
<p>All an all, &#8220;<span style="text-decoration: underline;"><em>Living in a cave does not make you a geologist</em></span>&#8221; .i.e. without deliberate practice you go no where.</p>
<p>Original Article: <a href="http://hbr.org/2007/07/the-making-of-an-expert/ar/1" target="_blank">The Making of an Expert</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2012/01/02/the-expressway-to-becoming-an-expert-deliberated-practice/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Its all because you did not write a Unit Test&#8230;</title>
		<link>http://blogs.agilefaqs.com/2011/11/01/its-all-because-you-did-not-write-a-unit-test/</link>
		<comments>http://blogs.agilefaqs.com/2011/11/01/its-all-because-you-did-not-write-a-unit-test/#comments</comments>
		<pubDate>Tue, 01 Nov 2011 09:48:22 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[less is more]]></category>
		<category><![CDATA[Map]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[unit tests]]></category>
		<category><![CDATA[useless code]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=1845</guid>
		<description><![CDATA[Recently we realized that our server logs were showing &#8216;null&#8217; for all HTTP request parameters values. SEVERE: Attempting to get user with null userName parameters=&#91;version:null&#93;&#91;sessionId:null&#93;&#91;userAgent:null&#93;&#91;requestedUrl:null&#93;&#91;queryString:null&#93; &#91;action:null&#93;&#91;session:null&#93;&#91;userIPAddress:null&#93;&#91;year:null&#93;&#91;path:null&#93;&#91;user:null&#93; On digging around a bit, we found the following buggy code in our custom Map class, which was used to hold the request parameters: @Override public String toString&#40;&#41; &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>Recently we realized that our server logs were showing &#8216;null&#8217; for all HTTP request parameters values.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">SEVERE: Attempting to get user with null userName
<span style="color: #007800;">parameters</span>=<span style="color: #7a0874; font-weight: bold;">&#91;</span>version:null<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>sessionId:null<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>userAgent:null<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>requestedUrl:null<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>queryString:null<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>action:null<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>session:null<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>userIPAddress:null<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>year:null<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>path:null<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>user:null<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>On digging around a bit, we found the following buggy code in our custom Map class, which was used to hold the request parameters:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Override
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    StringBuilder parameters <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Map</span>.<span style="color: #006633;">Entry</span><span style="color: #339933;">&lt;</span>String, Object<span style="color: #339933;">&gt;</span> entry <span style="color: #339933;">:</span> entrySet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        parameters.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;[&quot;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>entry.<span style="color: #006633;">getKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>get<span style="color: #009900;">&#40;</span>entry.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> parameters.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When we found this, many team members&#8217; reaction was:</p>
<blockquote><p>If the author had written unit tests, this bug would have been caught immediately.</p></blockquote>
<p>Others responded saying: </p>
<blockquote><p>But we usually don&#8217;t write tests for toString(), Getters and Setters. We <a href="http://blogs.agilefaqs.com/2011/11/01/unit-testing-dilemma-should-i-invest-or-not/" title="Unit Testing Dilemma: Should I Invest or Not?">pragmatically choose when to invest in unit tests</a>.</p></blockquote>
<p>As all of this was taking place, I was wondering, why in the first place, the author even wrote this code? As you can see from the following snippet, Maps already know how to print themselves.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> mapKnowsHowToPrintItself<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">Map</span> hashMap <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    hashMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Key1&quot;</span>, <span style="color: #0000ff;">&quot;Value1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    hashMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Key2&quot;</span>, <span style="color: #0000ff;">&quot;Value2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>hashMap<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Output<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>Key2<span style="color: #339933;">=</span>Value2, Key1<span style="color: #339933;">=</span>Value1<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Its easy to fall into the trap of first writing useless code and then defending it by writing more useless tests for it. </p>
<p>I&#8217;m a lazy developer and I always strive real hard to write as little code as possible. IMHO real power and simplicity comes from less code, not more.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2011/11/01/its-all-because-you-did-not-write-a-unit-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Testing Dilemma: Should I Invest or Not?</title>
		<link>http://blogs.agilefaqs.com/2011/11/01/unit-testing-dilemma-should-i-invest-or-not/</link>
		<comments>http://blogs.agilefaqs.com/2011/11/01/unit-testing-dilemma-should-i-invest-or-not/#comments</comments>
		<pubDate>Tue, 01 Nov 2011 09:23:41 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[unit tests]]></category>
		<category><![CDATA[XP]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=1837</guid>
		<description><![CDATA[Every single line of code must be unit tested! This sound advice rather seems quite extreme to me. IMHO a skilled programmer pragmatically decides when to invest in unit testing. After practicing (automated) unit testing for over a decade, I&#8217;m a strong believer and proponent of automated unit testing. My take on why developers should care [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>Every single line of code must be unit tested!</p></blockquote>
<p>This sound advice rather seems quite extreme to me. IMHO a skilled programmer pragmatically decides when to invest in unit testing.</p>
<p>After practicing (automated) unit testing for over a decade, I&#8217;m a strong believer and proponent of automated unit testing. My take on <a title="Importance of Unit Testing and Test Driven Development (TDD)" href="http://blogs.agilefaqs.com/2011/11/01/importance-of-unit-testing-and-test-driven-development-tdd/">why developers should care about Unit Testing and TDD</a>.</p>
<p>However over the years I&#8217;ve realized that automated unit tests do have four, very important, costs associated with them:</p>
<ul>
<li>Cost of <strong>writing</strong> the unit tests in the first place</li>
<li>Cost of <strong>running</strong> the unit tests regularly to get feedback</li>
<li>Cost of <strong>maintaining</strong> and updating the unit tests as and when required</li>
<li>Cost of <strong>understanding</strong> other&#8217;s unit tests</li>
</ul>
<div>One also starts to recognize some other subtle costs associated with unit testing:</div>
<div>
<ul>
<li><span style="text-decoration: underline;"><strong>Illusion of safety</strong></span>: While unit tests gives you a great safety net, at times, it can also create an illusion of safety leading to developers too heavily relying on just unit tests (possibly doing more harm than good.)</li>
<li><span style="text-decoration: underline;"><strong>Opportunity cost</strong></span>: If I did not invest in this test, what else could I have done in that time? Flip side of this argument is the opportunity cost of repetitive manually testing or even worse not testing at all.</li>
<li><strong><span style="text-decoration: underline;">Getting in the way</span></strong>: While unit tests help you drive your design, at times, they do get in the way of refactoring. Many times, I&#8217;ve refrained from refactoring the code because I get intimidated by the sheer effort of refactor/rewrite a large number of my tests as well. (I&#8217;ve learned many patterns to reduce this pain over the years, but the pain still exists.)</li>
<li><strong><span style="text-decoration: underline;">Obscures a simpler design</span></strong>: Many times, I find myself so engrossed in my tests and the design it leads to, that I become ignorant to a better, more simpler design. Also sometimes half-way through, even if I realize that there might be an alternative design, because I&#8217;ve already invested in a solution (plus all its tests), its harder to throw away the code. In retrospect this always seems like a bad choice.</li>
</ul>
<div>If we consider all these factors, would you agree with me that:</div>
<blockquote>
<div>Automated unit testing is extremely important, but each developer has to make a conscious, pragmatic decision when to invest in unit testing.</div>
</blockquote>
<div><em>Its easy to say always write unit tests, but it takes years of first-hand experience to judge where to draw the line.</em></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2011/11/01/unit-testing-dilemma-should-i-invest-or-not/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Importance of Unit Testing and Test Driven Development (TDD)</title>
		<link>http://blogs.agilefaqs.com/2011/11/01/importance-of-unit-testing-and-test-driven-development-tdd/</link>
		<comments>http://blogs.agilefaqs.com/2011/11/01/importance-of-unit-testing-and-test-driven-development-tdd/#comments</comments>
		<pubDate>Tue, 01 Nov 2011 09:06:15 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=1838</guid>
		<description><![CDATA[Why should developers care of automated unit tests? Keeps you out of the (time hungry) debugger! Reduces bugs in new features and in existing features Reduces the cost of change Improves design Encourages refactoring Builds a safety net to defend against other programmers Is fun Forces you to slow down and think Speeds up development [...]]]></description>
			<content:encoded><![CDATA[<p>Why should developers care of automated unit tests?</p>
<ul>
<li>Keeps you out of the (time hungry) <strong>debugger</strong>!</li>
<li>Reduces <strong>bugs</strong> in new features and in existing features</li>
<li>Reduces the <strong>cost of change</strong></li>
<li>Improves <strong>design</strong></li>
<li>Encourages <strong>refactoring</strong></li>
<li>Builds a <strong>safety net</strong> to defend against other programmers</li>
<li>Is <strong>fun</strong></li>
<li>Forces you to <strong>slow down and think</strong></li>
<li>Speeds up development by <strong>eliminating waste</strong></li>
<li>Reduces <strong>fear</strong></li>
</ul>
<p>And how TDD takes it to the next level?</p>
<ul>
<li>Improves <strong>productivity</strong> by</li>
<ul>
<li>minimizing time spent <strong>debugging</strong></li>
<li>reduces the need for <strong>manual (monkey) checking</strong> by developers and tester</li>
<li>helping developers to maintain <strong>focus</strong></li>
<li>reduce <strong>wastage</strong>: hand overs</li>
</ul>
<li>Improves <strong>communication</strong></li>
<li>Creating living, up-to-date <strong>specification</strong></li>
<li>Communicate <strong>design decisions</strong></li>
<li><strong>Learning</strong>: listen to your code</li>
<li><strong>Baby steps</strong>: slow down and think</li>
<li>Improves <strong>confidence</strong></li>
<li><strong>Testable</strong> code by <strong>design</strong> + safety net</li>
<li><strong>Loosely-coupled</strong> design</li>
<li><strong>Refactoring</strong></li>
</ul>
<div style="width:510px" id="__ss_9973009">
<object id="__sse9973009" width="510" height="426"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=whytdd-111101035104-phpapp02&#038;stripped_title=why-test-driven-development&#038;userName=nashjain" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse9973009" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=whytdd-111101035104-phpapp02&#038;stripped_title=why-test-driven-development&#038;userName=nashjain" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="510" height="426"></embed></object>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2011/11/01/importance-of-unit-testing-and-test-driven-development-tdd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is Code Coverage, Cyclomatic Complexity or Defect Density a good Measure of Quality?</title>
		<link>http://blogs.agilefaqs.com/2011/10/09/is-code-coverage-cyclomatic-complexity-or-defect-density-a-good-measure-of-quality/</link>
		<comments>http://blogs.agilefaqs.com/2011/10/09/is-code-coverage-cyclomatic-complexity-or-defect-density-a-good-measure-of-quality/#comments</comments>
		<pubDate>Sun, 09 Oct 2011 01:25:03 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Metrics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Code Coverage]]></category>
		<category><![CDATA[cyclomatic complexity]]></category>
		<category><![CDATA[defect density]]></category>
		<category><![CDATA[External Quality]]></category>
		<category><![CDATA[Extrinsic Quality]]></category>
		<category><![CDATA[Fundamental Attribution Error]]></category>
		<category><![CDATA[Internal Quality]]></category>
		<category><![CDATA[Intrinsic Quality]]></category>
		<category><![CDATA[Quality]]></category>
		<category><![CDATA[Software Quality]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=1825</guid>
		<description><![CDATA[In Software, Quality is one of those badly abused term, which is getting harder and harder to define what it really means. I think we have a sense of quality. When we see something in a specific context, we can say its high quality or low quality, but its hard to define (and hence measure) [...]]]></description>
			<content:encoded><![CDATA[<p>In Software, <a href="http://en.wikipedia.org/wiki/Software_quality" title="Software Quality" target="_blank">Quality</a> is one of those badly abused term, which is getting harder and harder to define what it really means. I think we have a sense of quality. When we see something in a specific context, we can say its high quality or low quality, but its hard to define (and hence measure) what absolute quality really is.</p>
<blockquote><p>You can measure somethings about quality, but don&#8217;t fool yourself to believe that IS quality. </p>
<p>Quality is subjective, relative and contextual.</p></blockquote>
<p>Some might say things like code coverage, cyclomatic complexity and defect density is a good measure of quality. I would argue that those are attributes/aspects of quality, but not quality itself (symptoms not the disease itself.) Its a classic case of <a href="http://blogs.agilefaqs.com/2011/01/21/fundamental-attribution-error-managementcoachesconsultants-watch-out/" title="Fundamental Attribution Error: Management/Coaches/Consultants Watch-out">Fundamental Attribution Error.</a> (If you go to France and see the first 50 Frenchmen wear glasses, you cannot conclude all Frenchmen wear glasses. Nor can you conclude that, if I wear glasses I&#8217;ll also be French.)</p>
<p>BTW people already differentiate between <a href="http://c2.com/cgi/wiki?InternalAndExternalQuality" title="Internal And External Quality" target="_blank">Internal/Intrinsic Quality and External/Extrinsic Quality</a>. This is not enough to complicate things, evangelists would like to further slice and dice quality along different parameters (structural, functional, UX, etc.)</p>
<p>Some anecdotes:</p>
<ul>
<li><a href="http://c2.com/cgi/wiki?WhatIsQuality" title="What is Quality" target="_blank">What is Quality</a></li>
<li><a href="http://blogs.agilefaqs.com/2011/02/01/dont-be-seduced-by-code-coverage-numbers/" title="Don’t be Seduced by Code Coverage Numbers">Don’t be Seduced by Code Coverage Numbers</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2011/10/09/is-code-coverage-cyclomatic-complexity-or-defect-density-a-good-measure-of-quality/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Code Smell of the Week: Obsessed with Out Parameters</title>
		<link>http://blogs.agilefaqs.com/2011/08/06/code-smell-of-week-obsessed-with-out-parameters/</link>
		<comments>http://blogs.agilefaqs.com/2011/08/06/code-smell-of-week-obsessed-with-out-parameters/#comments</comments>
		<pubDate>Sat, 06 Aug 2011 03:47:36 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Code Smells]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code smells]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[Encapsulation]]></category>
		<category><![CDATA[Error Code]]></category>
		<category><![CDATA[MSTest]]></category>
		<category><![CDATA[out params]]></category>
		<category><![CDATA[Refactoring]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=1785</guid>
		<description><![CDATA[Consider the following code to retrieve a user&#8217;s profile: public bool GetUserProfile&#40;string userName, out Profile userProfile, out string msg&#41; &#123; msg = string.Empty; userProfile = null; if &#40;some_validations_here&#41;&#41; &#123; msg = string.Format&#40;&#34;Insufficient data to get Profile for username: {0}.&#34;, userName&#41;; return false; &#125; &#160; IList&#60;User&#62; users = // retrieve from database &#160; if &#40;users.Count&#40;&#41; &#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Consider the following code to retrieve a user&#8217;s profile:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> GetUserProfile<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> userName, <span style="color: #0600FF; font-weight: bold;">out</span> Profile userProfile, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">string</span> msg<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    msg <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
    userProfile <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>some_validations_here<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        msg <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Insufficient data to get Profile for username: {0}.&quot;</span>, userName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span> 
&nbsp;
    IList<span style="color: #008000;">&lt;</span>User<span style="color: #008000;">&gt;</span> users <span style="color: #008000;">=</span> <span style="color: #008080; font-style: italic;">// retrieve from database </span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>users<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        msg <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Username {0} has {1} Profiles&quot;</span>, userName, users<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>users<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
       userProfile <span style="color: #008000;">=</span> Profiles<span style="color: #008000;">.</span><span style="color: #0000FF;">Guest</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
    <span style="color: #008000;">&#123;</span>
        userProfile <span style="color: #008000;">=</span> users<span style="color: #008000;">.</span><span style="color: #0000FF;">Get</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Profile</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Notice the <strong>bool return value</strong> and the use of <strong>out</strong> parameters. This code is heavily influenced by COM &#038; C Programming. We don&#8217;t operate under the same constraints these days. </p>
<p>If we were to write a test for this method, what would it look like?</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>TestClass<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ProfileControllerTest
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> msg<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Profile userProfile<span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">//create a fakeDB</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> ProfileController controller <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ProfileController<span style="color: #008000;">&#40;</span>fakeDB<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> UserName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Naresh.Jain&quot;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ValidUserNameIsRequiredToGetProfile<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>      
        var emptyUserName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsFalse</span><span style="color: #008000;">&#40;</span>controller<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUserProfile</span><span style="color: #008000;">&#40;</span>emptyUserName, <span style="color: #0600FF; font-weight: bold;">out</span> userProfile, <span style="color: #0600FF; font-weight: bold;">out</span> msg<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsNull</span><span style="color: #008000;">&#40;</span>userProfile<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Insufficient data to get Profile for username: &quot;</span> <span style="color: #008000;">+</span> UserName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;.&quot;</span>, msg<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> UsersCannotHaveMultipleProfiles<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>     
        <span style="color: #008080; font-style: italic;">//fake DB returns 2 records</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsFalse</span><span style="color: #008000;">&#40;</span>controller<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUserProfile</span><span style="color: #008000;">&#40;</span>UserName, <span style="color: #0600FF; font-weight: bold;">out</span> userProfile, <span style="color: #0600FF; font-weight: bold;">out</span> msg<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsNull</span><span style="color: #008000;">&#40;</span>userProfile<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Username &quot;</span><span style="color: #008000;">+</span> UserName <span style="color: #008000;">+</span><span style="color: #666666;">&quot; has 2 Profiles.&quot;</span>, msg<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ProfileDefaultedToGuestWhenNoRecordsAreFound<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>       
        <span style="color: #008080; font-style: italic;">//fake DB does not return any records</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>controller<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUserProfile</span><span style="color: #008000;">&#40;</span>UserName, <span style="color: #0600FF; font-weight: bold;">out</span> userProfile, <span style="color: #0600FF; font-weight: bold;">out</span> msg<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span>Profiles<span style="color: #008000;">.</span><span style="color: #0000FF;">Guest</span>, userProfile<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsNull</span><span style="color: #008000;">&#40;</span>msg<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> MatchingProfileIsRetrievedForValidUserName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>         
        <span style="color: #008080; font-style: italic;">//fake DB returns valid tester</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>controller<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUserProfile</span><span style="color: #008000;">&#40;</span>UserName, <span style="color: #0600FF; font-weight: bold;">out</span> userProfile, <span style="color: #0600FF; font-weight: bold;">out</span> msg<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span>Profiles<span style="color: #008000;">.</span><span style="color: #0000FF;">Tester</span>, userProfile<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsNull</span><span style="color: #008000;">&#40;</span>msg<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This code really stinks.  </p>
<p>What problems do you see with this approach?</p>
<ul>
<li>Code like this lacks encapsulation. All the out parameters could be encapsulated into an object.</li>
<li>Encourages duplication in both client code and inside this method.</li>
<li>The caller of this method needs to check the return value first. If its false then they need to get the msg and do the needful. Its very easy to ignore the failure conditions. (In fact with this very code we saw that happen in 4 out of 6 places.)</li>
<li>Tests have to validate multiple things to ensure the code is functions correctly.</li>
<li>Overall more difficult to understand</li>
</ul>
<p>We can refactor this code as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> Profile GetUserProfile<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> userName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>some_validations_here<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>   
        <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Insufficient data to get Profile for username: {0}.&quot;</span>, userName<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
&nbsp;
    IList<span style="color: #008000;">&lt;</span>User<span style="color: #008000;">&gt;</span> users <span style="color: #008000;">=</span> <span style="color: #008080; font-style: italic;">// retrieve from database</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>users<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>  
        <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span>Username <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#125;</span> has <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#125;</span> Profiles<span style="color: #666666;">&quot;, userName, users.Count()));
&nbsp;
    if (users.Count() == 0) return Profiles.Guest;
&nbsp;
    return users.Get(0).Profile;
}</span></pre></div></div>

<p>and Test code as:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>TestClass<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ProfileControllerTest
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//create a fakeDB</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> ProfileController controller <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ProfileController<span style="color: #008000;">&#40;</span>fakeDB<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> UserName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Naresh.Jain&quot;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>   
    <span style="color: #008000;">&#91;</span>ExpectedException<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Exception<span style="color: #008000;">&#41;</span>, <span style="color: #666666;">&quot;Insufficient data to get Profile for username: .&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ValidUserNameIsRequiredToGetProfile<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>      
        var emptyUserName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
        controller<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUserProfile</span><span style="color: #008000;">&#40;</span>emptyUserName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>    
    <span style="color: #008000;">&#91;</span>ExpectedException<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Exception<span style="color: #008000;">&#41;</span>, <span style="color: #666666;">&quot;Username &quot;</span><span style="color: #008000;">+</span> UserName <span style="color: #008000;">+</span><span style="color: #666666;">&quot; has 2 Profiles.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> UsersCannotHaveMultipleProfiles<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>     
        <span style="color: #008080; font-style: italic;">//fake DB returns 2 records</span>
        controller<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUserProfile</span><span style="color: #008000;">&#40;</span>UserName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ProfileDefaultedToGuestWhenNoRecordsAreFound<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>       
        <span style="color: #008080; font-style: italic;">//fake DB does not return any records  </span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span>Profiles<span style="color: #008000;">.</span><span style="color: #0000FF;">Guest</span>, controller<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUserProfile</span><span style="color: #008000;">&#40;</span>UserName<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>   
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> MatchingProfileIsRetrievedForValidUserName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>         
        <span style="color: #008080; font-style: italic;">//fake DB returns valid tester</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span>Profiles<span style="color: #008000;">.</span><span style="color: #0000FF;">Tester</span>, controller<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUserProfile</span><span style="color: #008000;">&#40;</span>UserName<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>See how simple the client code (tests are also client code) can be.</p>
<p>My heart sinks when I see the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> GetDataFromConfig<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> i, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> x, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> y, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> z<span style="color: #008000;">&#41;</span><span style="color: #008000;">...</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> AdjustDataBasedOnCorrelation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span> correlation, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> i, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> x, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> y, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> z<span style="color: #008000;">&#41;</span><span style="color: #008000;">...</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> Add<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> factor, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> i, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> x, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> y, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> z<span style="color: #008000;">&#41;</span><span style="color: #008000;">...</span></pre></div></div>

<p>I sincerely hope we can find a home (encapsulation) for all these orphans (i, x, y and z). </p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2011/08/06/code-smell-of-week-obsessed-with-out-parameters/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Duplicate Code and Ceremony in Java</title>
		<link>http://blogs.agilefaqs.com/2011/07/21/duplicate-code-and-ceremony-in-java/</link>
		<comments>http://blogs.agilefaqs.com/2011/07/21/duplicate-code-and-ceremony-in-java/#comments</comments>
		<pubDate>Thu, 21 Jul 2011 11:37:50 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Code Smells]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[code smells]]></category>
		<category><![CDATA[duplicate code]]></category>
		<category><![CDATA[Duplication]]></category>
		<category><![CDATA[Dynamic Languages]]></category>
		<category><![CDATA[Dynamic Method Invocation]]></category>
		<category><![CDATA[Dynamic Typing]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Static Languages]]></category>
		<category><![CDATA[Static Typing]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=1783</guid>
		<description><![CDATA[How would you kill this duplication in a strongly typed, static language like Java? private int calculateAveragePreviousPercentageComplete&#40;&#41; &#123; int result = 0; for &#40;StudentActivityByAlbum activity : activities&#41; result += activity.getPreviousPercentageCompleted&#40;&#41;; return result / activities.size&#40;&#41;; &#125; &#160; private int calculateAverageCurrentPercentageComplete&#40;&#41; &#123; int result = 0; for &#40;StudentActivityByAlbum activity : activities&#41; result += activity.getPercentageCompleted&#40;&#41;; return result / [...]]]></description>
			<content:encoded><![CDATA[<p>How would you kill this duplication in a strongly typed, static language like Java?</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> calculateAveragePreviousPercentageComplete<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">int</span> result <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>StudentActivityByAlbum activity <span style="color: #339933;">:</span> activities<span style="color: #009900;">&#41;</span>
        result <span style="color: #339933;">+=</span> activity.<span style="color: #006633;">getPreviousPercentageCompleted</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;">return</span> result <span style="color: #339933;">/</span> activities.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> calculateAverageCurrentPercentageComplete<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">int</span> result <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>StudentActivityByAlbum activity <span style="color: #339933;">:</span> activities<span style="color: #009900;">&#41;</span>
        result <span style="color: #339933;">+=</span> activity.<span style="color: #006633;">getPercentageCompleted</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;">return</span> result <span style="color: #339933;">/</span> activities.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> calculateAverageProgressPercentage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">int</span> result <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>StudentActivityByAlbum activity <span style="color: #339933;">:</span> activities<span style="color: #009900;">&#41;</span>
        result <span style="color: #339933;">+=</span> activity.<span style="color: #006633;">getProgressPercentage</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;">return</span> result <span style="color: #339933;">/</span> activities.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is my horrible solution:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> calculateAveragePreviousPercentageComplete<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> <span style="color: #000000; font-weight: bold;">new</span> Average<span style="color: #009900;">&#40;</span>activities<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#40;</span>StudentActivityByAlbum activity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> activity.<span style="color: #006633;">getPreviousPercentageCompleted</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: #009900;">&#125;</span>.<span style="color: #006633;">result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> calculateAverageCurrentPercentageComplete<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> <span style="color: #000000; font-weight: bold;">new</span> Average<span style="color: #009900;">&#40;</span>activities<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#40;</span>StudentActivityByAlbum activity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> activity.<span style="color: #006633;">getPercentageCompleted</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: #009900;">&#125;</span>.<span style="color: #006633;">result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> calculateAverageProgressPercentage<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> <span style="color: #000000; font-weight: bold;">new</span> Average<span style="color: #009900;">&#40;</span>activities<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#40;</span>StudentActivityByAlbum activity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> activity.<span style="color: #006633;">getProgressPercentage</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: #009900;">&#125;</span>.<span style="color: #006633;">result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> Average <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> result<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Average<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>StudentActivityByAlbum<span style="color: #339933;">&gt;</span> activities<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">int</span> total <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>StudentActivityByAlbum activity <span style="color: #339933;">:</span> activities<span style="color: #009900;">&#41;</span>
            total <span style="color: #339933;">+=</span> value<span style="color: #009900;">&#40;</span>activity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        result <span style="color: #339933;">=</span> total <span style="color: #339933;">/</span> activities.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#40;</span>StudentActivityByAlbum activity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>if this were Ruby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@activities</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0.0</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>total, activity<span style="color:#006600; font-weight:bold;">|</span> total <span style="color:#006600; font-weight:bold;">+</span> activity.<span style="color:#9900CC;">previous_percentage_completed</span>? <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#0066ff; font-weight:bold;">@activities</span>.<span style="color:#9900CC;">size</span>
<span style="color:#0066ff; font-weight:bold;">@activities</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0.0</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>total, activity<span style="color:#006600; font-weight:bold;">|</span> total <span style="color:#006600; font-weight:bold;">+</span> activity.<span style="color:#9900CC;">percentage_completed</span>? <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#0066ff; font-weight:bold;">@activities</span>.<span style="color:#9900CC;">size</span>
<span style="color:#0066ff; font-weight:bold;">@activities</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0.0</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>total, activity<span style="color:#006600; font-weight:bold;">|</span> total <span style="color:#006600; font-weight:bold;">+</span> activity.<span style="color:#9900CC;">progress_percentage</span>? <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#0066ff; font-weight:bold;">@activities</span>.<span style="color:#9900CC;">size</span></pre></div></div>

<p>or even something more kewler</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">average_of <span style="color:#ff3333; font-weight:bold;">:previous_percentage_completed</span>?
average_of <span style="color:#ff3333; font-weight:bold;">:percentage_completed</span>?
average_of <span style="color:#ff3333; font-weight:bold;">:progress_percentage</span>?
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> average_of<span style="color:#006600; font-weight:bold;">&#40;</span>message<span style="color:#006600; font-weight:bold;">&#41;</span>
	<span style="color:#0066ff; font-weight:bold;">@activities</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0.0</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>total, activity<span style="color:#006600; font-weight:bold;">|</span> total <span style="color:#006600; font-weight:bold;">+</span> activity.<span style="color:#9900CC;">send</span> message <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#0066ff; font-weight:bold;">@activities</span>.<span style="color:#9900CC;">size</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2011/07/21/duplicate-code-and-ceremony-in-java/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Dynamic Typing is NOT Weak Typing</title>
		<link>http://blogs.agilefaqs.com/2011/07/11/dynamic-typing-is-not-weak-typing/</link>
		<comments>http://blogs.agilefaqs.com/2011/07/11/dynamic-typing-is-not-weak-typing/#comments</comments>
		<pubDate>Mon, 11 Jul 2011 08:40:17 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Dynamic Languages]]></category>
		<category><![CDATA[Dynamic Typing]]></category>
		<category><![CDATA[implicit conversions]]></category>
		<category><![CDATA[Static Languages]]></category>
		<category><![CDATA[Static Typing]]></category>
		<category><![CDATA[Strong Typing]]></category>
		<category><![CDATA[type coercions]]></category>
		<category><![CDATA[Variable Declaration]]></category>
		<category><![CDATA[Weak Typing]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=912</guid>
		<description><![CDATA[Till very recently I did not know the clear distinguish between Static/Dynamic and Strong/Weak typing. Thanks to Venkat for enlightening me. Dynamic typing: Variables&#8217; type declarations are not mandatory and they will be generated/inferred on the fly, by their first use. Static typing: Variable declarations are mandatory before usage, else results in a compile-time error. [...]]]></description>
			<content:encoded><![CDATA[<p>Till very recently I did not know the clear distinguish between Static/Dynamic and Strong/Weak typing. Thanks to <a href="http://agiledeveloper.com/aboutus.html" target="_blank">Venkat</a> for enlightening me.</p>
<p><span style="text-decoration: underline; color: #ff6600;"><strong>Dynamic typing</strong></span>: Variables&#8217; type declarations are not mandatory and they will be generated/inferred on the fly, by their first use.</p>
<p><span style="text-decoration: underline; color: #ff6600;"><strong>Static typing</strong></span>: Variable declarations are mandatory before usage, else results in a compile-time error.</p>
<p><span style="text-decoration: underline; color: #ff6600;"><span style="text-decoration: underline;"><strong>Strong typing</strong></span></span>: Once a variable is declared as a specific data type, it will be bound to that particular data type. You can explicitly cast the data type though.</p>
<p><span style="color: #ff6600;"><strong><span style="text-decoration: underline;">Weak typing</span></strong></span>: Variables are not of a specific data type. However it doesn&#8217;t mean that variables are not &#8220;bound&#8221; to a specific data type. In weakly typed languages, once a block of memory is associated with an object it can be reinterpreted as a different type of object.</p>
<p><a href="http://blogs.agilefaqs.com/wp-content/uploads/2009/09/strongweakstaticdynamic_type.png"><img class="aligncenter size-medium wp-image-1754" title="strongweakstaticdynamic_type" src="http://blogs.agilefaqs.com/wp-content/uploads/2009/09/strongweakstaticdynamic_type-300x202.png" alt="" width="300" height="202" /></a></p>
<blockquote><p>One thing I&#8217;ve realized, Strong vs. Weak and Dynamic vs. Static is a <em>continuum</em> rather than an absolute measure. For instance, SmallTalk is more strongly typed compared to Python which is more strongly typed than JavaScript.</p></blockquote>
<p><a href="http://blogs.agilefaqs.com/wp-content/uploads/2011/07/strongvsweak.png"><img class="alignnone size-full wp-image-1770" title="strongvsweak" src="http://blogs.agilefaqs.com/wp-content/uploads/2011/07/strongvsweak.png" alt="" width="944" height="360" /></a></p>
<p>There seem to be two major lines along which strong/weak typing is defined:</p>
<ul>
<li>The more type coercions (implicit conversions) for built-in operators the language offers, the weaker the typing. (This could also be viewed as more built-in overloading of built-in operators.)</li>
<li>The easier it is in a language, or the more ways a language offers, to reinterpret a memory block (associated with a data value) as a different type, the weaker the typing.</li>
</ul>
<p>In strongly typed languages if you cast to the wrong type, you get a runtime cast exception. While in weakly typed languages, your program might crash if you are lucky. Usually it leads to wrong behavior.</p>
<p><a href="http://blogs.agilefaqs.com/wp-content/uploads/2011/07/dynamicvsstatic.png"><img class="alignnone size-full wp-image-1771" title="dynamicvsstatic" src="http://blogs.agilefaqs.com/wp-content/uploads/2011/07/dynamicvsstatic.png" alt="" width="911" height="307" /></a></p>
<p>In most static languages you need to specify the data type at declaration. However in languages like Scala, you don&#8217;t need to specify the data types, the compiler is smart enough to infer the data types based in the context in which its used.</p>
<p>Also if you don&#8217;t have a compiler, then the language is surely dynamic language. However the inverse is not true. For example, Groovy is compiled, yet its a dynamic language.</p>
<p>In Strongly typed Dynamic languages, the type inference is postponed till runtime. This has many advantages:</p>
<ul>
<li>One can achieve greater degree of polymorphism</li>
<li>One does not need to keep fighting the compiler by doing trivial type casting</li>
<li>One gets greater flexibility by deferring the implementation to a later point. i.e. the actual type verification is postponed to runtime; allowing us to modify the structure of the program between compile time and runtime.</li>
</ul>
<p>I always thought weakly typed, dynamic language would be a disaster. However both VB and PHP (amongst most popular languages in the last 2 decades) fall into this category.</p>
<p>Having said that, these days I see more and more languages are strongly typed. Also the ability to infer types is gaining a lot of traction.</p>
<p>What do you prefer in your programming language and why?</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2011/07/11/dynamic-typing-is-not-weak-typing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Example of Test Driving Code with Events</title>
		<link>http://blogs.agilefaqs.com/2011/07/06/example-of-test-driving-code-with-events/</link>
		<comments>http://blogs.agilefaqs.com/2011/07/06/example-of-test-driving-code-with-events/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 04:01:13 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Event Handler]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[NullReferenceException]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=1745</guid>
		<description><![CDATA[Following is a stripped down version of the actual code. Start with a simple Test: private static void ContinueMethod&#40;object sender, EventArgs arg&#41; &#123; &#40;&#40;Eventful&#41;sender&#41;.ContinueExecutionFor&#40;&#34;ContinueMethod&#34;&#41;; &#125; &#160; &#91;Test&#93; public void EventHandlerCanRequestFurtherCodeExecution&#40;&#41; &#123; var eventful = new Eventful&#40;&#41;; eventful.ContinuationHandler += ContinueMethod; eventful.Execute&#40;&#41;; Assert.AreEqual&#40;&#34;ContinueMethod wants to execute more logic&#34;, eventful.State&#41;; &#125; and then wrote the production code: public [...]]]></description>
			<content:encoded><![CDATA[<p>Following is a stripped down version of the actual code.</p>
<p>Start with a simple Test:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> ContinueMethod<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs arg<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>Eventful<span style="color: #008000;">&#41;</span>sender<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ContinueExecutionFor</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ContinueMethod&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> EventHandlerCanRequestFurtherCodeExecution<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var eventful <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Eventful<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    eventful<span style="color: #008000;">.</span><span style="color: #0000FF;">ContinuationHandler</span> <span style="color: #008000;">+=</span> ContinueMethod<span style="color: #008000;">;</span>
    eventful<span style="color: #008000;">.</span><span style="color: #0000FF;">Execute</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ContinueMethod wants to execute more logic&quot;</span>, eventful<span style="color: #008000;">.</span><span style="color: #0000FF;">State</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and then wrote the production code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Eventful
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> State<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">event</span> EventHandler<span style="color: #008000;">&lt;</span>EventArgs<span style="color: #008000;">&gt;</span> ContinuationHandler<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Execute<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// logic to get ready</span>
&nbsp;
        var args <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> EventArgs<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">//stuff args with some data</span>
        ContinuationHandler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, args<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ContinueExecutionFor<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> delegateName<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        State <span style="color: #008000;">=</span> delegateName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; wants to execute more logic&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">// some logic</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The wrote the next Test:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> StopMethod<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs arg<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// no call back</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> EventHandlerCanStopFurtherCodeExecution<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    eventful<span style="color: #008000;">.</span><span style="color: #0000FF;">ContinuationHandler</span> <span style="color: #008000;">+=</span> StopMethod<span style="color: #008000;">;</span>
    eventful<span style="color: #008000;">.</span><span style="color: #0000FF;">Execute</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsNull</span><span style="color: #008000;">&#40;</span>eventful<span style="color: #008000;">.</span><span style="color: #0000FF;">State</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and this went Green.</p>
<p>Then I wondered what happened if there were no registered event handlers. So I wrote this test.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> NoOneIsInterestedInThisEvent<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    eventful<span style="color: #008000;">.</span><span style="color: #0000FF;">Execute</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsNull</span><span style="color: #008000;">&#40;</span>eventful<span style="color: #008000;">.</span><span style="color: #0000FF;">State</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This resulted in:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">NullReferenceException</span> <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">Object</span> reference not set to an instance of an <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">.</span>
<span style="color: #0000FF;">at</span> Eventful<span style="color: #008000;">.</span><span style="color: #0000FF;">Execute</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">in</span> Eventful<span style="color: #008000;">.</span><span style="color: #0000FF;">cs</span><span style="color: #008000;">:</span> line <span style="color: #FF0000;">17</span>
at EventTest<span style="color: #008000;">.</span><span style="color: #0000FF;">NoOneIsInterestedInThisEvent</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">in</span> EventTest<span style="color: #008000;">.</span><span style="color: #0000FF;">cs</span><span style="color: #008000;">:</span> line <span style="color: #FF0000;">46</span></pre></div></div>

<p>Then I updated the Execute() method in Eventful class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Execute<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// logic to get ready</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ContinuationHandler <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var args <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> EventArgs<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">//stuff args with some data</span>
        ContinuationHandler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, args<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This made all the 3 tests go Green!</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2011/07/06/example-of-test-driving-code-with-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Double Dispatch Demystified</title>
		<link>http://blogs.agilefaqs.com/2011/07/05/double-dispatch-demystified/</link>
		<comments>http://blogs.agilefaqs.com/2011/07/05/double-dispatch-demystified/#comments</comments>
		<pubDate>Tue, 05 Jul 2011 18:20:10 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Code Smells]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Double Dispatch]]></category>
		<category><![CDATA[Multiple Dispatch]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[Objects]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Polymorphism]]></category>
		<category><![CDATA[Single DIspatch]]></category>
		<category><![CDATA[Test]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=1740</guid>
		<description><![CDATA[Single Dispatch: In most object-oriented programming languages, the concrete method that is invoked at runtime depends on the dynamic (runtime) type of the object on which the method is invoked. For Example: If we have 2 cars; Car and derived class RubberCar. public class Car &#123; public virtual string CrashInto&#40;Wall wall&#41; &#123; return &#34;Car crashed [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>Single Dispatch</strong>: In most object-oriented programming languages, the concrete method that is invoked at runtime depends on the dynamic (runtime) type of the object on which the method is invoked.</p></blockquote>
<p>For Example: If we have 2 cars; Car and derived class RubberCar.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Car
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">string</span> CrashInto<span style="color: #008000;">&#40;</span>Wall wall<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span> 
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Car crashed into the Wall&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> RubberCar<span style="color: #008000;">:</span> Car
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">string</span> CrashInto<span style="color: #008000;">&#40;</span>Wall wall<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Rubber Car crashed into the Wall&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and its test code</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> SingleDispathWorksCorrectly<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Wall wall <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Wall<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Car currentCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Car<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Car crashed into the Wall&quot;</span>, currentCar<span style="color: #008000;">.</span><span style="color: #0000FF;">CrashInto</span><span style="color: #008000;">&#40;</span>wall<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    currentCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> RubberCar<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Rubber Car crashed into the Wall&quot;</span>, currentCar<span style="color: #008000;">.</span><span style="color: #0000FF;">CrashInto</span><span style="color: #008000;">&#40;</span>wall<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>First time we call the method CrashInto() on currentCar, we are holding the reference of Car and hence Car&#8217;s CrashInto() method is invoked. However the second time, we&#8217;re holding the reference of RubberCar in currentCar and CrashInto() method from RubberCar is invoked correctly. In other words this is referred to as Polymorphism.</p>
<p>Its called Single Dispatch because one object&#8217;s runtime type (object on which the method is invoked) is used to decide which concrete method to invoke. </p>
<blockquote><p><strong>Double Dispatch</strong>: A mechanism that dispatches a method call to different concrete methods depending on the runtime types of two objects involved in the call (object and its parameter)</p></blockquote>
<p>Let&#8217;s say, we had 2 types of wall, Wall and the derived class MagicWall. </p>
<p>Now when we called currentCar.CrashInto(new MagicWall()) we want to execute different behavior compared to currentCar.CrashInto(new Wall()). One classic way to implement this is:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Car class</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">string</span> CrashInto<span style="color: #008000;">&#40;</span>Wall wall<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>wall <span style="color: #008000;">is</span> MagicWall<span style="color: #008000;">&#41;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Car crashed into the Magic Wall&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Car crashed into the Wall&quot;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// RubberCar class</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">string</span> CrashInto<span style="color: #008000;">&#40;</span>Wall wall<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>wall <span style="color: #008000;">is</span> MagicWall<span style="color: #008000;">&#41;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Rubber Car crashed into the Magic Wall&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Rubber Car crashed into the Wall&quot;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This of-course works. However an alternative way to avoid  the <a href="http://www.industriallogic.com/papers/smellstorefactorings.pdf" target="_blank">Switch Statement Smell</a> and to truly use Double Dispatch is:</p>
<p>In Car:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">string</span> CrashInto<span style="color: #008000;">&#40;</span>Wall wall<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Car crashed into the Wall&quot;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">string</span> CrashInto<span style="color: #008000;">&#40;</span>MagicWall wall<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Car crashed into the Magic Wall&quot;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and in RubberCar</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">string</span> CrashInto<span style="color: #008000;">&#40;</span>Wall wall<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Rubber Car crashed into the Wall&quot;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">string</span> CrashInto<span style="color: #008000;">&#40;</span>MagicWall wall<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Rubber Car crashed into the Magic Wall&quot;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and its respective test:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CarCanCrashIntoTheWall<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Wall wall <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Wall<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Car crashed into the Wall&quot;</span>, <span style="color: #008000;">new</span> Car<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CrashInto</span><span style="color: #008000;">&#40;</span>wall<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CarCanCrashIntoTheMagicWall<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    MagicWall wall <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MagicWall<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Car crashed into the Magic Wall&quot;</span>, <span style="color: #008000;">new</span> Car<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CrashInto</span><span style="color: #008000;">&#40;</span>wall<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> RubberCarCanCrashIntoTheWall<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Wall wall <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Wall<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Rubber Car crashed into the Wall&quot;</span>, <span style="color: #008000;">new</span> RubberCar<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CrashInto</span><span style="color: #008000;">&#40;</span>wall<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> RubberCarCanCrashIntoTheMagicWall<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    MagicWall wall <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MagicWall<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;RubberCar crashed into the Magic Wall&quot;</span>, <span style="color: #008000;">new</span> RubberCar<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CrashInto</span><span style="color: #008000;">&#40;</span>wall<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Believe it or not, this is Double Dispatch in action. Which concrete method to invoke, depends on runtime type of 2 objects (car and wall.)</p>
<p>However there is a catch with this method. If you did the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> MethodOverloadingTakesPlaceAtCompileTime<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Wall wall <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MagicWall<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Car crashed into the Wall&quot;</span>, <span style="color: #008000;">new</span> Car<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CrashInto</span><span style="color: #008000;">&#40;</span>wall<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">//   Instead of &quot;Car Crashed into the Magic Wall&quot;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Since MagicWall&#8217;s reference is held in wall, which is of type Wall at compile type and overloaded method are bound at compile time, this method behaves unexpectedly. </p>
<p>One way to fix this issue is to use vars in .Net.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> UseOfVarForcesMethodOverloadingToTakesPlaceAtRunTime<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var wall <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MagicWall<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Car crashed into the Magic Wall&quot;</span>, <span style="color: #008000;">new</span> Car<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CrashInto</span><span style="color: #008000;">&#40;</span>wall<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In Languages that don&#8217;t support Double Dispatch, one needs to do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ToAvoidTheConfusion<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Wall wall <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MagicWall<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Car crashed into the Magic Wall&quot;</span>, wall<span style="color: #008000;">.</span><span style="color: #0000FF;">CollidesWith</span><span style="color: #008000;">&#40;</span>car<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, car<span style="color: #008000;">.</span><span style="color: #0000FF;">DentCount</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> DoubleDispatch_OnWall<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Wall wall <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Wall<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Car crashed into the Wall&quot;</span>, wall<span style="color: #008000;">.</span><span style="color: #0000FF;">CollidesWith</span><span style="color: #008000;">&#40;</span>car<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span>, car<span style="color: #008000;">.</span><span style="color: #0000FF;">DentCount</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Production Code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Wall
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">string</span> CollidesWithCar<span style="color: #008000;">&#40;</span>Car car<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> car<span style="color: #008000;">.</span><span style="color: #0000FF;">CrashIntoWall</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>   
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">string</span> CollidesWithRubberCar<span style="color: #008000;">&#40;</span>RubberCar car<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> car<span style="color: #008000;">.</span><span style="color: #0000FF;">CrashIntoWall</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> MagicWall <span style="color: #008000;">:</span> Wall
<span style="color: #008000;">&#123;</span> 
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">string</span> CollidesWithCar<span style="color: #008000;">&#40;</span>Car car<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> car<span style="color: #008000;">.</span><span style="color: #0000FF;">CrashIntoMagicWall</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span> 
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">string</span> CollidesWithRubberCar<span style="color: #008000;">&#40;</span>RubberCar car<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> car<span style="color: #008000;">.</span><span style="color: #0000FF;">CrashIntoMagicWall</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Car
<span style="color: #008000;">&#123;</span> 
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">string</span> CrashIntoWall<span style="color: #008000;">&#40;</span>Wall wall<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span> 
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Car crashed into the Wall&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">string</span> CrashIntoMagicWall<span style="color: #008000;">&#40;</span>Wall magicWall<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Car crashed into the Magic Wall&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> RubberCar<span style="color: #008000;">:</span>Car
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">string</span> CrashIntoWall<span style="color: #008000;">&#40;</span>Wall wall<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Rubber Car crashed into the Wall&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">string</span> CrashIntoMagicWall<span style="color: #008000;">&#40;</span>Wall magicWall<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Rubber Car crashed into the Magic Wall&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>      
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>We can expand the same technique to use more than 2 objects to decide which concrete method to invoke at run time. This mechanism is called Multiple Dispatch.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2011/07/05/double-dispatch-demystified/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Stop Over-Relying on your Beautifully Elegant Automated Tests</title>
		<link>http://blogs.agilefaqs.com/2011/06/21/stop-over-relying-on-your-beautifully-elegant-automated-tests/</link>
		<comments>http://blogs.agilefaqs.com/2011/06/21/stop-over-relying-on-your-beautifully-elegant-automated-tests/#comments</comments>
		<pubDate>Tue, 21 Jun 2011 11:47:02 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Continuous Deployment]]></category>
		<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Automated Testing]]></category>
		<category><![CDATA[Automation]]></category>
		<category><![CDATA[exploratory testing]]></category>
		<category><![CDATA[Manual Sanity Testing]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=1732</guid>
		<description><![CDATA[Time and again, I find developers (including myself) over-relying on our automated tests. esp. unit tests which run fast and  reliably. In the urge to save time today, we want to automate everything (which is good), but then we become blindfolded to this desire. Only later to realize that we&#8217;ve spent a lot more time [...]]]></description>
			<content:encoded><![CDATA[<p>Time and again, I find developers (including myself) over-relying on our automated tests. esp. unit tests which run fast and  reliably.</p>
<p>In the urge to save time today, we want to automate everything (which is good), but then we become blindfolded to this desire. Only later to realize that we&#8217;ve spent a lot more time debugging issues that our automated tests did not catch (leave the embarrassment caused because of this aside.)</p>
<p>I&#8217;ve come to believe:</p>
<blockquote><p>A little bit of manual, sanity testing by the developer before checking in code can save hours of debugging and embarrassment.</p></blockquote>
<p>Again this is contextual and needs personal judgement based on the nature of the change one makes to the code.</p>
<p>In addition to quickly doing some manual, sanity test on your machine before checking, it can be extremely important to do some <a href="http://en.wikipedia.org/wiki/Exploratory_testing">exploratory testing</a> as well. However, not always we can test things locally. In those cases, we can test them on a staging environment or on a live server. But its important for you to discover the issue much before your users encounter it.</p>
<p>P.S: Recently we faced <a title="Error CS0234: The type or namespace name ‘Specialized’ does not exist in the namespace ‘System.Collections’" href="http://blogs.agilefaqs.com/2011/06/21/error-cs0234-the-type-or-namespace-name-specialized-does-not-exist-in-the-namespace-system-collections/">Error CS0234: The type or namespace name ‘Specialized’ does not exist in the namespace ‘System.Collections’</a> issue, which prompted me to write this blog post.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2011/06/21/stop-over-relying-on-your-beautifully-elegant-automated-tests/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Error CS0234: The type or namespace name &#8216;Specialized&#8217; does not exist in the namespace &#8216;System.Collections&#8217;</title>
		<link>http://blogs.agilefaqs.com/2011/06/21/error-cs0234-the-type-or-namespace-name-specialized-does-not-exist-in-the-namespace-system-collections/</link>
		<comments>http://blogs.agilefaqs.com/2011/06/21/error-cs0234-the-type-or-namespace-name-specialized-does-not-exist-in-the-namespace-system-collections/#comments</comments>
		<pubDate>Tue, 21 Jun 2011 11:39:41 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 2.0]]></category>
		<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[error CS0234]]></category>
		<category><![CDATA[System.Collections. Specialized]]></category>
		<category><![CDATA[System.Core.dll]]></category>
		<category><![CDATA[System.dll]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=1733</guid>
		<description><![CDATA[Industrial Logic&#8217;s eLearning has a feature where students can upload their programming exercise and get automated, personalized feedback. We do various server-side analysis (automated critique) of the student&#8217;s code to score them and to give them feedback about how well they performed in their programming exercises. To do this we need to compile their code [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://industriallogic.com/shop">Industrial Logic&#8217;s eLearning</a> has a feature where students can upload their programming exercise and <a href="http://www.youtube.com/watch?v=T5XTBRphaIU">get automated, personalized feedback</a>. We do various server-side analysis (automated critique) of the student&#8217;s code to score them and to give them feedback about how well they performed in their programming exercises. To do this we need to compile their code on our server.</p>
<p>Recently we upgraded our .Net Compiler from version 2.0 to version 3.5. In version 2.0 we had to provide a reference to System.dll file (located in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll) for compiling. In version 3.5, they don&#8217;t have System.dll file. It was replaced by System.core.dll (located in c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll)</p>
<p>After making this change, when we ran the compiler using c:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe we got the following error:</p>
<blockquote><p>Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.1<br />
for Microsoft (R) .NET Framework version 3.5<br />
Copyright (C) Microsoft Corporation. All rights reserved.</p>
<p>SomeCSharpClass.cs: error CS0234: The type or namespace name &#8216;Specialized&#8217; does not exist in the namespace &#8216;System.Collections&#8217; (are you missing an assembly reference?)</p></blockquote>
<p>It turns out that &#8216;System.collections.specialized&#8217; namespace used to exist in System.dll in .net 2.0. In 3.5 System.Core.dll (which replaced the System.dll) does not contain it. Hence the compile time error.</p>
<p>We&#8217;ve fixed this issue by adding both System.Core.dll (v3.5) and System.dll (v2.0) to the compiler reference path. Not sure if this is the right thing to do. But it seems to work.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2011/06/21/error-cs0234-the-type-or-namespace-name-specialized-does-not-exist-in-the-namespace-system-collections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eclipse, CVS Command-Line-Client and extssh</title>
		<link>http://blogs.agilefaqs.com/2011/06/03/eclipse-cvs-command-line-client-and-extssh/</link>
		<comments>http://blogs.agilefaqs.com/2011/06/03/eclipse-cvs-command-line-client-and-extssh/#comments</comments>
		<pubDate>Fri, 03 Jun 2011 08:30:16 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[CVS]]></category>
		<category><![CDATA[CVS 1.12.13]]></category>
		<category><![CDATA[CVS Repository]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[ext]]></category>
		<category><![CDATA[extssh]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=1699</guid>
		<description><![CDATA[As an Eclipse user, who still works with CVS for some projects, when I check-out a repository through Eclipse, I have the choice between extssh method: Enter the password only once, ext method: Enter the password at every operation, several times. &#8216;extssh&#8217; is the obvious choice. And life is good, until you need to use [...]]]></description>
			<content:encoded><![CDATA[<p>As an Eclipse user, who still works with CVS for some projects, when I check-out a repository through Eclipse, I have the choice between</p>
<ul>
<li>extssh method: Enter the password only once,</li>
<li>ext method: Enter the password at every operation, several times.</li>
</ul>
<p>&#8216;extssh&#8217; is the obvious choice. And life is good, until you need to use the command line CVS client.</p>
<p>The latest version of CVS 1.12.13 from ftp.gnu.org does not support extssh method.</p>
<p>If you run a CVS command from the command-line on a project created by Eclipse, it gives the following error message:</p>
<blockquote><p>$cvs log<br />
cvs log: Unknown method (`extssh&#8217;) in CVSROOT.<br />
cvs log: in directory .:<br />
cvs log: ignoring CVS/Root because it does not contain a valid root.<br />
cvs log: No CVSROOT specified!  Please use the `-d&#8217; option<br />
cvs [log aborted]: or set the CVSROOT environment variable.</p></blockquote>
<p>Unfortunately even after having reported this issue several years ago, the official cvs program does not support this method.</p>
<p>Luckily Eclipse offers an improved interoperability. You can specify &#8216;ext&#8217; method while checking out the repository. Then via<br />
Eclipse &gt; Preferences &gt; Team &gt; CVS &gt; Ext Connection Method</p>
<p>Select &#8216;Use another connection method type to connect&#8217; and specify &#8216;Connection Type&#8217; as &#8216;extssh&#8217;</p>
<div align="center">
<a href="http://blogs.agilefaqs.com/wp-content/uploads/2011/06/CVSExt.jpg"><img class="aligncenter size-full wp-image-1700" title="CVSExt" src="http://blogs.agilefaqs.com/wp-content/uploads/2011/06/CVSExt.jpg" alt="" width="500" height="382" /></a>
</div>
<p>If you had already created a project using extssh, don&#8217;t worry. You can always go the CVS Perspective &gt; Select your repository &gt; right click &gt; Properties &gt; change the connection type back to ext.</p>
<div align="center">
<a href="http://blogs.agilefaqs.com/wp-content/uploads/2011/06/CVSProperties.jpg"><img class="aligncenter size-full wp-image-1701" title="CVSProperties" src="http://blogs.agilefaqs.com/wp-content/uploads/2011/06/CVSProperties1.jpg" alt="" width="500" height="386" /></a>
</div>
<p>It will take a while for Eclipse to update the meta files on the disk. Once its done, you should be able to execute cvs commands from both Eclipse and the command line client.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2011/06/03/eclipse-cvs-command-line-client-and-extssh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When Should We Encourage Developers to Write Comments?</title>
		<link>http://blogs.agilefaqs.com/2011/05/15/when-should-we-encourage-developers-to-write-comments/</link>
		<comments>http://blogs.agilefaqs.com/2011/05/15/when-should-we-encourage-developers-to-write-comments/#comments</comments>
		<pubDate>Sun, 15 May 2011 10:31:36 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Code Smells]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Training]]></category>
		<category><![CDATA[code smells]]></category>
		<category><![CDATA[Comments]]></category>
		<category><![CDATA[Comments Smell]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Fluent Interfaces]]></category>
		<category><![CDATA[Self-Documenting Code]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=1682</guid>
		<description><![CDATA[Many people will argue that there is more badly written code than good code. And its important to write comments to avoid these situations. Therefore we should encourage (force) people to write comments. IMHO they are absolutely right that today many project suffer from poorly written code without any (good) comments. However every team I [...]]]></description>
			<content:encoded><![CDATA[<p>Many people will argue that there is more badly written code than good code. And its important to write comments to avoid these situations. Therefore we should encourage (force) people to write comments.</p>
<p>IMHO they are absolutely right that today many project suffer from poorly written code without any (good) comments. However every team I know, that suffers from this problem, has always been told (forced) to write comments. In spite of the emphasis on writing comments it has not really helped them.</p>
<p>I usually ask:</p>
<blockquote><p>By asking developers to write comments are we really addressing the root of the problem?</p></blockquote>
<p><span style="color: #ff6600;"><strong><span style="text-decoration: underline;">.i.e. developers don&#8217;t invest quality time to write self-documenting code; code that clearly communicates its intent and does not require the deodorant of comments.</span></strong></span></p>
<blockquote><p>May be its time to try something different?</p></blockquote>
<p>I have seen this myself many times, when we emphasize &amp; <a href="https://elearning.industriallogic.com/gh/submit?Action=AlbumContentsAction&amp;album=recognizingSmells&amp;devLanguage=Java">educate the team on how to write clean code</a> and ask them to stop wasting time writing comments, the code starts to communicate lot better. Its lot more maintainable. Also we have found that <a href="http://blogs.agilefaqs.com/2008/12/27/fluent-interfaces-improve-readability-of-my-tests/">writing automated tests</a> is a great way to document your intent as well.</p>
<p>This is how I would explain the concept Comments Smell to a team:</p>
<p><span style="color: #ff6600;"><strong><span style="text-decoration: underline;">Writing comments that explain &#8220;how&#8221; or &#8220;what&#8221; the code does, what it does, is evil IMHO</span></strong></span>. Comments (esp. about what and how) is a clear failure to express the intent in code. Comment is a deodorant to hide that failure (smell).</p>
<ul>
<li>If developers don&#8217;t invest time to write clear code, what is the guarantee that they will write clear comments?</li>
<li>Is doing a mediocre job at both (comments and code) better than doing a great job at just Code?</li>
<li>Will it actually be more productive to do both or just one?</li>
</ul>
<p>Remember the biggest problem with comments it that they <span style="color: #ff6600;"><strong><span style="text-decoration: underline;">fall out-of-sync</span></strong></span> with code very soon. So its not just about the extra investment to write good comments, but also the investment to maintain them.</p>
<p>One has to think hard to write code that expresses intent rather than write some sloppy code with poor abstractions and get away (washing their hands off) by writing comments. Developers have to take responsibility for writing code that others can easily understand.</p>
<p>Having said that, there are times when &#8220;the why&#8221; (why we are doing something in the code, a particular way) is not apparent by just looking at the code. So if we don’t find a suitable way to communicate &#8220;the why&#8221; through code, comment is the fall back option.</p>
<p><span style="text-decoration: underline; color: #ff6600;"><strong>Note that comments are a fall back option in &#8220;the why&#8221; case rather than a default option.</strong></span></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2011/05/15/when-should-we-encourage-developers-to-write-comments/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Preemptively Branching a Release Candidate and Splitting Teams Considered Harmful</title>
		<link>http://blogs.agilefaqs.com/2011/04/18/preemptively-branching-a-release-candidate-and-splitting-teams-considered-harmful/</link>
		<comments>http://blogs.agilefaqs.com/2011/04/18/preemptively-branching-a-release-candidate-and-splitting-teams-considered-harmful/#comments</comments>
		<pubDate>Sun, 17 Apr 2011 23:55:08 +0000</pubDate>
		<dc:creator>Naresh Jain</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Continuous Deployment]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Organizational]]></category>
		<category><![CDATA[Planning]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[branching]]></category>
		<category><![CDATA[CI]]></category>
		<category><![CDATA[Continuous Integration]]></category>
		<category><![CDATA[continuous learning]]></category>
		<category><![CDATA[cut corners]]></category>
		<category><![CDATA[Knowledge divide]]></category>
		<category><![CDATA[over-staffed]]></category>
		<category><![CDATA[Release Hardening]]></category>
		<category><![CDATA[resource utilization]]></category>
		<category><![CDATA[Skill divide]]></category>
		<category><![CDATA[throughput]]></category>
		<category><![CDATA[Version control branching]]></category>
		<category><![CDATA[WIP]]></category>
		<category><![CDATA[WIP Limit]]></category>
		<category><![CDATA[work-in-proress]]></category>

		<guid isPermaLink="false">http://blogs.agilefaqs.com/?p=1662</guid>
		<description><![CDATA[Building on top of my previous blog entry: Version Control Branching (extensively) Considered Harmful I always discourage teams from preemptively branching a release candidate and then splitting their team to harden the release while rest of the team continues working on next release features. My reasoning: Increases the work-in-progress and creates a lot of planning, [...]]]></description>
			<content:encoded><![CDATA[<p>Building on top of my previous blog entry: <a href="http://blogs.agilefaqs.com/2011/04/17/version-control-branching-extensively-considered-harmful/">Version Control Branching (extensively) Considered Harmful</a></p>
<p>I always discourage teams from <em>preemptively</em> branching a release candidate and then splitting their team to harden the release while rest of the team continues working on next release features.</p>
<div align="center">
<a href="http://blogs.agilefaqs.com/wp-content/uploads/2011/04/FallingTree.jpg"><img class="aligncenter size-full wp-image-1663" title="FallingTree" src="http://blogs.agilefaqs.com/wp-content/uploads/2011/04/FallingTree.jpg" alt="" width="300" height="200" /></a></div>
<p>My reasoning:</p>
<ul>
<li>Increases the <span style="color: #ff6600;"><em>work-in-progress</em></span> and creates a lot of planning, management, version-control, testing, etc. <span style="color: #ff6600;"><em>overheads</em></span>.</li>
<li>In the grand scheme of things, we are focusing on <em><span style="color: #ff6600;">resource utilization</span></em>, but the <em><span style="color: #ff6600;">throughput</span></em> of the overall system is actually reducing.</li>
<li>During development, teams get very focused on <span style="color: #ff6600;"><em>churning out features</em></span>. Subconsciously they know there will be a hardening/optimization phase at the end, so they tend to <em><span style="color: #ff6600;">cut corners for short-term speed gains</span></em>. This attitude had a snowball effect. Overall encourages a <em><span style="color: #ff6600;">&#8220;not-my-problem&#8221; attitude</span></em> towards quality, performance and overall usability.</li>
<li>The team (developers, testers and managers) responsible for hardening the release have to <em><span style="color: #ff6600;">work extremely hard, under high pressure</span></em> causing them to burn-out (and possibly introducing more problems into the system.) They have to suffer for the mistakes others have done. Does not seem like a fair system.</li>
<li>Because the team is under high pressure to deliver the release, even though they know something really needs to be <em><span style="color: #ff6600;">redesigned/refactored</span></em>, they just <span style="color: #ff6600;"><em>patch it up</em></span>. Constantly doing this, really creates a big ball of complex mud that only a few people understand.</li>
<li>Creates a &#8220;<em><span style="color: #ff6600;">Knowledge/Skill divide</span></em>&#8221; between the developers and testers of the team. Generally the best (most trusted and knowledgable) members are pick up to work on the release hardening and performance optimization. They learn many interesting things while doing this. This newly acquired knowledge does not effectively get communicate back to other team members (mostly developers). Others continue doing what they used to do (potentially wrong things which the hardening team has to fix later.)</li>
<li>As releases pass by, there are <em><span style="color: #ff6600;">fewer and fewer people</span></em> who understand the overall system and only they are able to effectively harden the project. This is a huge <em><span style="color: #ff6600;">project risk</span></em>.</li>
<li>Over a period of time, every new release needs <em><span style="color: #ff6600;">more hardening time</span></em> due to the points highlighted above. This approach does not seem like a good strategy of getting out of the problem.</li>
</ul>
<blockquote><p>If something hurts, do it all the time to reduce the pain and get better at it.</p></blockquote>
<p><span style="text-decoration: underline;">Hence we should build release hardening as much as possible into the routine everyday work. If you still need hardening at the end, then instead of splitting the teams, we should let the whole swamp on making the release</span>.</p>
<p>Also usually I notice that if only a subset of the team can effectively do the hardening, then its a good indication that the team is <em><span style="color: #ff6600;">over-staffed</span></em> and that might be one of the reasons for many problems in the first place. It might be worth considering down-sizing your team to see if some of those problems can be addressed.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.agilefaqs.com/2011/04/18/preemptively-branching-a-release-candidate-and-splitting-teams-considered-harmful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

