<?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>Push cx &#187; TDD</title>
	<atom:link href="http://push.cx/tag/tdd/feed" rel="self" type="application/rss+xml" />
	<link>http://push.cx</link>
	<description>A traveling geek&#039;s blog on development, games, and the web</description>
	<lastBuildDate>Fri, 14 Oct 2011 10:24:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>You&#8217;re Not Refactoring</title>
		<link>http://push.cx/2009/youre-not-refactoring</link>
		<comments>http://push.cx/2009/youre-not-refactoring#comments</comments>
		<pubDate>Sat, 10 Jan 2009 22:43:16 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Extreme Programming]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://push.cx/?p=400</guid>
		<description><![CDATA[Changing code is a great way to break it, especially in really subtle ways that you won&#8217;t pick up on for weeks or months. The maintainer of Unangband, Andrew Doull, wrote that Refactoring Is Hell. I sent him a note in response to that blog post that I think I may just as well have [...]]]></description>
			<content:encoded><![CDATA[<p>
Changing code is a great way to break it, especially in really subtle ways that you won&#8217;t pick up on for weeks or months. The maintainer of <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VuYW5nYmFuZC5ibG9nc3BvdC5jb20v">Unangband</a>, Andrew Doull, wrote that <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3JvZ3VlbGlrZWRldmVsb3Blci5ibG9nc3BvdC5jb20vMjAwOC8xMi9yZWZhY3RvcmluZy1oZWxsLmh0bWw=">Refactoring Is Hell</a>. I sent him a note in response to that blog post that I think I may just as well have blogged. So:
</p>

<blockquote>
<p>
I wanted to write to say you&#8217;re not refactoring. The term refactoring
was created in the context of extreme programming [er, I should have said "popularized"] and it was defined as
reworking code that&#8217;s under test. Without the tests, you&#8217;re just
rewriting, working without a net. That&#8217;s why your code ends up buggier.
</p>

<p>
I don&#8217;t write to be pedantic. (Well, OK, maybe a little. I am a
programmer, after all.) I write because I also used to wonder what all
the fuss was about &#8216;refactoring&#8217; when it really just seemed like a
dangerous bunch of nonsense that left my code with new bugs. I didn&#8217;t do
any automated testing at the time and didn&#8217;t realize how valuable it can
be.
</p>

<p>
I still haven&#8217;t drunk the XP kool-aid, but having a good test suite
means I don&#8217;t code in fear that I&#8217;ve broken things, or feel awful when a
bug slips into production because I didn&#8217;t remember to hand-test some
weird situation. I&#8217;ll even write tests on the personal projects I&#8217;m just
hacking away privately at &#8212; yeah, I probably will just do a little unit
testing instead of a really comprehensive, robust system, but even a bit
helps.
</p>
</blockquote>

<p>
He responded that a game like Unangband is especially hard to test because it has so many moving parts and much of the gameplay is generated. It does have a very high-level test, an automated player. I wrote back:
</p>

<blockquote>
<p>
I don&#8217;t really think anything is too complex for test cases. You have a
&#8216;pick up&#8217; command that, if the player is standing on an object and has
inventory space, removes it from the world and puts it in their
inventory. There&#8217;s a fair amount of data structure you have to mock
(which is not the easiest thing in the world in C), but you can know the
basic command works even because you test all the really weird corner
cases about when the player is polymorphed into a creature without hands
or something. I&#8217;d bet as simple as the command usually is, you&#8217;ve broken
&#8216;pick up&#8217; at least a few times. It&#8217;s a safety net you build one strand
at a time, like a spider web. You may still be able to fall through
(especially at first), but sometimes you&#8217;ll catch a strand and be glad
you built it for yourself.
</p>

<p>
The other part of complexity is that thinking about testability changes
how you structure your programs. You force yourself to decouple
different parts of code because you want to be able to test them in
isolation from each other &#8212; that this improves your overall design is a
nice side-effect. That you can spend less time worrying about or kicking
yourself for breaking something &#8216;so obvious!&#8217; is the real one.
</p>

<p>
You may enjoy the book <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5hbWF6b24uY29tL2RwLzAyMDE0ODU2NzIvP3RhZz1wdXNoY3gtMjA=">Refactoring: Improving the Design of Existing
Code</a>, it&#8217;s the classic in the space. Though my money&#8217;s on the later book
<a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5hbWF6b24uY29tL2RwLzAxMzExNzcwNTIvP3RhZz1wdXNoY3gtMjA=">Working Effectively with Legacy Code</a>, which is a couple hundred pages
on how to go from cursing your predecessors to a reliable codebase.
Saved my bacon at a previous job. I know Unangband is your hobby rather
than your job, but I suggest them because they&#8217;ve helped keep my hobby
code more fun than frustrating.
</p>
</blockquote>

<p>
Part of what keeps developers from testing more is <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2FuYW5lbHNvbi5jb20vYmxvZy8yMDA4LzA4L2EtcmFpbHMtYXBwbGljYXRpb24tZmFjdG9yeS8=">friction</a>. You have to pick a test suite, read the docs, learn the API, blow an hour or few chasing a bug because you misunderstood something, figure out the philosophy of it, rewrite your tests so they&#8217;re clear and paradigmatic&#8230;
</p>



<p>
The friction seems to be a lot higher in C/C++ than in dynamic languages. There are good technical reasons for it, but I think at least part of it is cultural. I&#8217;ve noticed a less testing in the Python community than Ruby, and I think that&#8217;s just because Rails was big on testing and has driven so much of the interest in Ruby. The community assumption is that software includes tests.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=400" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2009/youre-not-refactoring/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Painless Upgrade to Rails 2.0</title>
		<link>http://push.cx/2007/painless-upgrade-to-rails-20</link>
		<comments>http://push.cx/2007/painless-upgrade-to-rails-20#comments</comments>
		<pubDate>Thu, 13 Dec 2007 12:10:20 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[ActiveResource]]></category>
		<category><![CDATA[NearbyGamers]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Rails 2.0]]></category>
		<category><![CDATA[routing]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[upgrade]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://push.cx/2007/painless-upgrade-to-rails-20</guid>
		<description><![CDATA[I spent a dead-easy 2.5 hours last night updating NearbyGamers to Rails 2.0. My svn commit message read (with links added here for convenience): Updated to Rails 2.0.1 rm&#8217;d lib/slash_urls.rb: Rails switched from ; to / to separate actions rm&#8217;d lib/resource_requirements.rb: now included in ActiveResources rm&#8217;d app/helpers/tags_helper.rb and gamers_helper.rb that had old controller names and [...]]]></description>
			<content:encoded><![CDATA[<p>
I spent a dead-easy 2.5 hours last night updating <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL05lYXJieUdhbWVycy5jb20=">NearbyGamers</a> to Rails 2.0. My svn commit message read (with links added here for convenience):
</p>

<blockquote>
Updated to Rails 2.0.1

<ul>
<li>rm&#8217;d <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNy9yYWlscy1zZW1pY29sb25zLW91dC1zbGFzaGVzLWlu">lib/slash_urls.rb</a>: Rails switched from ; to / to separate actions</li>
<li>rm&#8217;d <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2Rldi5ydWJ5b25yYWlscy5vcmcvdGlja2V0Lzc2MzMgd2l0aA0KCmh0dHA6Ly9wdXNoLmN4LzIwMDcvcmFpbHMtc2VtaWNvbG9ucy1vdXQtc2xhc2hlcy1pbgoK">lib/resource_requirements.rb</a>: now included in ActiveResources</li>
<li>rm&#8217;d app/helpers/tags_helper.rb and gamers_helper.rb that had old controller names and were unused anyways</li>
<li>app/controllers/gamers_controller.rb: documented and fixed raw post variable extraction</li><li>updated post_path linkers in many views, controllers, and tests</li>
<li>renamed discussion_anchor_post_path to anchor_discussion_post_path to match the new ordering for nested resource urls</li>
<li>fix tag sorting on <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL05lYXJieUdhbWVycy5jb20vdGFncw==">tag index</a></li>
<li>move tags from /tags/Board+Games to <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL05lYXJieUdhbWVycy5jb20vdGFncy9Cb2FyZF9HYW1lcw==">/tags/Board_Games</a>, as Rails fixed the bug that encoded &#8216; &#8216; as &#8216;+&#8217; in non-get variable parts of the URL</li>
</ul>
</blockquote>

<h2>The Details</h2>

<p>
The first two files I deleted were my patches to URL generation that Rails now includes. The next two helper files I never used, but had had old (singular noun) names.
</p>

<p>
The bit about documentation: the way to get at raw post variables changed, so I tweaked code and left myself a reminder of why I&#8217;m doing it (I had to look at the changelog, which means it wasn&#8217;t self-evident).
</p>

<p>
Nested resources (I have nested <kbd>map.resources :discussions { |d| d.resources :posts }</kbd>) changed from <kbd>discussion_edit_post_url</kbd> to <kbd>edit_discussion_post_url</kbd>, so there was a bunch of places to change that. The next change about anchor is me following this convention with my own convenience method.
</p>

<p>
Tag sorting on the index page was a bug that Snarky pointed out to me. When I made some performance tweaks to that page I forgot to keep sorting tags as they came out of the database. This arguably should&#8217;ve been a revision of its own as it&#8217;s unrelated to the upgrade, but it was an easy one-line bugfix so I let it in.
</p>

<p>
Last, I renamed the tag pages. The tag model&#8217;s <kbd>to_param</kbd> just returns <kbd>self.name</kbd> and Rails would turn &#8220;Board Games&#8221; into &#8220;Board+Games&#8221;. It&#8217;s common but technically incorrect, as + only means space in encoded GET/POST variables. The proper encoding of &#8220;Board%20Games&#8221; was really noisy, so I took a page from Wikipedia&#8217;s pagebook and use underscores instead, like &#8220;Board_Games&#8221;. Was one line in the <kbd>to_param</kbd> and one line in the controller&#8217;s <kbd>load_tag</kbd> before_filter, a dozen lines of tweaking old tests, and seven lines of new tests. Eeeeeasy. 
</p>

<h2>The Verdict</h2>

<p>
Tests, tests, tests. If I didn&#8217;t have a solid test suite, I&#8217;d be noticing bugs two months from now in the least-frequently-exercised bits of code. Not only would I not have remembered to test some of the functionality, I wouldn&#8217;t have remembered the corner cases &#8212; or maybe I&#8217;d just have gotten bored of clicking through pages over and over and ignored the corner cases.
</p>

<p>
None of these were hard fixes and none were frustrating. I&#8217;d probably have finished even sooner if I hadn&#8217;t also been poking <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2ljYW5oYXNjaGVlemJ1cmdlci5jb20vMjAwNy8xMi8xMS9taXRvc2lzLw==">lolcats</a> and chatting with friends. And now I have a big list of shiny new Rails 2.0 features I can put to work in <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL05lYXJieUdhbWVycy5jb20=">NearbyGamers</a>.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=292" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2007/painless-upgrade-to-rails-20/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drifting Into Test-Driven Development</title>
		<link>http://push.cx/2007/drifting-into-test-driven-development</link>
		<comments>http://push.cx/2007/drifting-into-test-driven-development#comments</comments>
		<pubDate>Fri, 07 Dec 2007 12:19:41 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://push.cx/2007/drifting-into-test-driven-development</guid>
		<description><![CDATA[About two years ago I first read about Test-Driven Development on the c2 wiki. It&#8217;s a simple plan: before you write code, write the tests that will exercise it. My thoughts: this is exactly backwards. But I really liked the pages I read about automated testing and started putting that in practice almost immediately. Rather [...]]]></description>
			<content:encoded><![CDATA[<p>
About two years ago I first read about <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2MyLmNvbS9jZ2kvd2lraT9UZXN0RHJpdmVuRGV2ZWxvcG1lbnQ=">Test-Driven Development on the c2 wiki</a>. It&#8217;s a simple plan: before you write code, write the tests that will exercise it.                                                                                                  
</p>

<p>
My thoughts: this is exactly backwards.
</p>

<p>                                                                                                                                
But I really liked the pages I read about <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2MyLmNvbS9jZ2kvd2lraT9BdXRvbWF0ZWRUZXN0cw==">automated testing</a> and started putting that in practice almost immediately. Rather than sitting there and hitting reload and filling in forms and clicking buttons and waiting for pages and forgetting to check what happens when you leave half the form blank and&#8230; well, rather than all that error-prone tedium, I wrote code to test my code. And that&#8217;s great. It saves me from a lot of &#8220;oh yeah, I forgot that thing over there called this code&#8221; bugs.
</p>

<p>
I kept hearing about TDD, though. Books would mention it, blogs would tout it it. I guess it&#8217;s squirmed its way into my habits.
</p>

<p>
In the last few months of coding on <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL05lYXJieUdhbWVycy5jb20=">NearbyGamers</a> I&#8217;d hack at the models a bit, write some tests for them, and hey, as long as I&#8217;m writing tests, add some tests for the controller code I was about to build on top of that. And then maybe, after I implemented the basic feature, I&#8217;d write a test for an improvement and then tweak that code. In the last two weeks I&#8217;ve been writing code for the messages and forums and I just recognized that my tests have snuck in front of my code almost every time.
</p>

<p>
And it doesn&#8217;t feel backwards anymore. It just makes sense to think about my interface first, and to spend a minute pondering what sort of devious corner cases I can throw at it. I&#8217;m writing more tests, letting fewer bugs into production, and having more fun. I`ve broken the &#8220;Ehhh&#8230; it works, I&#8217;ll check it in and write some tests tomorrow&#8221; habit.
</p>

<p>
So this is my little bit of cheerleading for Test-Driven Development, a little more exposure for your unconscious mind to chew on.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=290" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2007/drifting-into-test-driven-development/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mocha</title>
		<link>http://push.cx/2007/mocha</link>
		<comments>http://push.cx/2007/mocha#comments</comments>
		<pubDate>Sat, 18 Aug 2007 19:41:31 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Mocha]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>

		<guid isPermaLink="false">http://push.cx/2007/mocha</guid>
		<description><![CDATA[I posted yesterday that I was dropping my own little custom mocking code and considering Mocha. A few hours later I made a pass at it and was blown away. It took very little time to convert my ~600 lines of tests to use Mocha and I spent most of that time just cleaning out [...]]]></description>
			<content:encoded><![CDATA[<p>
I <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNy9zaW1wbGUtcnVieS1tb2NraW5n">posted yesterday</a> that I was dropping my own little custom mocking code and considering Mocha. A few hours later I made a pass at it and was blown away. It took very little time to convert my ~600 lines of tests to use Mocha and I spent most of that time just cleaning out the cruft that my mocking code had engendered.
</p>

<p>
I&#8217;m now a big fan of Mocha like I am <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNy9yYWlscy0xMjEtaW1wcmVzc2lvbg==">of Rails</a>. Ruby has a way of allowing coders to write magically polished libraries.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=254" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2007/mocha/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

