{"id":110,"date":"2010-10-25T14:25:00","date_gmt":"2010-10-25T13:25:00","guid":{"rendered":"https:\/\/pagan-gerbil.net\/?p=110"},"modified":"2010-10-25T14:25:00","modified_gmt":"2010-10-25T13:25:00","slug":"learning-unit-testing-ii-tdd-vs-traditional-design","status":"publish","type":"post","link":"https:\/\/www.pagan-gerbil.net\/?p=110","title":{"rendered":"Learning Unit Testing II &#8211; TDD vs. Traditional Design"},"content":{"rendered":"<p>As you can see, I\u2019ve only done a small amount of Test-Driven Development so far. I notice that for small classes \u2013 so far, there\u2019s only a very simple turn tracker \u2013 it\u2019s easy to write a test first when there\u2019s only very little code and no interaction with other classes. When things get a little more complex, will it still be so easy?<\/p>\n<p>I am wondering how people use TDD when they have big projects (as this will invariably become). Is it possible to stick to just writing code to pass tests all the way through to the end? Surely, at some point, major refactoring will need to be done when something comes right out of nowhere. I can see one example I\u2019ll run into at some point: in Necromunda, a model may go on \u2018Overwatch\u2019. This allows them to fire in their opponents turn, shooting at a target before it\u2019s move, after it\u2019s move or during it\u2019s move. That\u2019s probably going to require a major change to the Movement code (to account for the opponent taking an action), which could itself lead to a lot of refactoring. Should I try and design around this idea now, or wait until much, much later when I get to the Overwatch section to worry about it?<\/p>\n<p>Whether or not there is a simple solution to that particular problem will depend on many things \u2013 not least, the movement and shooting code that is written before it. I would traditionally sit down and try to work out exactly how to architecture the whole thing, taking into account things like shooting, overwatch, etc before I sit down to code. TDD seems to suggest that I write the test, then the code, and then eventually get around to a new test that \u2013 had I known \u2013 would have influenced the code I wrote earlier. I can see that would have some benefits (for example, the Bowling Game Kata shows the change in design to a simpler one due to TDD) but I don\u2019t know how likely it is that writing code just to pass the test will lead to a design path that isn\u2019t optimal given later functionality requirements.<\/p>\n<p>Maybe I am worrying about nothing, and the forced simplicity of design for TDD will ultimately lead to a good solution. Maybe I\u2019m missing the point entirely, and there <em>will<\/em> be major refactoring down the road, and that\u2019s a good thing. If that\u2019s true, isn\u2019t TDD going to end up taking a lot more time than is useful? Is there a secret, undocumented art to writing code following TDD where certain things are written first entirely to limit this sort of refactoring? If that\u2019s the case, I may be handicapping myself by slavishly following the rulebook.<\/p>\n<p>After reading the accepted answer on <a href=\"http:\/\/stackoverflow.com\/questions\/255509\/how-does-tdd-make-refactoring-easier\" target=\"_blank\" rel=\"noopener\">this StackOverflow question<\/a>,&nbsp; I\u2019ve decided to push ahead with my TDD experiment. I need to understand the beast. I\u2019ve got an idea of how certain things should be done, but we\u2019ll see if it makes sense once I\u2019ve laid the foundations using TDD. It\u2019s going to be an interesting way to do the work!<\/p>\n<p><em>This is a learning project I am documenting in order to teach myself TDD and unit testing \u2013 as I publish this, I have already written many parts in advance but I want to know what I\u2019m doing wrong! If you see improvements or corrections, either to the code or the process, please leave a comment and let me know! Introduction to this project can be found <a href=\"http:\/\/pagan-gerbil.net\/blog\/post.aspx?id=dfe72ad8-e4e9-48b4-ada3-6247abb5aa71\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you can see, I\u2019ve only done a small amount of Test-Driven Development so far. I notice that for small classes \u2013 so far, there\u2019s only a very simple turn tracker \u2013 it\u2019s easy to write a test first when there\u2019s only very little code and no interaction with other classes. When things get a &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.pagan-gerbil.net\/?p=110\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Learning Unit Testing II &#8211; TDD vs. Traditional Design&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[43,62,63,64],"series":[73],"_links":{"self":[{"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=\/wp\/v2\/posts\/110"}],"collection":[{"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=110"}],"version-history":[{"count":0,"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=110"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}