I just finished reading Kent Beck’s Test-Driven Development, and, while I enjoyed the examples in the first half of the book, I’m disappointed about the lack of discussion on dealing with “legacy” code.
In my short amount of time using Test-Driven Development (TDD), I have seen that it can be a great, confidence building tool; and it has enabled the Entertainer devs to iron out some major issues in the code base (e.g., video thumbnailing was done entirely by Paul by following the TDD principles). However, I have struggled with retroactively applying TDD and unit testing to preexisting code.
I have literally lost sleep thinking about how to isolate tests and break coupling of existing classes. My hope was that Beck’s book would provide some answers. Instead, I got “There is a whole book (or books) to be written about switching to TDD when you have lots of code. What follows is necessarily only a teaser.” His “teaser” basically stated that there is a Catch-22 of “refactoring would result in errors because there aren’t tests, and tests can’t be written because the code isn’t refactored for it” (paraphrased). And his advice is essentially to try your best to break it. The entire subject gets about a page in the book. Thanks Kent.
Would I recommend reading Beck’s Test-Driven Development? Sure. It’s a good introductory read to understanding the core principles of “test-first” unit testing. The first half of the book offers some great, relatively simple examples that most developers should have no trouble following. The second half does disappoint, however, as it seems more like half-baked refactoring advice instead of tackling really challenging problems that could be addressed (like what to do with preexisting code or GUIs).
I’ll be reading Fowler and Beck’s Refactoring: Improving the Design of Existing Code next to see if I can get some answers to the questions that I desperately want; even if that advice is set outside of the unit testing/TDD mentality.
3 comments
Comments feed for this article
April 9, 2008 at 5:10 pm
Paul Hummer
Matt-
I actually had a chat with someone at Canonical today, and he took the concept of test driven development to a tedious and more finegrained approach then I have ever heard of. It’s amazing how high you can get your test coverage well when write a minimal test, write a minimal amount of code to pass the test, write more of the test, write more of the code. It was crazy.
Paul
November 13, 2008 at 10:50 am
Ian Cooper
Matt,
Have you ever taken a look at Behavior Driven Development (BDD)? You may find it interesting: http://dannorth.net/introducing-bdd
-Ian
November 15, 2008 at 2:24 pm
laymanstermsdev
Ian,
I’ve never heard of that before. I’ll be sure to give it a look though. However, since the time I wrote this post, I’ve been point to Michael Feathers’ “Working Effectively with Legacy Code.” It’s a great book that gets right to the heart of what I was so woeful about. I’d strongly recommend it to anyone that was having the same problems that I was.
-Matt