<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Derik Whittaker : Development, Featured, Agile</title><link>http://devlicio.us/blogs/derik_whittaker/archive/tags/Development/Featured/Agile/default.aspx</link><description>Tags: Development, Featured, Agile</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Why I believe IN and WRITE unit tests</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2007/10/01/why-i-believe-in-and-write-unit-tests.aspx</link><pubDate>Tue, 02 Oct 2007 00:27:25 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:38554</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=38554</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=38554</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2007/10/01/why-i-believe-in-and-write-unit-tests.aspx#comments</comments><description>&lt;p&gt;In my last post I think I struck a cord with some people in my post &amp;#39;Unit tests taking too much time&amp;#39;.&amp;nbsp; My intent was NOT to sound like an elitist Agilist or any else of that nature.&amp;nbsp; My intent was simply to put a post out there about the misperception (in my opinion) about how writing unit tests take too much time.&lt;/p&gt; &lt;p&gt;I thought I would put my &amp;#39;retort&amp;#39; on why I believe in&amp;nbsp;and write unit tests.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Allows&amp;nbsp;ME to write better code&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;I believe that when&amp;nbsp;I truly follow TDD practices&amp;nbsp;my code comes out simpler.&amp;nbsp; I have also found, that my code tends to follow&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/YAGNI" target="_blank"&gt;YAGNI&lt;/a&gt;&amp;nbsp;more so then when I don&amp;#39;t follow TDD practices.&lt;/p&gt; &lt;p&gt;Here is an example from my past experience.&amp;nbsp; On a prior project I needed to build a &amp;#39;search engine&amp;#39; into our app (this was not an actual search engine, more like a hunt path engine).&amp;nbsp; When I first started out writing code I started out by creating all these classes and logic that I thought I would need.&amp;nbsp; After about 3-4 hours of coding I was dead in the water.&amp;nbsp; I was not able to solve my problem in a simple manner and I was frustrated.&amp;nbsp; I decided to take a step back and start over.&amp;nbsp; &lt;/p&gt; &lt;p&gt;This time though I&amp;nbsp;started out by writing test to mimic what I actually needed.&amp;nbsp; I continued to follow the principles of &lt;a href="http://en.wikipedia.org/wiki/Test-driven_development" target="_blank"&gt;TDD&lt;/a&gt; (btw, TDD is more then just about writing tests), within an hour or so I had my solution pretty much good to go.&amp;nbsp; All those classes and methods I THOUGHT I would need I did NOT.&amp;nbsp; I was able to scrap about 50% of the code/classes that I thought I needed.&amp;nbsp; And guess what, now I have a test suit to test my new code.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Allows ME to find more bugs during the development process&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;It has been my experience that I find more bugs while writing tests.&amp;nbsp; This is due to the fact that I not only follow TDD, but I also do my best to implement the &lt;a href="http://en.wikipedia.org/wiki/Fail-fast" target="_blank"&gt;Fail Fast Principle&lt;/a&gt;.&amp;nbsp; When I am writing my tests I have the ability to create tests that easily walk though the different pathways of my code (I attempt to hit 100% code coverage as well).&amp;nbsp; If I were not writing tests I would have to do this via the application, and&amp;nbsp;this is much more painful and it less likely that I would actually do it.&lt;/p&gt; &lt;p&gt;By writing my tests and testing my code during the development process I know that I have reduced my bug count significantly.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Makes ME feel more comfortable making changes&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;This is the beauty of TDD, I am not afraid to make any critical changes to my code because I KNOW that I have tests to verify if I screwed anything up.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Have you ever run across some code that &amp;#39;you were scared&amp;#39; to change?&amp;nbsp; If you had tests for that code I bet you would a WHOLE LOT LESS scared to make any changes.&amp;nbsp; I know that I am.&lt;/p&gt; &lt;p&gt;A case in point for this is as follows.&amp;nbsp; On a prior project a consultant I was working with needed to make changes to some &amp;#39;smelly&amp;#39; logic.&amp;nbsp; This just happened to be a week or so before a scheduled release.&amp;nbsp; Beacuse he had NO confidence that the changes he would make would NOT break the application, he convinced the business to NOT make the changes. &amp;nbsp;I bet you a dollar that had there been unit tests he would have been much more willing to make the changes.&amp;nbsp; In this case the fact of NOT having tests may have caused the business to lose out on features it needed.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Improves MY code&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Because following TDD practices&amp;nbsp;forces you to write your failing code before your passing code,&amp;nbsp;I tend to implement my intent much cleaner and simpler.&amp;nbsp; I also know that because I am writing tests along the way my code is solid.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Allows ME to better convey intent&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;I know that tests are not a&amp;nbsp;substitution for&amp;nbsp;quality comments that convey intent, but they sure are the next best thing.&amp;nbsp; It is really nice to be able to point someone towards a suite of tests to &amp;#39;demo&amp;#39; how to use a component.&lt;/p&gt; &lt;p&gt;A case in point for this is as follows.&amp;nbsp; A few weeks ago at work I wrote an API to allow the querying of some data via our imaging system at work.&amp;nbsp; Turns out that an outside party needed to use this API for integration into on of our 3rd party applications.&amp;nbsp; When he came to me asking for &amp;#39;how do I use this&amp;#39;, I simply emailed him my suite of tests.&amp;nbsp; In this suite I had pretty much any conceivable use for the API.&amp;nbsp; To date the vendor has not asked me how to implement the API.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Allows MY tests to be automated and rerunable&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;This is a nice side effect of having tests.&amp;nbsp; Because we use&amp;nbsp;both NUnit and CruiseControl I can run my tests on every check in.&amp;nbsp; This is cool because now I (and the team) know if changes&amp;nbsp;I made&amp;nbsp;breaks some other part of the system.&amp;nbsp; I would rather know now then before QA hits it.&amp;nbsp; &lt;/p&gt; &lt;p&gt;The above are simply my opinion, if you don&amp;#39;t share the same opinions, let me know.&lt;/p&gt; &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=38554" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Development/default.aspx">Development</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Agile/default.aspx">Agile</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Methodology/default.aspx">Methodology</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/TDD/default.aspx">TDD</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Opinion/default.aspx">Opinion</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Featured/default.aspx">Featured</category></item></channel></rss>