{"id":40,"date":"2009-05-04T09:53:10","date_gmt":"2009-05-04T16:53:10","guid":{"rendered":"http:\/\/kai.mactane.org\/blog\/2009\/05\/04\/tdd-and-peace-of-mind\/"},"modified":"2009-05-04T09:53:10","modified_gmt":"2009-05-04T16:53:10","slug":"tdd-and-peace-of-mind","status":"publish","type":"post","link":"https:\/\/kagan.mactane.org\/blog\/2009\/05\/04\/tdd-and-peace-of-mind\/","title":{"rendered":"TDD and Peace of Mind"},"content":{"rendered":"<p>Let&#8217;s face it, we&#8217;re not perfect. As much as I might realize that automated testing is a good practice, it still <em>feels<\/em> like a chore sometimes. In my latest round of personal-project development, just setting up a decent set of test fixtures and a working test framework turned into something of a hassle, as it&#8217;s my first attempt both at Greasemonkey scripting <em>and<\/em> at building a script that will act on Livejournal pages. (Since LJ users can customize their views with any of 36 basic &#8220;styles&#8221;, this means quite a few fixtures.)<\/p>\n<p>So it was awfully tempting to say &#8220;screw this!&#8221; and just start writing code&nbsp;&mdash; you know, &#8220;code that works, code that gets stuff done&#8221;. Actual <em>program logic<\/em>, instead of testing tools. &#8220;Why don&#8217;t I just get something built to begin with,&#8221; I asked myself, &#8220;and then I can try to test that?&#8221;<\/p>\n<p>Ha, ha. Of course, we all know that starting off without any tests just makes it easier to continue without them later. So I took the virtuous road, forced myself to get tests working, but allowed myself to skimp by only setting up fixtures for 3 of the 36 styles. The other styles are something I will <em>have to<\/em> go back and fill in before I can release, so there&#8217;s no chance I&#8217;ll &#8220;give myself permission&#8221; to blow them off.<\/p>\n<p>When you really want to be writing code (because let&#8217;s face it, you consider yourself a coder, not a tester), it&#8217;s pretty annoying to write tests instead. But writing tests is at least a form of writing; if you&#8217;re not even writing tests, but rather setting up a test framework and fixtures, that&#8217;s almost excruciating.<\/p>\n<p>But it all became worth it the moment I made a change in one of my basic data structures. I had a structure (what a Perlist would call a hash of arrays) that held information about how to identify which LJ style the page uses. Then I realized I didn&#8217;t want to have a separate structure for how to manipulate the page; instead, all information about LJ&#8217;s styles and DOM structure should live in one place.<\/p>\n<p>So I altered the structure, then altered the functions created so far that rely on it. And that&#8217;s where I would then have to wonder, &#8220;How badly did I break everything?&#8221; Instead, I just ran my JSunit tests again. And seeing them pass was instant peace of mind. I don&#8217;t have to worry that there&#8217;s some hidden flaw waiting there, ready to be exposed by a user doing something unexpected. And as I add the other styles, I can easily be sure my code works for them, as well.<\/p>\n<p>I&#8217;m still fairly early in the development of this code, and while unit testing has cost me a bit of time, it&#8217;s given me back <em>peace of mind<\/em>. The time profit? I have no doubt that will come later. (And had I made any errors in my data-structure change, the unit tests would have helped me find them more quickly than the usual debugging methods.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s face it, we&#8217;re not perfect. As much as I might realize that automated testing is a good practice, it still feels like a chore sometimes. In my latest round of personal-project development, just setting up a decent set of test fixtures and a working test framework turned into something of a hassle, as it&#8217;s [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[37,7,38,6],"_links":{"self":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts\/40"}],"collection":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":0,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"wp:attachment":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}