Jeremy D. Miller -- The Shade Tree Developer

Sponsors

The Lounge

Syndication

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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.

 


Posted Tue, Dec 11 2007 8:26 AM by Jeremy D. Miller

[Advertisement]

Comments

John Chapman wrote re: TDD/BDD and Design by Contract: I don't really see any conflict between the two
on Tue, Dec 11 2007 2:14 PM

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.

h_push_it » TDD/BDD and Design by Contract: I don't really see any conflict … wrote h_push_it » TDD/BDD and Design by Contract: I don't really see any conflict …
on Tue, Dec 11 2007 11:42 PM

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

Barbricks.Com » TDD/BDD and Design by Contract: I don't really see any conflict … wrote Barbricks.Com » TDD/BDD and Design by Contract: I don't really see any conflict …
on Wed, Dec 12 2007 3:33 AM

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

Colin Jack wrote re: TDD/BDD and Design by Contract: I don't really see any conflict between the two
on Wed, Dec 12 2007 7:16 AM

@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.

Greg wrote re: TDD/BDD and Design by Contract: I don't really see any conflict between the two
on Thu, Dec 13 2007 5:56 PM

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

http://rails.codebetter.com/blogs/jeremy.miller/archive/2007/12/11/tdd-bdd-and-design-by-contract-i-don-t-really-see-any-conflict-between-the-two.aspx wrote http://rails.codebetter.com/blogs/jeremy.miller/archive/2007/12/11/tdd-bdd-and-design-by-contract-i-don-t-really-see-any-conflict-between-the-two.aspx
on Sun, Mar 23 2008 3:23 AM
http://fitnesse.codebetter.com/blogs/jeremy.miller/archive/2007/12/11/tdd-bdd-and-design-by-contract-i-don-t-really-see-any-conflict-between-the-two.aspx wrote http://fitnesse.codebetter.com/blogs/jeremy.miller/archive/2007/12/11/tdd-bdd-and-design-by-contract-i-don-t-really-see-any-conflict-between-the-two.aspx
on Mon, Apr 7 2008 10:52 AM

Add a Comment

(required)  
(optional)
(required)  
Remember Me?