<?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; Refactoring</title>
	<atom:link href="http://blog.agilephp.com/tag/refactoring/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>
		<item>
		<title>Most confused discussion in the known universe</title>
		<link>http://blog.agilephp.com/2009/05/02/most-confused-discussion-in-the-known-universe/</link>
		<comments>http://blog.agilephp.com/2009/05/02/most-confused-discussion-in-the-known-universe/#comments</comments>
		<pubDate>Sat, 02 May 2009 20:03:12 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Source code]]></category>

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



Image by B Tal via Flickr



How confused can a discussion get? As confused as the discussion in the comments to Benjamin Eberlei&#8217;s Explicit Code requires no comments &#8211; Only bad code does. This discussion has a fake identity, and nobody seems to notice. As you can see, the blog post claims to be about code [...]]]></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/68634595@N00/163450213"><img title="If You're Not Confused" src="http://farm1.static.flickr.com/73/163450213_18478d3aa6_m.jpg" alt="If You're Not Confused" width="240" height="160" /></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image by <a href="http://www.flickr.com/photos/68634595@N00/163450213">B Tal</a> via Flickr</dd>
</dl>
</div>
</div>
<p>How confused can a discussion get? As confused as the discussion in the comments to Benjamin Eberlei&#8217;s <a href="http://www.whitewashing.de/blog/articles/116">Explicit Code requires no comments &#8211; Only bad code does</a>. This discussion has a fake identity, and nobody seems to notice<strong>.</strong> As you can see, the blog post claims to be about code comments, but it isn&#8217;t. The example given does not adress the issue of commenting. In the refactored version, Benjamin has not removed the comment from the original, and done nothing (OK, a tiny bit) to replace it with expressive code.  Since the example is irrelevant to the subject matter, it fails to keep the discussion grounded, allowing it to degenerate into dogmatic opinion and subjective speculation.</p>
<p>Also contributing to the confusion is an apparent lack of understanding of the process of refactoring. Refactoring is not a deterministic process, nor is it a strait jacket. You have choice, and you should learn to exercise it judiciously. You take a chunk of code, change it somewhat, and then you decide whether there&#8217;s actually been an improvement. If there hasn&#8217;t, as many think in this case (and I&#8217;m somewhat inclined to agree with them, although Benjamin has great principles and valid points), you have three choices:</p>
<ol>
<li>Refactor further, hoping to arrive at something more satisfactory.</li>
<li>Undo the change and try something else.</li>
<li>Undo the change and keep the original version if you think that&#8217;s the best you can do.</li>
</ol>
<p>These are the normal choices for those of us who refactor routinely. It happens often enough. Even if you undo the change, it doesn&#8217;t necessarily mean you&#8217;ve wasted your time. You&#8217;ve probably learned something.</p>
<p>In this case, too, there is a chance to learn something worthwhile if you can distance yourself from the noisy confrontation. The most obvious thing to observe is that trying to extract methods from a 6-7 line method is a suicide mission in the circumstances. It&#8217;s bound to increase the volume of code <em>a lot</em>, and it&#8217;s only natural that the result gets labeled &#8220;bloated&#8221;. If you do something similar with a longer method, the percentage increase is much less, and you have a chance of not drowning in irrelevant criticism.</p>
<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=Most%20confused%20discussion%20in%20the%20known%20universe&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2009%2F05%2F02%2Fmost-confused-discussion-in-the-known-universe%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/05/02/most-confused-discussion-in-the-known-universe/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Show me your code comments and I&#8217;ll show why you don&#8217;t need them</title>
		<link>http://blog.agilephp.com/2009/04/30/show-me-your-code-comments-and-ill-show-why-you-dont-need-them/</link>
		<comments>http://blog.agilephp.com/2009/04/30/show-me-your-code-comments-and-ill-show-why-you-dont-need-them/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 20:52:45 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Martin Fowler]]></category>
		<category><![CDATA[Technical debt]]></category>
		<category><![CDATA[Ward Cunningham]]></category>

		<guid isPermaLink="false">http://blog.agilephp.com/?p=1567</guid>
		<description><![CDATA[Brandon Savage has written a blog post On Code Commenting And Technical Debt. He believes that code comments are a good way to minimize technical debt.

I&#8217;m surprised to find the term technical debt mentioned without being accompanied by the term refactoring. Refactoring is generally recognized (outside the PHP world) as the way to pay down [...]]]></description>
			<content:encoded><![CDATA[<p>Brandon Savage has written a blog post <a href="http://www.brandonsavage.net/on-code-commenting-and-technical-debt/">On Code Commenting And Technical Debt</a>. He believes that code comments are a good way to minimize <a class="zem_slink" title="Technical debt" rel="wikipedia" href="http://en.wikipedia.org/wiki/Technical_debt">technical debt</a>.<br />
<a href="http://www.brandonsavage.net/on-code-commenting-and-technical-debt/"></a><br />
I&#8217;m surprised to find the term technical debt mentioned without being accompanied by the term <a class="zem_slink" title="Code refactoring" rel="wikipedia" href="http://en.wikipedia.org/wiki/Code_refactoring">refactoring</a>. Refactoring is generally recognized (outside the PHP world) as <em>the</em> way to pay down technical debt. Commenting may help, but is clearly the second-best practice.</p>
<p><a href="http://martinfowler.com/bliki/TechnicalDebt.html">Martin Fowler</a> puts it this way:</p>
<blockquote><p>Technical Debt is a wonderful metaphor developed by <a class="zem_slink" title="Ward Cunningham" rel="wikipedia" href="http://en.wikipedia.org/wiki/Ward_Cunningham">Ward Cunningham</a> to help us think about this problem. In this metaphor, doing things the quick and dirty way sets us up with a technical debt, which is similar to a financial debt. Like a financial debt, the technical debt incurs interest payments, which come in the form of the extra effort that we have to do in future development because of the quick and dirty design choice. We can choose to continue paying the interest, or we can pay down the principal by refactoring the quick and dirty design into the better design. Although it costs to pay down the principal, we gain by reduced interest payments in the future.</p></blockquote>
<p>Brandon&#8217;s argument in favor of commenting is perfectly valid, but misses the crux of the matter, since he ignores the option of actually improving the code itself rather than just adding comments.</p>
<p>Let me also comment briefly on <a href="http://mtabini.blogspot.com/2009/04/myth-of-myth-of-self-commenting-code.html">Marco Tabini&#8217;s reponse</a>:</p>
<blockquote><p>What I suspect Brandon really means is that the comments are there to illustrate the intentions of the author <span style="font-style: italic;">when those intentions are not immediately made obvious by the code itself</span>.</p></blockquote>
<p>Yes. And no. There is no absolute boundary, no limit in principle, to how <a href="http://c2.com/cgi/wiki/wiki?IntentionRevealingNames">intention-revealing</a> code can be. It&#8217;s not necessarily easy in practice, though. As I&#8217;ve said before, it&#8217;s primarily inline comments that I&#8217;m objecting to. The comments I feel a need to write are often at the class level and address the interaction between different classes.</p>
<p>Anyway, arguing about it theoretically is not the way to resolve the issue. Show me some good examples of comments that serve to make code clearer and that supposedly can&#8217;t be usefully eliminated by refactoring the code into something more readable. I&#8217;ll either admit that you&#8217;re right or show you (or at least outline) how to do it differently. I do recognize that even inline comments are useful&#8230;<em>occasionally</em>.<a href="http://mtabini.blogspot.com/2009/04/myth-of-myth-of-self-commenting-code.html"></a></p>
<p>(By the way, I seem to have missed Brandon&#8217;s comment (<a href="http://www.brandonsavage.net/where-comments-are-useful/">Where Comments Are Useful</a>) to my <a href="http://blog.agilephp.com/2008/12/23/comments-considered-harmful/">comments considered harmful</a> post last December.)</p>
<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=Show%20me%20your%20code%20comments%20and%20I%26%238217%3Bll%20show%20why%20you%20don%26%238217%3Bt%20need%20them&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2009%2F04%2F30%2Fshow-me-your-code-comments-and-ill-show-why-you-dont-need-them%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/04/30/show-me-your-code-comments-and-ill-show-why-you-dont-need-them/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>
