CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeremy D. Miller -- The Shade Tree Developer

Under the hood and working with .Net, TDD, Software Design, and Agile Stuff

TDD/BDD and Design by Contract: I don't really see any conflict between the two

I found an interesting link today from Mario Gleichmann called Test Driven Development and Design By Contract - friend or foe?.  I've always been lukewarm toward Design by Contract (DbC).  Occasionally I'll run into claims from DbC enthusiasts that using DbC completely eliminates the need or desire for Test Driven Development and I'll start rolling my eyes.  That attitude stayed perfectly intact until I spent some time with Greg Young at DevTeach talking about spec#

After learning a bit about spec# I can see plenty of value for DbC, but certainly not as a full replacement or even an alternative to TDD/BDD.  One way or another it's all about specifying the desired behavior and using some sort of automation to check that the specification is being satisfied.  What I envision is using DbC to efficiently knock down a lot of the boring problems like erroneously passing nulls around at compile time.  I would still use TDD/BDD to get to the interesting part -- the actual behavior of classes and methods.  Greg is claiming that having spec# would replace 2/3 of his unit tests with more declarative DbC style specifications inside the code with arguably a faster feedback loop than running NUnit tests.  All I can say is "cool."  Cutting down feedback cycle time is always advantageous.  Eliminating tedious imperative code with declarative specifications is definitely good.

I'm not a fan of copious amounts of defensive programming being anywhere but externally facing API's.  I don't write a lot of unit tests checking for bad data coming into or out of methods because I don't see that activity as having a positive reward to effort ratio and I can't abide clutter in the code.  I haven't particularly recognized any real pain from not writing these unit tests, but if I did feel that pain, I'd rather just use DbC specifications in the code and get going with the more interesting work.

 



Comments

John Chapman said:

Am I the only developer out there who uses TDD to verify that my contracts are being checked?  Maybe I just take it to the extreme?  Note that I typically just use the pre-condition contract portion, not the post-condition checks.  I rely entirely on my tests for the results.

# December 11, 2007 2:14 PM

h_push_it » TDD/BDD and Design by Contract: I don't really see any conflict … said:

Pingback from  h_push_it » TDD/BDD and Design by Contract: I don't really see any conflict …

# December 11, 2007 11:42 PM

Barbricks.Com » TDD/BDD and Design by Contract: I don't really see any conflict … said:

Pingback from  Barbricks.Com » TDD/BDD and Design by Contract: I don't really see any conflict …

# December 12, 2007 3:33 AM

Colin Jack said:

@John

No we do that too, as you say just for pre-conditions but as Jeremy says Spec# would remove the need for all those tests.

# December 12, 2007 7:16 AM

Greg said:

Damn jeremy now I have to write a few pages on this :)

# December 13, 2007 5:56 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Jeremy D. Miller

Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy previously worked as a systems architect building mission critical supply chain software for a Fortune 100 company and learned agile development practices as a .Net consultant at ThoughtWorks, one of the pioneers of agile development. Jeremy is the author of the open source StructureMap (http://structuremap.sourceforge.net) tool for Dependency Injection with .Net and the forthcoming StoryTeller (http://storyteller.tigris.org) tool for supercharged FIT testing in .Net. Jeremy's thoughts on just about everything software related can be found on his weblog "The Shade Tree Developer" at http://codebetter.com/blogs/jeremy.miller, part of the popular CodeBetter site. Jeremy is a Microsoft MVP for C#. Check out Devlicio.us!

Our Sponsors

Free Tech Publications

This Blog

Syndication

News

All opinions expressed here constitute my (Jeremy D. Miller's) personal opinion, and do not necessarily represent the opinion of any other organization or person, including (but not limited to) my fellow employees, my employer, its clients or their agents.

About Me

"Best Of" Compendium

StructureMap (Dependency Injection for .Net)

StoryTeller (Supercharged Fit)

Build your own Cab

TestDriven

MVP