Test-First vs. Test-After, Round 4

by Jeff Langr

January 02, 2008

I submit two articles per month to Developer.com. It’s often tough coming up with new article ideas, so coming up with a theme that will produce a long stream of ideas is always a good idea (as long as the publisher is willing to accept it). Design patterns is something I’ve done about a dozen articles on.

Over the nice, long Christmas holidays (vacation for me), I started writing an article featuring the memento design pattern. For me, the hardest part of writing any of these articles is coming up with a good example. I try to avoid the common examples that are already out there, and I don’t think abstract examples are very useful. In any case, once I’ve thought of and coded an example, the article almost writes itself.

Right now, I’m on my third example attempt for memento, which for me is a fiasco. I’ve probably burned eight hours on something that usually only takes me a few. Memento is not something I’ve (explicitly) used, so that’s not helping.

Each example attempt has been centered around the typical use of memento, to support undo/redo capability. I floundered through two examples, discarding each in turn when I decided that it wasn’t a good representation of “real” use of memento.

The other real problem was that I figured I’d wing it. I’ve coded the examples for every other design pattern article using TDD. But over the holidays, I guess I got lazy. For memento, I banged out a solution and figured I’d refactor to something that I could easily unit test after the fact if I felt compelled to.

It’s been a while since I’ve lazed off and not done TDD. As usual, it was a good reminder that it’s just a crappy, less-effective way of working. The code I produced was a mess, with logic entwined in the UI class, as tends to happen when there’s no interest in unit testing. Worse, I kept getting stuck on a problem with BigDecimal and scaling that wouldn’t have given me such trouble had I used TDD.

Maybe I’m just not a very good developer, after all. Or maybe, just maybe, test-after is a less mature way to code.

Share your comment

Jeff Langr

About the Author

Jeff Langr has been building software for 40 years and writing about it heavily for 20. You can find out more about Jeff, learn from the many helpful articles and books he's written, or read one of his 1000+ combined blog (including Agile in a Flash) and public posts.