<?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>Crafting software&#187; Code smells</title>
	<atom:link href="http://davidlaing.com/category/software-craftmanship/code-smells/feed/" rel="self" type="application/rss+xml" />
	<link>http://davidlaing.com</link>
	<description>David Laing&#039;s thoughts on software development</description>
	<lastBuildDate>Wed, 01 Feb 2012 11:02:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Implementing the strategy pattern without an explosion of classes &#8211; part 3 of ??</title>
		<link>http://davidlaing.com/2011/04/16/implementing-the-strategy-pattern-without-an-explosion-of-classes-part-3-of/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=implementing-the-strategy-pattern-without-an-explosion-of-classes-part-3-of</link>
		<comments>http://davidlaing.com/2011/04/16/implementing-the-strategy-pattern-without-an-explosion-of-classes-part-3-of/#comments</comments>
		<pubDate>Sat, 16 Apr 2011 21:51:25 +0000</pubDate>
		<dc:creator>mrdavidlaing</dc:creator>
				<category><![CDATA[Code smells]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Refactoring]]></category>

		<guid isPermaLink="false">http://davidlaing.radweb.co.za/?p=272</guid>
		<description><![CDATA[I feel uncomfortable when I see large switch statements. I appreciate how they break the Open Closed Principle. I have enough experience to know that they seem to attract extra conditions &#38; additional logic during maintenance, and quickly become bug hotspots. A refactoring I use frequently to deal with this is Replace Conditional with Polymorphism; [...]]]></description>
			<content:encoded><![CDATA[<p>I feel uncomfortable when I see large switch statements.  I appreciate how they break the <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open Closed Principle</a>.  I have enough experience to know that they seem to attract extra conditions &amp; additional logic during maintenance, and quickly become bug hotspots.</p>
<p>A refactoring I use frequently to deal with this is <a href="http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html">Replace Conditional with Polymorphism</a>; but for simple switches, its always seemed like a rather large hammer.</p>
<p>Take the following simple example that performs slightly different processing logic based on the credit card type:</p>
<p><script src="https://gist.github.com/923516.js?file=CreditCard-simple-switch.js"></script> Its highly likely that the number of credit card types will increase; and that the complexity of processing logic for each will also increase over time.  The traditional application of the <a href="http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html">Replace Conditional with Polymorphism</a> refactoring gives the following:  </p>
<p><script src="https://gist.github.com/923516.js?file=CreditCard-simple-strategy.js"></script></p>
<p>This explosion of classes containing almost zero logic has always bothered me as quite a lot of boilerplate overhead for a relatively small reduction in complexity.</p>
<p>Consider however, the functional approach to the same refactoring:</p>
<p><script src="https://gist.github.com/923516.js?file=CreditCard-simple-functional.js"></script> </p>
<p>Here we have obtained the same simplification of the switch statement; but avoided the explosion of simple classes.  Whilst strictly speaking we are still violating the  <a href="http://en.wikipedia.org/wiki/Open/closed_principle">Open Closed Principle</a>; we do have a collection of simple methods that are easy to comprehend and test.  It&#8217;s worth noting that when our logic becomes very complex; converting to the OO Strategy pattern becomes a more compelling option.  Consider the case when we include a collection of validation logic for each credit card:  <script src="https://gist.github.com/923516.js?file=CreditCard-complex-functional.js"></script></p>
<p>In this case the whole file starts to feel too complex to me; and having the logic partitioned into separate strategy classes / files seems more maintainable to me.</p>
<p><script src="https://gist.github.com/923516.js?file=CreditCard-complex-strategy.js"></script></p>
<p>To conclude then, the fact that languages treat functions as first class constructs, gives us the flexibility to use them in a &#8220;polymorphic&#8221; way; where our &#8220;interface&#8221; is the function signature.  </p>
<p>And for some problems, like a refactoring a simple switch statement; I feel this gives us a more elegant solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://davidlaing.com/2011/04/16/implementing-the-strategy-pattern-without-an-explosion-of-classes-part-3-of/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fitnesse Smell &#8211; Executable Requirements that look like Scripts</title>
		<link>http://davidlaing.com/2010/06/16/fitnesse-smell-executable-requirements-that-look-like-scripts/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fitnesse-smell-executable-requirements-that-look-like-scripts</link>
		<comments>http://davidlaing.com/2010/06/16/fitnesse-smell-executable-requirements-that-look-like-scripts/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 14:11:19 +0000</pubDate>
		<dc:creator>mrdavidlaing</dc:creator>
				<category><![CDATA[Code smells]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[acceptance tests]]></category>
		<category><![CDATA[codesmell]]></category>
		<category><![CDATA[fitnesse]]></category>

		<guid isPermaLink="false">http://davidlaing.com/?p=232</guid>
		<description><![CDATA[Gojko (who wrote the Fitnesse book) has this interesting discussion on what makes a good acceptance test in Fitnesse. http://gojko.net/2010/06/16/anatomy-of-a-good-acceptance-test/ His point seems to be that Fitnesse is a good tool for documenting specifications, and continuously automating their validation. When your Fitnesse tests become like &#8220;scripts&#8221; (which is how developers are trained to see the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://gojko.net">Gojko</a> (who wrote the <a href="http://gojko.net/2009/12/07/fitnesse-book-now-free-online/">Fitnesse book</a>) has this interesting discussion on what makes a good acceptance test in Fitnesse.</p>
<p><a href="http://gojko.net/2010/06/16/anatomy-of-a-good-acceptance-test/">http://gojko.net/2010/06/16/anatomy-of-a-good-acceptance-test/</a></p>
<p>His point seems to be that Fitnesse is a good tool for documenting specifications, and continuously automating their validation.  When your Fitnesse tests become like &#8220;scripts&#8221; (which is how developers are trained to see the world), then Fitnesse is a pretty crummy test execution environment (just use a unit test runner!)</p>
<p>Interesting that alternate tools &#8211; http://www.concordion.org, http://wiki.github.com/aslakhellesoy/cucumber/, http://www.specflow.org/ have arisen that effectively try to limit the power of the &#8220;test description language&#8221; to prevent the acceptance tests becoming script like.</p>
<p>Interesting food for thought &#8211; I know that many of my Fitnesse tests exhibit this codesmell</p>
]]></content:encoded>
			<wfw:commentRss>http://davidlaing.com/2010/06/16/fitnesse-smell-executable-requirements-that-look-like-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Utils anti-pattern &amp; AutoMapper</title>
		<link>http://davidlaing.com/2009/06/02/utils-anti-pattern-automapper/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=utils-anti-pattern-automapper</link>
		<comments>http://davidlaing.com/2009/06/02/utils-anti-pattern-automapper/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 09:18:41 +0000</pubDate>
		<dc:creator>mrdavidlaing</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Code smells]]></category>
		<category><![CDATA[automapper]]></category>
		<category><![CDATA[utils-anti-pattern]]></category>

		<guid isPermaLink="false">http://davidlaing.com/?p=190</guid>
		<description><![CDATA[These are more for my reference purposes &#8211; hopefully you&#8217;ll find them useful: If you&#8217;re about to name a class **Util; think harder &#8211; there is a better name that discribes what that class is for: Chriss Missal has some advice for you Faced with the prospect of heaps of left hand side =&#62; right [...]]]></description>
			<content:encoded><![CDATA[<p>These are more for my reference purposes &#8211; hopefully you&#8217;ll find them useful:</p>
<p>If you&#8217;re about to name a class **Util; think harder &#8211; there is a better name that discribes what that class is for:<br />
<a href="http://www.lostechies.com/blogs/chrismissal/archive/2009/06/01/anti-patterns-and-worst-practices-utils-class.aspx">Chriss Missal has some advice for you</a></p>
<p>Faced with the prospect of heaps of left hand side =&gt; right hand side code in your DTO of anti-corruption layer?  Consider <a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/01/22/automapper-the-object-object-mapper.aspx">Jimmy Bogard&#8217;s Automapper</a></p>
]]></content:encoded>
			<wfw:commentRss>http://davidlaing.com/2009/06/02/utils-anti-pattern-automapper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Functions with side effects are just rude!</title>
		<link>http://davidlaing.com/2009/02/25/functions-with-side-effects-are-just-rude/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=functions-with-side-effects-are-just-rude</link>
		<comments>http://davidlaing.com/2009/02/25/functions-with-side-effects-are-just-rude/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 14:30:26 +0000</pubDate>
		<dc:creator>mrdavidlaing</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[Code smells]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://davidlaing.com/?p=155</guid>
		<description><![CDATA[Today I fell into a trap when using a function that had a side effect &#8211; it unexpectedly changed an input parameter; causing a later statement to fail. Debugging took an age! For example, consider the following function: string StringReplace(string haystack, string needle) If this function is side-effect free, we can use it without fear [...]]]></description>
			<content:encoded><![CDATA[<p>Today I fell into a trap when using a function that had a side effect &#8211; it unexpectedly changed an input parameter; causing a later statement to fail.  Debugging took an age!</p>
<p>For example, consider the following function:</p>
<pre>
      string StringReplace(string haystack, string needle)
</pre>
<p>If this function is side-effect free, we can use it without fear like this:</p>
<pre>
        string menagerie = "cat,dog,bee,llama";
        string catFreeMenagerie = StringReplace(menagerie, "cat");
        string beeFreeMengerie = StringReplace(menagerie, "eric");

        Assert.AreEqual(",dog,fish,llama", catFreeMenagerie);
        Assert.AreEqual("cat,dog,,llama", beeFreeMengerie);
</pre>
<p>However, if StringReplace() had the side effect of also changing the passed in haystack, then the second Assert would fail, because the first StringReplace has the unexpected side effect of changing one of its arguments.</p>
<p>Evans in the DDD book has quite a bit to say about this; arguing that <a href="http://my.safaribooksonline.com/0321125215/ch10lev1sec2">having side effect free functions goes a long way towards making a supple design</a></p>
<p><a href="http://webmat.wordpress.com/2007/12/13/an-easy-way-to-make-your-code-more-testable/">Side effect free functions also make testing &amp; refactoring easier</a> (less state to worry about etc)</p>
<p>Remember, a function that changes its parameters is rude, and should not be trusted!</p>
<p>PS:  <a href="http://lyrics.doheth.co.uk/songs/monty-python/sings/eric-the-half-a-bee.php">Eric the half a bee lyrics</a></p>
]]></content:encoded>
			<wfw:commentRss>http://davidlaing.com/2009/02/25/functions-with-side-effects-are-just-rude/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

