<?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>It's a .NET Life &#187; TDD</title>
	<atom:link href="http://www.david-yancey.com/blog/index.php/tag/tdd/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.david-yancey.com/blog</link>
	<description>Thoughts on .NET, Agile and beyond</description>
	<lastBuildDate>Mon, 26 Apr 2010 00:31:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Code Noise!!!</title>
		<link>http://www.david-yancey.com/blog/index.php/2008/12/10/dirty-code/</link>
		<comments>http://www.david-yancey.com/blog/index.php/2008/12/10/dirty-code/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 05:16:02 +0000</pubDate>
		<dc:creator>david.yancey</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.david-yancey.com/blog/index.php/2008/12/10/dirty-code/</guid>
		<description><![CDATA[If 4 letter words immediately come to mind when you do a code review&#8230;then you might have dirty code. If you wince each time a new feature is requested to be added to your existing library of code&#8230;then you might have dirty code. If the amount of time it takes to research a bug is inversely [...]]]></description>
			<content:encoded><![CDATA[<p>If 4 letter words immediately come to mind when you do a code review&#8230;then you might have dirty code.</p>
<p>If you wince each time a new feature is requested to be added to your existing library of code&#8230;then you might have dirty code.</p>
<p>If the amount of time it takes to research a bug is inversely proportional to the amount of time you took to develop said bug&#8230;then you might have dirty code.</p>
<p>Alright enough of my attempts to making &#8220;redneck jokes&#8221; about dirty code.  Lets take a look first at what defines dirty code.  Then we&#8217;ll take a look at some tips/tricks to refactor dirty code into what would be considered clean code.</p>
<p><strong>What makes code dirty?</strong></p>
<p>Bad Names:</p>
<p>Take a look at the code below and try to determine what each variable represents.  You&#8217;ll be able to after studying the code for a few minuets, but as programmers we should write our code so that the intent is revealed not just in the method names but also the parameter names and variable names.</p>
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">static</span> XDocument BuildXml(IEnumerable&lt;v&gt; vL)
        {
            XDocument x = <span class="kwrd">new</span> XDocument();
            XElement r= <span class="kwrd">new</span> XElement(<span class="str">"searchResults"</span>);
            XElement b= <span class="kwrd">new</span> XElement(<span class="str">"book"</span>, vL.FirstOrDefault().BookName);
            results.Add b
            <span class="kwrd">foreach</span> (var v <span class="kwrd">in</span> vL)
            {
                XElement v1 = <span class="kwrd">new</span> XElement(<span class="str">"verse"</span>,
                              <span class="kwrd">new</span> XElement(<span class="str">"chapter"</span>, v.Chapter),
                              <span class="kwrd">new</span> XElement(<span class="str">"verseNumber"</span>, v.VerseNumber),
                              <span class="kwrd">new</span> XElement(<span class="str">"verseText"</span>, v.Verse)
                     );
                results.Add v1
            }

            returnXML.Add(r);
            <span class="kwrd">return</span> returnXML;

        }</pre>
<p>Noisy Comments:</p>
<p>We&#8217;ve all seen these comments, probably most of us are guilty of creating them.  Noisy comments are comments that serve no other purpose other than to repeat what has been stated in the code.  Comments should reveal intent that is not apparent in the code.</p>
<pre class="csharpcode"><span class="rem">/// RandomVerse</span>
<span class="rem">/// returns a random integer</span>
<span class="rem">/// uses no parameters</span>
<span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">int</span> RandomVerse()
{
    <span class="kwrd">return</span> GetRandomNumber(1, 30000);
}</pre>
<p>Busy Methods:</p>
<p>Busy methods are those which do to much.  Methods need to follow the Single Responsibility Principle which says that a method should do one thing, do it well, and do only that one thing.  Well the SRP is commonly applied to Class&#8217;s but I think it fits Methods as well.</p>
<p>There are lots of articles, blogs, books on what dirty code looks like and what to do with it.</p>
<p><a href="http://www.codinghorror.com/">Coding Horror:</a><br />
Jeff Atwood has  quite a few good articles on the subject.</p>
<p><a title="http://www.codinghorror.com/blog/archives/000589.html" href="http://www.codinghorror.com/blog/archives/000589.html">Smelly Code</a></p>
<p><a title="http://www.codinghorror.com/blog/archives/000805.html" href="http://www.codinghorror.com/blog/archives/000805.html">Curly&#8217;s Law</a></p>
<p><a href="http://blog.objectmentor.com/">Robert Martin (Uncle Bob)</a></p>
<p>Robert Martin has written a book on the subject of <a style="&amp;quot;border: none;" href="&lt;a href=&quot;http://www.amazon.com/gp/product/0132350882?ie=UTF8&amp;tag=isanl-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0132350882&quot;&gt;Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin Series)&lt;/a&gt;&lt;img src=">Clean Code</a>.  Which I highly recommend.</p>
<p><strong>What to do with Dirty Code?</strong></p>
<p>Refactor!!!</p>
<p>to be continued&#8230;</p>
<p>David Yancey</p>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.david-yancey.com/blog/index.php/2008/12/10/dirty-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Refactoring: Getting started is the hard part</title>
		<link>http://www.david-yancey.com/blog/index.php/2008/11/25/refactoring-getting-started-is-the-hard-part/</link>
		<comments>http://www.david-yancey.com/blog/index.php/2008/11/25/refactoring-getting-started-is-the-hard-part/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 06:50:59 +0000</pubDate>
		<dc:creator>david.yancey</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.david-yancey.com/blog/index.php/2008/11/25/refactoring-getting-started-is-the-hard-part/</guid>
		<description><![CDATA[Just the thought of &#8216;refactoring&#8217; can be daunting to a programmer.  Martin Fowler defines &#8216;refactoring&#8217; as: Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations. Each transformation (called a &#8216;refactoring&#8217;) does little, but [...]]]></description>
			<content:encoded><![CDATA[<p>Just the thought of &#8216;refactoring&#8217; can be daunting to a programmer. </p>
<p><a href="http://www.refactoring.com/">Martin Fowler</a> defines &#8216;refactoring&#8217; as:</p>
<blockquote><p>Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations. Each transformation (called a &#8216;refactoring&#8217;) does little, but a sequence of transformations can produce a significant restructuring. Since each refactoring is small, it&#8217;s less likely to go wrong. The system is also kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring.</p></blockquote>
<p>When I first started to look at our library of Legacy Code my first thought was to just scrap the library and start over.  Now try to sell that idea to the stake holders.  Not going to happen is it.  So that&#8217;s where refactoring come&#8217;s in.  But there are a few obstacles we have to over come in this process.</p>
<ol>
<li><strong>Sell the process to the product owner/business:<br />
</strong><a href="http://www.jbrains.ca/">J.B Rainsburger</a> used an analogy that helps us with this obstacle.  The use of credit cards.  Each time we purchase an item with a credit card, we are purchasing that item with something we don&#8217;t have which is money.  Now let&#8217;s take this and apply it to programming against that library of legacy code.  Each time we do we are developing on credit and spending something we don&#8217;t have which is time.</li>
<li><strong>Single Methods with large amounts of code:</strong><br />
First let me refer you to an excellent book by <a href="http://www.objectmentor.com/omTeam/feathers_m.html">Michael Feathers</a> titled <a href="http://www.amazon.com/gp/product/0131177052?ie=UTF8&amp;tag=isanl-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0131177052">Working Effectively with Legacy Code</a><img style="margin: 0px; border-top-style: none! important; border-right-style: none! important; border-left-style: none! important; border-bottom-style: none! important" src="http://www.assoc-amazon.com/e/ir?t=isanl-20&amp;l=as2&amp;o=1&amp;a=0131177052" border="0" alt="" width="1" height="1" />.  In this book Michael suggests that we take that method and create a class with only that code.  With this class we can now start writing unit tests for the code and begin refactoring.  Now in our legacy code where we just pulled the large method from create a call to the method in our new class. </li>
<li><strong>Duplicate Code:<br />
</strong>“When N things need to change and N&gt;1, <a href="http://www.netobjectives.com/blogs/shalloways-law-and-shalloways-principle">Shalloway</a> will find at most N-1 of these things.”  Basically what Alan Shalloway is saying here is that if you have more than 1 duplication of code chances are high you won&#8217;t find all instances of that duplication.  This is where design patterns come in and the phrase &#8220;refactor to patterns&#8221;.</li>
<li><strong>Where do I start?:<br />
</strong>The hardest obstacle to overcome is where to start.  Practice</p>
<ul>
<li>Start off by finding a section of code that you feel may could use some refactoring. </li>
<li>Work on refactoring it for 30min.</li>
<li>Throw that code away. </li>
<li>Take a break</li>
<li>Repeat.</li>
</ul>
<p>&#8220;Throw away my code!!!&#8221; you might be saying to yourself right now. Yes, Throw it away. What this does for you is reminds you that you are just practicing and 2nd it separates you from your code.</li>
<li><strong>TDD and Refactoring?:</strong>  Yes its possible.  As you go through your refactoring you&#8217;ll see that you are refactoring to patterns, while you are doing this, ask yourself &#8220;how would I want to test this code?&#8221;  Retrain your way of thinking in how you approach code and you&#8217;ll soon find yourself refactoring through TDD and Design Patterns.</li>
</ol>
<p> </p>
<p>So there you have a few tips on how to overcome a few obstacles when you start to look at refactoring your legacy code for the first time.</p>
<p> </p>
<p>David Yancey</p>
]]></content:encoded>
			<wfw:commentRss>http://www.david-yancey.com/blog/index.php/2008/11/25/refactoring-getting-started-is-the-hard-part/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
