TDD, Contracts, and Reading the Fine Print

Jimmy Bogard on coming to TDD and interface oriented Jesus and the aha moment of why we use interfaces:

An interface is a contract, that any implementation needs to adhere to. Consumers of the interface do not care about implementation details, nor can they, as the interface provides no other information besides the signatures it provides.

I quite like the point that Jimmy hints at. Classes, at a certain level, are tiny components. But is an interface really a contract? Yes… but it’s not the whole contract; our tests and specifications are part of the contract of our components.

A component does a couple of things. It will a) tell its container environment what services it needs to run and b) what services it provides. It does this by requiring or publishing one or more contracts. As far as what we have right now in the mainstream, if we’re pursuing the highest quality, it’s up to us to deliver contracts that fully document not just the data (as interfaces do) but, at times, the behavior or semantic qualities of these tiny components we’re writing. Sometimes those behaviors mean something to the supplier and the consumer.

Say we have a controller and that controller depends on a service. The controller creates a transaction and invokes a service method. Perhaps that service method then throws a business-meaningful exception. For argument let’s say we have a shopping cart and you’ve just added -1 pairs of agile pants to your order. Wait a second… what just happened?! The fact the exception happened is surely a part of the contract! We have to deal with it after all and abort the transaction or take some compensating action. In the case of ordering some agile pants, maybe you want to display a message indicating that quantity should be one or more. Yes, I realize this is a bad example, but stuff like this necessarily happens in the real world.

What if — just go with me here — we have a dependency that can throw an exception? Is that not part of the contract? Last I checked there wasn’t a way to say “hey, this interface can throw these kinds of exceptions” in C#. Consumers surely need to care about implementation details. What about invariants? If we simply have a structural contract we have nothing that expresses these behavioral elements. C# might get some spec# features, but we’re not quite there yet. Preconditions, postconditions, and invariants are a part of the contract we take in.

When we’re driving out interfaces and using a nice mock objects framework, we need to think about our consumers when we dive into a dependency and drive it out. What preconditions are you creating? What exceptions are you throwing? Do consumers of this service need to be aware or are they likely to want to handle these exceptions? If so, you need to return to the consumer and write some more tests. Get more mileage out of your code by applying defensive programming conservatively.

Simply put: contract = specifications + interface.

Interested in the potential benefits of DbC for more-robust contracts? Sifting through a quick Google search on the subject yields a nice post by Mario Gleichmann exploring the idea that interfaces all by themselves are poor contracts.

This entry was posted in BDD, DbC, oo, TDD. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

40 Responses to TDD, Contracts, and Reading the Fine Print

  1. will read more…thanks for share….

  2. massager says:

    What if — just go with me here — we have a dependency that can throw an exception? Is that not part of the contract?

  3. buy wow gold says:

    I like play online game, I also buy wow gold

  4. lotre says:

    hi,guy this site lotro-gold.eu it is about online game’s web,we offer the news and important cheats,The main we sell lotro gold,if u want to buy lotro gold,u need lotro gold from this site,it is cheapest,right,u want buy lord of the rings online gold,cheap lotro gold,plz click here:lotro gold,it is cool,isn’t it?everyone who play lotro and want buy lord of the rings online gold can get some help from our.we We have mass available stock of lord of the rings online gold on most of the servers,so that we can do a really instant cheap lotro gold delivery.We know what our buyers need so we offer an instant way of cheap cheap lotro gold, the cheapest wow gold delivery.lol…

  5. lotre says:

    hi,guy this site lotro-gold.eu it is about online game’s web,we offer the news and important cheats,The main we sell lotro gold,if u want to buy lotro gold,u need lotro gold from this site,it is cheapest,right,u want buy lord of the rings online gold,cheap lotro gold,plz click here:lotro gold,it is cool,isn’t it?everyone who play lotro and want buy lord of the rings online gold can get some help from our.we We have mass available stock of lord of the rings online gold on most of the servers,so that we can do a really instant cheap lotro gold delivery.We know what our buyers need so we offer an instant way of cheap cheap lotro gold, the cheapest wow gold delivery.lol…

  6. qwsaa says:

    [url=http://www.viccol.com/]eq2 platinum[/url]
    [url=http://www.viccol.com/]eq2 gold[/url]
    [url=http://www.runescapegogo.com/]runescape[/url]
    [url=http://www.viccol.com/]eq2 plat[/url]
    [url=http://www.viccol.com/]everquest2 plat[/url]
    [url=http://www.runescapegogo.com/]runescape gold[/url]
    [url=http://www.viccol.com/]everquest2 gold[/url]
    [url=http://www.viccol.com/]everquest2 platinum[/url]
    [url=http://www.runescapegogo.com/]rs gold[/url]
    [url=http://www.viccol.com/]buy eq2 platinum[/url]
    [url=http://www.viccol.com/]buy eq2 gold[/url]
    [url=http://www.viccol.com/]buy eq2 plat[/url]
    [url=http://www.runescapeize.com/]rs gold[/url]
    [url=http://www.viccol.com/]buy everquest2 plat[/url]
    [url=http://www.viccol.com/]buy everquest2 gold[/url]
    [url=http://www.viccol.com/]buy everquest2 platinum[/url]
    [url=http://www.viccol.com/2moons-buy.html]2moons dil[/url]
    [url=http://www.viccol.com/2moons-buy.html]buy 2moons dil[/url]
    [url=http://www.viccol.com/Age-Of-Conan-aoc.html]buy Age Of Conan gold[/url]
    [url=http://www.viccol.com/Age-Of-Conan-aoc.html]Age Of Conan gold[/url]
    [url=http://www.viccol.com/cheap-ArchLord-Gold-ArchLord.html]ArchLord Gold[/url]
    [url=http://www.viccol.com/dofus.html]dofus kamas[/url]
    [url=http://www.viccol.com/everquest2-gold-service.html]everquest2 plat[/url]
    [url=http://www.viccol.com/everquest2-gold-service.html]everquest2 platinum[/url]
    [url=http://www.viccol.com/everquest2-gold-service.html]everquest2 gold[/url]
    [url=http://www.viccol.com/everquest2-gold-service.html]eq2 plat[/url]
    [url=http://www.viccol.com/gaia-online-gold.html]gaia gold[/url]
    [url=http://www.viccol.com/guild-service.html]guild wars gold[/url]
    [url=http://www.viccol.com/guild-service.html]GW gold[/url]
    [url=http://www.viccol.com/Lineage2-adena.html]Lineage2 adena[/url]
    [url=http://www.viccol.com/buy-maple-story-MS-moses-eu.html]maple story moses[/url]
    [url=http://www.viccol.com/runescape.html]runescape[/url]
    [url=http://www.viccol.com/cheap-SNW-Vis-SwordoftheNewWorld.html]Sword of the New World Vis/url]
    [url=http://www.viccol.com/lotro-eu.html]lotro gold[/url]
    [url=http://www.viccol.com/cheap-wow-gold-eu.html]wow gold[/url]
    http://www.viccol.com/
    http://www.viccol.com/Age-Of-Conan-aoc.html
    http://www.viccol.com/2moons-buy.html
    http://www.viccol.com/cheap-ArchLord-Gold-ArchLord.html
    http://www.viccol.com/dofus.html
    http://www.viccol.com/everquest2-gold-service.html
    http://www.viccol.com/gaia-online-gold.html
    http://www.viccol.com/guild-service.html
    http://www.viccol.com/Lineage2-adena.html
    http://www.viccol.com/buy-maple-story-MS-moses-eu.html
    http://www.viccol.com/buy-maple-story-MS-moses.html
    http://www.viccol.com/runescape.html
    http://www.viccol.com/cheap-SNW-Vis-SwordoftheNewWorld.html
    http://www.viccol.com/lotro-eu.html
    http://www.viccol.com/lotro-gold-us.html
    http://www.viccol.com/cheap-wow-gold-eu.html
    http://www.viccol.com/cheap-wow-gold-us.html

  7. If you wanna buy some other things which are not listed in our website:
    http://www.rs-item.com
    http://www.rsloads.com
    http://www.g2gmart.com
    please contact with our customer service with on-line chat or add our Email,we will stock it for
    you in a short time after you told us what you needed.

  8. apple510 says:

    Rs2server.com is a professional company.Selling runescape gold(also called rs gold,rs money,rs gp),runescape item,runescape powerleveling,and doing the rs member quest or free quest,Buying or Selling runescape account

    http://www.rs2server.com rs2server runescape runescape gold runescape money runescape gp cheap runescape gold cheap runescape money cheap runescape gp fast runescape gold fast runescape money fast runescape gp runescape powerleveling runescape quest runescape member quest runescape item Runescape quest helper runescape account selling runescape account rs rs gold rs money rs gp cheap rs gold cheap rs money cheap rs gp fast rs gold fast rs money fast rs gp rs powerleveling rs quest rs member quest rs item rs quest helper rs account selling rs account runescape2 runescape2 gold runescape2 money runescape2 gp cheap runescape2 gold cheap runescape2 money cheap runescape2 gp fast runescape2 gold fast runescape2 money fast runescape2 gp runescape2 powerleveling runescape2 quest runescape2 member quest runescape2 item runescape2 quest helper runescape2 account selling runescape2 account rs2 rs2 gold rs2 money rs2 gp cheap rs2 gold cheap rs2 money cheap rs2 gp fast rs2 gold fast rs2 money fast rs2 gp rs2 powerleveling rs2 quest rs2 member quest rs2 item rs2 quest helper rs2 account selling rs2 account

  9. apple510 says:

    Rs2server.com is a professional company.Selling runescape gold(also called rs gold,rs money,rs gp),runescape item,runescape powerleveling,and doing the rs member quest or free quest,Buying or Selling runescape account

  10. nfl4sale says:

    Runescape Accounts
    http://www.rs-game.com

    Runescape Account
    http://www.rs-game.com

    RS Accounts
    http://www.rs-game.com

    RS Account
    http://www.rs-game.com

    Buy Runescape Accounts
    http://www.rs-game.com

    Buy Runescape Account
    http://www.rs-game.com

    Buy RS Accounts
    http://www.rs-game.com

    Buy RS Account
    http://www.rs-game.com

    Runescape Powerleveling
    http://www.runescape-power-leveling.com

    Runescape Power leveling
    http://www.runescape-power-leveling.com

    RS Powerleveling
    http://www.runescape-power-leveling.com

    RS Power leveling
    http://www.runescape-power-leveling.com

    Runescape Money
    http://www.runescape-club.com

    RS Money
    http://www.runescape-club.com

    Runescape Gold
    http://www.runescape-mall.com

    RS Gold
    http://www.runescape-mall.com

    Buy Runescape Gold
    http://www.ebuyrunescapegold.com

    Buy RS Gold
    http://www.ebuyrunescapegold.com

    Runescape Items
    http://www.salerunescapeitems.com

    Runescape Item
    http://www.salerunescapeitems.com

    Runescape Equipment
    http://www.rsluck.com

    RS Accounts
    http://www.rs-game.com

    RS Powerleveling
    http://www.runescape-power-leveling.com

    RS Money
    http://www.runescape-club.com

    RS Gold
    http://www.runescape-mall.com

    Runescape Items
    http://www.salerunescapeitems.com

    Runescape Item
    http://www.salerunescapeitems.com

    RS Item
    http://www.salerunescapeitems.com

    RS Items
    http://www.salerunescapeitems.com

    Runescape Equipment
    http://www.rsluck.com

    RS Equipment
    http://www.rsluck.com

    Runescape Equipments
    http://www.rsluck.com

    RS Equipments
    http://www.rsluck.com

    Runescape Forum
    http://forum.rs-game.com

    Runescape Community
    http://forum.rs-game.com

    Runescape Board
    http://forum.rs-game.com

    RS Game
    http://www.rs-game.com

    Runescape quest
    http://www.rs-game.com

    Runescape Quests
    http://www.rs-game.com/Runescape-Quests.html

    Runescape Coins
    http://www.rs-game.com/Runescape-Coins.html

    Runescape Quests
    http://www.rs-game.com

    Runescape Coins
    http://www.rs-game.com

  11. dfd says:

    engage in wholesale jewelry,handmade jewelry,fashion jewelry,discount jewelry,handcrafted Jewelry,cheap jewelry,jewelry wholesale,wholesale handmade jewelry,wholesale pearl jewelry,wholesale gemstone jewelry,wholesale crystal jewelry,wholesale turquoise jewelry,wholesale coral jewelry,wholesale shell jewelry,we can supply all styles of handmade jewelry

  12. gfd says:

    ccba
    [url=http://www.sz-window.com]ccbu[/url]
    引号ccb

  13. don says:

    Ultima Online Gold, UO Gold

  14. wholesale jewelry,wholesale handmade jewelry,discount jewelry,fashion jewelry,wholesale coral jewelry

  15. engage in wholesale jewelry,handmade jewelry,fashion jewelry,discount jewelry,handcrafted Jewelry,cheap jewelry,jewelry wholesale,wholesale handmade jewelry,wholesale pearl jewelry,wholesale gemstone jewelry,wholesale crystal jewelry,wholesale turquoise jewelry,wholesale coral jewelry,wholesale shell jewelry,we can supply all styles of handmade jewelry,discount jewelry,fashion jewelry,freshwater pearl jewelry,korean jewelry,crystal jewelry,beaded jewelry,agate jewelry,trendy jewelry,china fashion jewelry,jade jewelry,semi precious jewelry,jewelry set and so on.

  16. gold5465 says:

    Take a look at our price of wow gold. You will find it is a loyal and reliable store of cheap wow goldfor sale online. You may compare the price then buy wow gold from us . It will be a great fun that you will save huge money. We know what a gamer need so that we will provide you a really instant way of delivery, if you buy world of warcraft gold, gold wow from us. Just place your order to buy wow gold from us then feel the world class service.
    http://veryge.com/

  17. gold986986 says:

    Take a look at our price of wow gold. You will find it is a loyal and reliable store of cheap wow goldfor sale online. You may compare the price then buy wow gold from us . It will be a great fun that you will save huge money. We know what a gamer need so that we will provide you a really instant way of delivery, if you buy world of warcraft gold, gold wow from us. Just place your order to buy wow gold from us then feel the world class service.
    http://veryge.com/

  18. wow gold says:

    we sell world of warcraft gold. Welcome to our website about cheapest wow gold,delivery in 24 hours,7/24 service.

  19. wow gold says:

    More Azeroth sky! mmoinn.com owner cheapest wow gold , so that players can open the World of Warcraft advanced air transport network, as soon as possible to buy cheap wow gold and wow power leveling !

  20. wow gold says:

    To mmoinn.com purchase wow gold it! This is a professional trade platform, we provide you with cheap wow gold and the players need to wow power leveling , more surprises waiting for you!

  21. wow gold says:

    If wow goldfocal point of wow gold our new buy wow goldOpen RvR buy wow goldInfluence system cheap wow gold is mortal combat cheap wow goldbetween players of wow power levelingenemy realms. wow power levelingIn order power leveling to ensure that power leveling Keeps, and Fortresses. wow gold buy wow gold cheap wow gold

  22. asd says:

    The game has become the people’s lives in a large part of the game in the park where, dofus kamas is a favorite for all of the game,acheter des kamas on dofusvault.com we will be very much on the dofus kamas, Come on, friends, to understand All your favorite,acheter des kamas you should feel some happiness.

  23. asd says:

    The game has become the people’s lives in a large part of the game in the park where, dofus kamas is a favorite for all of the game,acheter des kamas on dofusvault.com we will be very much on the dofus kamas, Come on, friends, to understand All your favorite,acheter des kamas you should feel some happiness.

  24. lotro gold says:

    For the unaware, the Roheryn server is a “preview server” (not a test server, I’ve been told) for Lord of the Rings Online. It’s the staging ground for the big enhancements they have been introducing into the game since it was launched last April (has it really only been six months?!), and usually it’s not the final version that actually goes into production. As they are getting ready to publish the Book 11 expansion, which includes player housing, the Lotro Gold changes are in Roheryn but no houses were on sale except for a limited period of time yesterday afternoon (1-4 PM EDT). Judging from the turnout at the “Open House” event, considering that it was held during working hours on a working day, a great number of people must have been as excited about the new feature as I was. Here are a couple of pictures to show you what I mean.
    http://www.lotro-shop.com
    http://www.aocgolds.net/
    http://www.allgametrade.com

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>