<?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>PHP in Action &#187; Unit testing</title>
	<atom:link href="http://blog.agilephp.com/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.agilephp.com</link>
	<description>Dagfinn Reiersøl on PHP, agile development, Ruby and other addictive substances</description>
	<lastBuildDate>Mon, 28 Sep 2009 14:35:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Don&#8217;t refactor without unit tests</title>
		<link>http://blog.agilephp.com/2009/09/03/dont-refactor-without-unit-tests/</link>
		<comments>http://blog.agilephp.com/2009/09/03/dont-refactor-without-unit-tests/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 13:56:49 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Clean code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Unit testing]]></category>

		<guid isPermaLink="false">http://blog.agilephp.com/?p=1596</guid>
		<description><![CDATA[



Image by niallkennedy via Flickr



Brandon Savage is writing a series on code improvement using a code example (starting with Peer Review: Taking Code And Making It Better). In other words, it&#8217;s about refactoring, which is practically my favorite subject. Although I don&#8217;t agree with all of it, it&#8217;s mostly good advice. I recommend it.
That said, [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl class="wp-caption alignleft" style="width: 250px;">
<dt class="wp-caption-dt"><a href="http://www.flickr.com/photos/35034351734@N01/330227455"><img title="Google &quot;Testing on the Toilet&quot;" src="http://farm1.static.flickr.com/155/330227455_738b83c72e_m.jpg" alt="Google &quot;Testing on the Toilet&quot;" width="240" height="79" /></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image by <a href="http://www.flickr.com/photos/35034351734@N01/330227455">niallkennedy</a> via Flickr</dd>
</dl>
</div>
</div>
<p>Brandon Savage is writing a series on code improvement using a code example (starting with <a href="http://www.brandonsavage.net/peer-review-taking-code-and-making-it-better/">Peer Review: Taking Code And Making It Better</a>). In other words, it&#8217;s about <a class="zem_slink" title="Code refactoring" rel="wikipedia" href="http://en.wikipedia.org/wiki/Code_refactoring">refactoring</a>, which is practically my favorite subject. Although I don&#8217;t agree with all of it, it&#8217;s mostly good advice. I recommend it.</p>
<p>That said, I do have something important to add. The series is missing the first, most basic rule: Don&#8217;t refactor unless you have good automated test coverage (typically with <a class="zem_slink" title="Unit testing" rel="wikipedia" href="http://en.wikipedia.org/wiki/Unit_testing">unit tests</a>). And if there are no test, write them before you start refactoring. If you don&#8217;t, you&#8217;ll make mistakes and get lost in a frustrating bug search, unless you&#8217;re very stingy and don&#8217;t refactor too much. With test coverage, you have freedom to experiment, to change something and change it back if you don&#8217;t like the result. This, above all, is what makes refactoring such a great learning experience.</p>
<p>Some of those who have commented on the post <a href="http://www.brandonsavage.net/peer-review-looking-into-abstraction/">Peer Review: Looking Into Abstraction</a> (Greg Beaver, Jeff Carouth) mention testing and point out the need to inject objects so that they can be replaced with mock objects for testing. This is a wise move, and practically unavoidable in this case. To support refactoring, unit tests need to be run frequently, without accessing outside services (twitter or email in this case) that take time to process.</p>
<p>This means that in this case, it&#8217;s actually necessary to refactor a bit before proper unit tests can be implemented. This is what Michael Feathers calls the <em>Legacy Code Dilemma</em> in his book <a href="http://www.amazon.com/gp/product/0131177052?ie=UTF8&amp;tag=phinac-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0131177052">Working Effectively with Legacy Code</a><img class=" lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm" style="border: medium none  ! important; margin: 0px ! important;" src="http://www.assoc-amazon.com/e/ir?t=phinac-20&amp;l=as2&amp;o=1&amp;a=0131177052" border="0" alt="" width="1" height="1" />. Certainly building this dilemma into new code is not a good idea. Make the code unit-testable before it&#8217;s too late.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/20aff681-c4c1-8462-a15b-12d73157b0de/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_a.png?x-id=20aff681-c4c1-8462-a15b-12d73157b0de" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=Don%26%238217%3Bt%20refactor%20without%20unit%20tests&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2009%2F09%2F03%2Fdont-refactor-without-unit-tests%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2009/09/03/dont-refactor-without-unit-tests/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
