Wednesday, July 9, 2008

Don't repeat yourself, just quote yourself.

I just read an article by Dan North about how the DRY principle doesn't work with Test code. Obviously I agree with him, because it is almost trivial once you've figured it out on your own. I already met many people that did and were quite happy with themselves for it, actually that could have been me after I had about 1 year of experience with TDD.

When I read the linked blog I mused over the main counter argument against dropping DRY, it is actually the argument to adopt it: what about the maintenance hell of all that repeated code. Arguably if you use proper mocking or stubbing (especially mocking) you shouldn't have too much trouble with this, but wouldn't it be nice if there was a solution for this?

So what are the requirements:
  1. We want to get readable code (not requiring you to constantly switch source files or scroll)
  2. We want little writing effort (not repeating the same code snippet over and over)
Requirement 2. is covered by OO, AOP and friends, but they completely ruin 1. in certain scenarios (e.g. test code). To get 1. you will have to drop 2... NOT good enough!

I am a fan of wikis and as you might know many have a feature called templates that specifically targets these two requirements. How about template in code. Just add a template construct and let the IDE optionally show the actual code inline.

I like it.

No comments: