<?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>www.hans-eric.com &#187; test-driven</title>
	<atom:link href="http://www.hans-eric.com/category/test-driven/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hans-eric.com</link>
	<description>Hans-Eric Grönlund on software development</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:32:29 +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>Did I say don&#8217;t unit-test GUIs?</title>
		<link>http://www.hans-eric.com/2008/01/03/did-i-say-dont-unit-test-guis/</link>
		<comments>http://www.hans-eric.com/2008/01/03/did-i-say-dont-unit-test-guis/#comments</comments>
		<pubDate>Thu, 03 Jan 2008 09:48:15 +0000</pubDate>
		<dc:creator>Hans-Eric</dc:creator>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[test-driven]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.hans-eric.com/2008/01/03/did-i-say-dont-unit-test-guis/</guid>
		<description><![CDATA[Isn’t life funny? Two weeks ago I stated my opinion that unit-testing graphical user interfaces isn’t worth the trouble. Now I find myself doing it, writing unit-tests for GUI components.
What happened, did I come to my senses or go crazy (pick the expression that fits your point of view) during Christmas holidays? No, I still [...]]]></description>
			<content:encoded><![CDATA[<p>Isn’t life funny? Two weeks ago I stated <a href="http://www.hans-eric.com/2007/12/20/dont-unit-test-gui/" title="Don't unit-test GUI">my opinion that unit-testing graphical user interfaces isn’t worth the trouble</a>. Now I find myself doing it, writing unit-tests for GUI components.</p>
<p>What happened, did I come to my senses or go crazy (pick the expression that fits your point of view) during Christmas holidays? No, I still think that unit-testing user interfaces is most likely a waste of time, but for some special occasions, like this one, they will pay off.</p>
<p>My task is to make changes to a tool box control in a legacy application. The control is full of application logic and it has a strong and complicated relationship to an edit area control. There are two things I need to do:<br />
First I have to pull out the application logic of the tool box and break the tight relationship to the edit area. Then I need to push that application logic deeper into the application core, so that the tool box could be used via the plug-in framework.</p>
<p>I figured I had two options. I could either <a href="http://en.wikipedia.org/wiki/Refactor" title="Wikipedia: Code Refactoring">refactor</a> the tool box and make the changes in small steps, or I could rebuild the complete tool box logic and then change the controls to use the new programming interface.<br />
I found the rebuild alternative too risky. The code base is full of booby traps and I have to be very careful not to introduce bugs. Therefore I decided to go with refactoring.</p>
<p>But refactoring requires a unit-testing harness, which of course this application doesn&#8217;t have. Trust me; you don’t want to refactor without extensive unit-testing, so here I am setting up the tests around the involved GUI controls. It’s a lot of work, especially since I don’t have a decent framework for creating <a href="http://en.wikipedia.org/wiki/Mock_object" title="Wikipedia: Mock object">mock objects</a>, but it’ll be worth the effort once I start messing around with the code.</p>
<p>As a conclusion I’d like to refine the “Don’t unit-test GUI” statement to read “Don’t unit-test GUI unless you’re refactoring it, and there’s no easier way to make sure your changes doesn’t break anything.”</p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hans-eric.com/2008/01/03/did-i-say-dont-unit-test-guis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t unit-test GUI</title>
		<link>http://www.hans-eric.com/2007/12/20/dont-unit-test-gui/</link>
		<comments>http://www.hans-eric.com/2007/12/20/dont-unit-test-gui/#comments</comments>
		<pubDate>Thu, 20 Dec 2007 11:24:10 +0000</pubDate>
		<dc:creator>Hans-Eric</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[test-driven]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.hans-eric.com/2007/12/20/dont-unit-test-gui/</guid>
		<description><![CDATA[I’m currently rereading parts of the book Test-Driven Development: A Practical Guide, by David Astels. It’s a great book in many ways, well worth reading, but I have objections to one particular section in the book.
The author tries to convince me that developing my user interfaces using a test-driven approach is a good thing to [...]]]></description>
			<content:encoded><![CDATA[<p>I’m currently rereading parts of the book <a href="http://www.amazon.com/gp/product/0131016490?ie=UTF8&amp;tag=wwwhansericco-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0131016490">Test-Driven Development: A Practical Guide</a><img src="http://www.assoc-amazon.com/e/ir?t=wwwhansericco-20&amp;l=as2&amp;o=1&amp;a=0131016490" style="border: medium none  ! important; margin: 0px ! important" border="0" height="1" width="1" />, by David Astels. It’s a great book in many ways, well worth reading, but I have objections to one particular section in the book.<br />
The author tries to convince me that developing my user interfaces using a test-driven approach is a good thing to do. I disagree.</p>
<p>I love <abbr title="Test-Driven Development">TDD</abbr>, it&#8217;s one of the most powerful development techniques I know, but it&#8217;s not without limitations. For one thing, code with unit-tests attached is more difficult to change. This is often an acceptable price to pay since the benefit of producing and having the unit-tests is usually greater.</p>
<p>But the return of the investment isn’t always bigger than the price, and sometimes the cost of change exceeds the benefit of protection. That’s why most developers won’t use TDD for experimental code, and that’s why I’m not using it to test my user interfaces.</p>
<p>I prefer to develop <abbr title="Graphical User Interfaces">GUIs</abbr> in a <a href="http://en.wikipedia.org/wiki/Rapid_application_development" title="Rapid Application Development">RAD</a> environment, visually dragging and dropping components, moving them around, exchanging them for others if better ones are to be found. In that process unit-testing just gets in my way. After all, the GUI is supposed to be a thin layer, without business logic, so there is only so much to test.</p>
<p>One could theoretically test that the form contains certain key components, that they are visible, have a certain position or layout, stuff like that &#8211; but I find that kind of testing too specific for my taste.</p>
<p>In my opinion, unit-testing should test functionality, not usability. It shouldn’t dictate whether I decide to show a set of items in a plain list or in a combo-box. What it should do, is test that the business logic of my code produce the correct set of items, and leave the graphical worries to the testers.</p>
<p>This brings us to something that is sometimes misunderstood: Unit-testing can never replace conventional testing. Some things are just better left to humans. Like testing user interfaces.</p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hans-eric.com/2007/12/20/dont-unit-test-gui/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Tools of The Effective Developer: Make It Work &#8211; First!</title>
		<link>http://www.hans-eric.com/2007/10/29/tools-of-the-effective-developer-make-it-work-first/</link>
		<comments>http://www.hans-eric.com/2007/10/29/tools-of-the-effective-developer-make-it-work-first/#comments</comments>
		<pubDate>Mon, 29 Oct 2007 17:19:20 +0000</pubDate>
		<dc:creator>Hans-Eric</dc:creator>
				<category><![CDATA[habits]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[test-driven]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.hans-eric.com/2007/10/29/tools-of-the-effective-developer-make-it-work-first/</guid>
		<description><![CDATA[I&#8217;ve come across many different types of developers during my nearly two decades in the business. In my experience there are two developer character type extremes: the ones that always seek and settle with the simplest solution, and the ones that seek the perfect solution, perfect in terms of efficiency, readability or code elegance.
Developers from [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve come across many different types of developers during my nearly two decades in the business. In my experience there are two developer character type extremes: the ones that always seek and settle with the simplest solution, and the ones that seek the perfect solution, perfect in terms of efficiency, readability or code elegance.</p>
<p>Developers from the first group constantly create mess and agony among fellow developers. The second group contain developers that never produce anything of value since they care more for the code than they do for the result. The optimal balance is somewhere in between, but regardless of what type of developer you are: you should always start by making it work, meaning implement the simplest solution first.</p>
<p>Why spend time on an implementation that isn&#8217;t likely to be the final one, you might ask. Here&#8217;s why:</p>
<ol>
<li>The simple solution helps evolving the <a href="http://en.wikipedia.org/wiki/Unit_testing">unit-testing</a> safety net.</li>
<li>The simple solution provide rapid feedback, and may prevent extensive coding of the wrong feature. It is like a prototype on the code level.</li>
<li>The simple solution is often good enough, and &#8211; with a working solution ready &#8211; you are less inclined to proceed and implement a more complex solution unless you really have to. Thus avoiding premature optimization and premature design, that makes you add features that <em>might</em> be needed in the future.</li>
<li>With the simple solution in place, most integration and programming interfacing is done. That makes it easier to implement a more complex solution.</li>
<li>While implementing the simple solution, your knowledge of the system increases. This helps you make good decisions later on.</li>
</ol>
<p>This may all sound simple enough to you. After all, the habit of Making It Work First comes naturally to many developers. Unfortunately, for me, I&#8217;m not one of those developers. I still let more or less insignificant design issues consume an unnecessary amount of time. The thing is, it is hard to find the perfect design on the first try. The perfect design may not even exist, or cost too much to be worth the effort.</p>
<p>That is why I struggle to attain the habit of Making It Work First.</p>
<p><em>Previous posts in the Tools of The Effective Developer series:</em></p>
<ol>
<li><a href="http://www.hans-eric.com/2007/08/31/tools-of-the-effective-developer-personal-logs/">Tools of The Effective Developer: Personal Logs</a></li>
<li><a href="http://www.hans-eric.com/2007/09/05/tools-of-the-effective-developer-personal-planning/">Tools of The Effective Developer: Personal Planning</a></li>
<li><a href="http://www.hans-eric.com/2007/09/25/tools-of-the-effective-developer-programming-by-intention/">Tools of The Effective Developer: Programming By Intention</a></li>
<li><a href="http://www.hans-eric.com/2007/09/27/tools-of-the-effective-developer-customer-view/">Tools of The Effective Developer: Customer View</a></li>
<li><a href="http://www.hans-eric.com/2007/10/02/tools-of-the-effective-developer-fail-fast/">Tools of The Effective Developer: Fail Fast!</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.hans-eric.com/2007/10/29/tools-of-the-effective-developer-make-it-work-first/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>I&#8217;m back!</title>
		<link>http://www.hans-eric.com/2007/10/28/im-back/</link>
		<comments>http://www.hans-eric.com/2007/10/28/im-back/#comments</comments>
		<pubDate>Sun, 28 Oct 2007 19:47:59 +0000</pubDate>
		<dc:creator>Hans-Eric</dc:creator>
				<category><![CDATA[D Programming Language]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[research]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[test-driven]]></category>

		<guid isPermaLink="false">http://www.hans-eric.com/2007/10/28/im-back/</guid>
		<description><![CDATA[I&#8217;m back from my three week vacation!
I had a great time, but as suspected I wasn&#8217;t able to stay away from computers. In the warm evenings, just for fun, I started to implement a ray tracer in the D Programming Language.
I have been looking for a suitable project that would give me a chance to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m back from my three week vacation!</p>
<p>I had a great time, but as suspected I wasn&#8217;t able to stay away from computers. In the warm evenings, just for fun, I started to implement a <a href="http://en.wikipedia.org/wiki/Ray_tracing" title="Ray Tracing on Wikipedia">ray tracer</a> in the <a href="http://www.digitalmars.com/d/">D Programming Language</a>.</p>
<p>I have been looking for a suitable project that would give me a chance to get deep into D, and a ray tracer seems to be the perfect fit. D is supposed to be great at floating point programming and now I have the chance to find out on my own.</p>
<p>To make it a little more interesting I have used a more top-down breath-first kind of approach than I normally do. I want to see how that affects a <a href="http://en.wikipedia.org/wiki/Test-driven_development">test-driven development technique</a>. As a part of the experiment I keep a detailed development log which I plan to share with you when I reach a certain point. It could be within a week or take several months depending on work load and inspiration level.</p>
<p>So stay tuned. I&#8217;ll be back with ray tracing, or other topics that comes across my sphere of interest.</p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hans-eric.com/2007/10/28/im-back/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to automate acceptance tests</title>
		<link>http://www.hans-eric.com/2007/10/05/how-to-automate-acceptance-tests/</link>
		<comments>http://www.hans-eric.com/2007/10/05/how-to-automate-acceptance-tests/#comments</comments>
		<pubDate>Fri, 05 Oct 2007 09:53:05 +0000</pubDate>
		<dc:creator>Hans-Eric</dc:creator>
				<category><![CDATA[software development]]></category>
		<category><![CDATA[test-driven]]></category>

		<guid isPermaLink="false">http://www.hans-eric.com/2007/10/05/how-to-automate-acceptance-tests/</guid>
		<description><![CDATA[In a comment to my previous post AC wonders how I automate acceptance testing. He considers that as being done by real testers. Well, he&#8217;s absolutely right. I expressed myself a bit sloppy, so let me use this post to explain what I meant to say.
Acceptance testing is done by the customer to make sure [...]]]></description>
			<content:encoded><![CDATA[<p>In a comment to <a href="http://www.hans-eric.com/2007/10/02/tools-of-the-effective-developer-fail-fast/" title="Tool of The Effective Developer: Fail Fast!">my previous post</a> AC wonders how I automate acceptance testing. He considers that as being done by real testers. Well, he&#8217;s absolutely right. I expressed myself a bit sloppy, so let me use this post to explain what I meant to say.</p>
<p><a href="http://en.wikipedia.org/wiki/Acceptance_test">Acceptance testing</a> is done <em>by the customer</em> to make sure she got what she ordered, to <em>make sure you delivered the right system</em>. This process cannot and should not be automated. What you can do &#8211; and this was what I meant to say &#8211; is to automate acceptance tests, and use them during construction.</p>
<p>The customer defines the acceptance tests. These are valuable to the developer since they can be used to validate the system as it develops. The sooner you get a hold on these test cases the better, so make sure you press the customer to produce them early. Better yet, help the customer in the process. That way you can help making the test cases automatable.</p>
<p>So, how do you design a test so that it can be automated? First of all you need to stop thinking in terms of user interface. Instead you should describe the test, in plain text, and in terms of action, function and result. Look for possible test data, edge cases, exceptional uses and describe those as well.</p>
<p>The second thing you need to do is to make the system in such a way that it can be automated. Separating the GUI from the business logic is often all you need to do to achieve that. You then automate the acceptance tests in the same way as you automate integration tests. In fact, they could even become a part of your integration testing harness. The only difference being the fact that they are defined by the customer.</p>
<p>I&#8217;m not saying this is easy. (<a href="http://blog.testcrew.com/?p=5" title="To Automate or Not to Automate: That is the Question...">Here&#8217;s a nice post which discusses when to automate</a>.) I&#8217;m not saying it can be done for all of your acceptance tests. What I say is: it can be done for far more tests than you might think. For example, we are customizing a <a href="http://en.wikipedia.org/wiki/Gis" title="Geographical Information System">GIS</a> system for an internal customer&#8217;s needs. The application is user controlled and involve plenty of complex actions, modifying graphical data and properties. Still we were able to automate most of the acceptance tests.</p>
<p>We spent a lot of time writing code to set up <a href="http://en.wikipedia.org/wiki/Test_fixture" title="Test Fixtures">fixtures</a>, initiate actions and check the results. It was worth every second though. You see, manually running one of our acceptance test cases usually takes several minutes.  By having the computer do all the work, time is reduced significantly and free up developer time. We use the automated test cases individually, almost like an extended compiler, to verify features as we implement them. And at night we run all of our acceptance tests to get feedback on how far we&#8217;ve come and to spot unexpected problems.</p>
<p>But the real acceptance testing is still going to be done manually, by the customer, at the end of the project. Just like AC pointed out.</p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hans-eric.com/2007/10/05/how-to-automate-acceptance-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tools of The Effective Developer: Fail Fast!</title>
		<link>http://www.hans-eric.com/2007/10/02/tools-of-the-effective-developer-fail-fast/</link>
		<comments>http://www.hans-eric.com/2007/10/02/tools-of-the-effective-developer-fail-fast/#comments</comments>
		<pubDate>Tue, 02 Oct 2007 21:13:11 +0000</pubDate>
		<dc:creator>Hans-Eric</dc:creator>
				<category><![CDATA[habits]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[test-driven]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.hans-eric.com/2007/10/02/tools-of-the-effective-developer-fail-fast/</guid>
		<description><![CDATA[It&#8217;s a well known fact that we regularly introduce errors with the code we write. Chances are slim to get it right on the first try. If we do, the risk is great that changing requirements and murdering deadlines will mess things up later on.
It&#8217;s also well known that the cost of failure increases with [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s a well known fact that we regularly introduce errors with the code we write. Chances are slim to get it right on the first try. If we do, the risk is great that changing requirements and murdering deadlines will mess things up later on.</p>
<p>It&#8217;s also well known that the cost of failure increases with time. The sooner you discover the flaw, the easier it is to fix. In other words, if we are going to fail, there are good reasons to do it fast.</p>
<p>When developers talk about failing fast they usually refer to <a href="http://martinfowler.com/ieeeSoftware/failFast.pdf" title="Fail Fast by Jim Shore (PDF)">the defensive coding technique that is based on assertions and exception handling</a>. It&#8217;s true that assertions are the very foundation of failing fast, they should be your first line of defense against bugs. But it doesn&#8217;t stop there. Failing fast should pervade your whole process of making software. You should fail fast on all levels.</p>
<p>The most effective fail fast-technique is automated testing, the fastest way to get feedback. Be sure to <a href="http://en.wikipedia.org/wiki/Test-driven_development" title="Test-Driven Development">write the tests first</a>. And don&#8217;t just automate <a href="http://en.wikipedia.org/wiki/Unit_testing">unit-testing</a>; <a href="http://en.wikipedia.org/wiki/Integration_testing" title="Integration Testing">integration</a> and <a href="http://en.wikipedia.org/wiki/Acceptance_test">acceptance testing</a> are often easier to automate than you might think. The key is to isolate your code using <a href="http://en.wikipedia.org/wiki/Mock_object">mock objects</a>.</p>
<p>The fail-fast property doesn&#8217;t apply to code and systems alone. It should be used on the project level too. By using agile practices like <a href="http://en.wikipedia.org/wiki/Extreme_programming" title="Extreme Programming">short iterations, small releases, and on-site customers</a> you create an environment of continuous feedback. It will help you steer the project to success, or &#8211; by failing fast &#8211; avoid a disaster. <a href="http://www.gregcons.com/KateBlog/default.aspx" title="Kate Gregory's Blog">Kate Gregory</a> puts it this way in <a href="http://www.gregcons.com/KateBlog/CommentView.aspx?guid=f9765002-18bf-4531-a3a3-01ca3afdf560">a nice post from 2005</a>:</p>
<blockquote><p>&#8220;Failure can be a good thing. If it saves you from following a doomed path for a year, you&#8217;re glad to have failed early rather than late.&#8221;</p></blockquote>
<p>Failing takes courage, failing increases your knowledge, failing calls upon action. That&#8217;s why I like the habit of Failing Fast.</p>
<p>This was the fifth post in this series. Here are the other Tools of The Effective Developer posts:</p>
<ol>
<li><a href="http://www.hans-eric.com/2007/08/31/tools-of-the-effective-developer-personal-logs/">Tools of The Effective Developer: Personal Logs</a></li>
<li><a href="http://www.hans-eric.com/2007/09/05/tools-of-the-effective-developer-personal-planning/">Tools of The Effective Developer: Personal Planning</a></li>
<li><a href="http://www.hans-eric.com/2007/09/25/tools-of-the-effective-developer-programming-by-intention/">Tools of The Effective Developer: Programming By Intention</a></li>
<li><a href="http://www.hans-eric.com/2007/09/27/tools-of-the-effective-developer-customer-view/">Tools of The Effective Developer: Customer View</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.hans-eric.com/2007/10/02/tools-of-the-effective-developer-fail-fast/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Quit Debugging!</title>
		<link>http://www.hans-eric.com/2007/09/17/quit-debugging/</link>
		<comments>http://www.hans-eric.com/2007/09/17/quit-debugging/#comments</comments>
		<pubDate>Mon, 17 Sep 2007 11:27:19 +0000</pubDate>
		<dc:creator>Hans-Eric</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[test-driven]]></category>
		<category><![CDATA[time-optimizing]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/17/quit-debugging/</guid>
		<description><![CDATA[I have a confession to make: I used to be addicted to debugging. Yes, it&#8217;s true. When I got hooked &#8211; damn you Delphi &#8211; I wasn&#8217;t able to see the dark side, the demonic side of the debugger. It lured me into thinking only quick fixes, and I lost track of the big picture. [...]]]></description>
			<content:encoded><![CDATA[<p>I have a confession to make: I used to be addicted to debugging. Yes, it&#8217;s true. When I got hooked &#8211; damn you <a href="http://en.wikipedia.org/wiki/Borland_Delphi" title="Borland Delphi">Delphi</a> &#8211; I wasn&#8217;t able to see the dark side, the demonic side of the debugger. It lured me into thinking only quick fixes, and I lost track of the big picture. Heed my warning: debuggers are bad!</p>
<p>Fortunately I&#8217;m one of the lucky few who have been able to recover from this particularly addictive behavior. I&#8217;ve been clean &#8211; thank you <a href="http://en.wikipedia.org/wiki/JUnit" title="The java unit-testing framework">jUnit</a> &#8211; for almost 5 years now. And you can do it too, you can let go of the safety zone that these integrated debuggers provide, and break free just like I did.</p>
<p>The first thing to do is to realize that there is a better alternative: <a href="http://en.wikipedia.org/wiki/Test-driven_development">test-driven development</a>. To get rid of a bug, the right thing to do is not to fire up your debugger, but to write a unit-test to reveal it. If necessary, keep writing tests and go deeper and deeper into your code. Eventually the tests will tell you what is wrong, and they&#8217;ll even point out a solution for you.</p>
<p>I know that using a debugger may seem like a faster way to find and extinguish a bug, but that is just an illusion. Here are the reasons:</p>
<ol>
<li>TDD improves the design. Being forced to think testability tends to divide your code into small manageable pieces. This will make your code a bad breeding ground for bugs.</li>
<li>Tests remain useful for a long time. They become an addition to your testing harness, which helps protecting your code against future infestations. The work spent in a debugging session can never be reused.</li>
<li>Unit-testing saves time, a lot!  While this isn&#8217;t immediately obvious, the long term effects are huge. Think of it: all those debugging sessions can be run automatically at your command, whenever you want, how often you want, and in just a matter of seconds. All you have to do to achieve this is to let go of the debugger, and write relevant tests.</li>
<li>Unit-testing gives you courage. There&#8217;s nothing like a good harness to make you feel invincible. I still remember the first time I felt the real power of unit-testing. I was working on a huge legacy application and had developed a new set of functionalities, using TDD for the very first time. Several months later I realized I had to do a major rewrite. The rewrite was risky business and took me a couple of days to complete. When finished, I ran the unit-tests which all came out green! I could be confident the program worked just as before the rewrite. And the best part: I drew the conclusion out of just five seconds of testing. Boy, I still get the goose bumps.</li>
</ol>
<p>[PREACHING OFF]</p>
<p>Of course debuggers are useful tools. In certain occasions they are even invaluable. For someone who&#8217;s new to the software they provide a great way of getting to know it. The problem is that a debugger tends to make you lazy. So be sure to get rid of it as soon as you identify a testing strategy.</p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hans-eric.com/2007/09/17/quit-debugging/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Agile low level programming in D</title>
		<link>http://www.hans-eric.com/2007/08/21/agile-low-level-programming-with-d/</link>
		<comments>http://www.hans-eric.com/2007/08/21/agile-low-level-programming-with-d/#comments</comments>
		<pubDate>Tue, 21 Aug 2007 14:53:46 +0000</pubDate>
		<dc:creator>Hans-Eric</dc:creator>
				<category><![CDATA[D Programming Language]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[test-driven]]></category>

		<guid isPermaLink="false">http://www.hans-eric.com/2007/08/21/agile-low-level-programming-with-d/</guid>
		<description><![CDATA[Agile software development techniques have long been utopia for low level system developers. The C programming language has been the most reasonable choice for implementing hardware near applications and drivers; But C was not designed with agility in mind. Hence methods like test driven development has been a pain to implement using C.
Now an alternative [...]]]></description>
			<content:encoded><![CDATA[<p>Agile software development techniques have long been utopia for low level system developers. The C programming language has been the most reasonable choice for implementing hardware near applications and drivers; But C was not designed with agility in mind. Hence methods like <a href="http://en.wikipedia.org/wiki/Test-driven_development">test driven development</a> has been a pain to implement using C.</p>
<p>Now an alternative exists: <a href="http://www.digitalmars.com/d/">The D Programming Language</a> is designed to combine the low level programming abilities and performance of languages like C and C++, with the productivity features of modern languages.</p>
<p>You could describe D as C with features like object oriented programming, exception handling, garbage collection and design by contract. Cool agile features indeed, but D has another one that I instantly fell in love with: built in support for unit testing.</p>
<p>In D you can declare unittest blocks.</p>
<pre>unittest {
  assert(sometest, "sometest failed");
}</pre>
<p>The unittest construct makes it very easy to practice test-driven development. For instance, lets say we want to create a new type for  three dimensional vectors. In order to be test-driven we need to start with the test:</p>
<pre>unittest {
  Vector3 v;
}</pre>
<p>We compile the program using the -unittest switch.</p>
<pre>dmd -unittest test.d</pre>
<p>Of course we get a compiler error, Vector3 is still undefined. Lets define it.</p>
<pre>struct Vector3 {
}</pre>
<p>This time the program compiles. Back to the unittest. Now let&#8217;s try a simple assignment.</p>
<pre>unittest {
  Vector3 v = Vector3(1.0, 2.0, 3.0);
}</pre>
<p>This, again, produces a compile time error. Vector3 doesn&#8217;t have the x, y and z fields, so we implement them.</p>
<pre>struct Vector3 {
  real x, y, z;
}</pre>
<p>The code passes compilation. Next step: implement vector addition. We start with the test.</p>
<pre>unittest {
  Vector3 v1 = Vector3(1, 2, 3);
  Vector3 v2 = Vector3(3, 2, 1);
  Vector3 vr = v1 + v2;
}</pre>
<p>As we expect, the code doesn&#8217;t compile. We need to overload the + operator.</p>
<pre>struct Vector3 {
  real x, y, z;

  // Overload + operator
  Vector3 opAdd(Vector3 a)
  {
    return Vector3(0, 0, 0);
  }
}</pre>
<p>Now the program compiles, but we don&#8217;t know if the add operator produces the right result (which it doesn&#8217;t with the current code). To check the result we can use assert.</p>
<pre>unittest {
  Vector3 v1 = Vector3(1, 2, 3);
  Vector3 v2 = Vector3(3, 2, 1);
  Vector3 vr = v1 + v2;

  assert(vr.x == 4);
  assert(vr.y == 4);
  assert(vr.z == 4);
}</pre>
<p>We compile, and it compiles without error. To run the unittest code we simply run the program. Unittest code is executed after program initialization, but before the main function. If a unittest fails the program terminates prematurely. Our program terminates (as expected) with  an AssertError. Lets correct the add operator.</p>
<pre>struct Vector3 {
  real x, y, z;

  Vector3 opAdd(Vector3 a)
  {
    return Vector3(x + a.x, y + a.y, z + a.z);
  }
}</pre>
<p>It compiles and runs without errors. Great!</p>
<p>As you can see, test-driven development is a very smooth and simple process in D. There is no need for a separate framework, just compile and run your application. Also, the test code dwells close to the production code, which makes it easier to maintain and keep up-to-date. In fact, you can put unittest blocks anywhere in your code, even within the piece of code you are testing.</p>
<pre>struct Vector3 {
  real x, y, z;

  // unittest blocks are allowed
  // within structs.
  unittest { ... }
}</pre>
<p>Any type of code is allowed within a unittest block. This means that you can declare functions to do repetitive tasks.</p>
<pre>unittest {
  function assertEqual(Vector3 a, Vector3 b)
  {
    assert(a.x == b.x);
    assert(a.y == b.y);
    assert(a.z == b.z);
  }

  Vector3 v1 = Vector3(1, 2, 3);
  Vector3 v2 = Vector3(3, 2, 1);

  assertEqual(v1 + v2, Vector3(4, 4, 4));
  assertEqual(v1 - v2, Vector3(2, 0, -2));
}</pre>
<p>The test code is only included in the executable if it is compiled with the unittest flag, so there&#8217;s no need for a separate test project or conditional compilation. This is a very clean solution, highly suitable for a traditional language that compiles directly to machine code. Although I&#8217;m a big fan of <a href="http://en.wikipedia.org/wiki/XUnit">testing framworks</a> such as <a href="http://en.wikipedia.org/wiki/Junit">JUnit</a>, I find it much easier to work with the built in unit testing features of D. Of course you don&#8217;t have advanced features like <a href="http://en.wikipedia.org/wiki/Mock_object">mock object</a> support, but I guess that will be offered soon with some kind of unittest-based framework add-on.</p>
<p>If you have doubts about the foundation of the D Programming Language, you should be relieved to hear that it&#8217;s been designed by <a href="http://www.walterbright.com/">Walter Bright</a>, who have spent almost a lifetime constructing C and C++ compilers.</p>
<p>You&#8217;ll find the <a href="http://www.hans-eric.com/code-samples/unit-testing-in-d/">complete code</a> within <a href="http://www.hans-eric.com/code-samples">my code sample pages</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hans-eric.com/2007/08/21/agile-low-level-programming-with-d/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.523 seconds -->
