Software Quality

May 18, 2010

Code Contracts really speed development

Filed under: Code Contracts, Uncategorized — David Allen @ 8:45 pm

I was out of the office for 2 weeks. When I returned, I was pairing with two of my coworkers. They each reported cases in which code contracts alerted them to accidental misuse of code. They probably would have found their problem eventually. But the presence of code contracts, combined with a rich set of automated tests, detected the problems sooner than they might have otherwise.  This was more positive reinforcement for me that code contracts are a valuable technique to include in a system’s design. They help steer you or your coworkers away from incorrect usage.

Don’t forget to add the optional descriptive text parameter to your contracts. What is a “good” contract comment? Imagine you are another programmer, and you accidentally violate a contract. Is the reason for the contract’s existence obvious to you? If not, then you should explain WHY the contract has been placed there.

Example

 
void Deposit(decimal amount){
   Contract.Requires(amount  > 0,"system does not accept zero or negative deposits");
   ...
}

Code contracts are just one technique for your toolbox. But they are a useful tool to include in your systems. So don’t forget to develop your skill at contract programming.

Blog at WordPress.com.