http://stiller.co.il/blog/2013/03/wf-4-5-and-code-contracts-slide-decks-and-code/
If you ever used a 3rd party API, you surely know the importance of proper documentation - Can this method return "null"? Do I have to check this return value? What are the valid values of this argument? You also probably know that the only way to discover if your assumptions are correct is to execute the code and pray that exceptions won't be flying around. There must be a better way!
Enter "Code Contracts". Code Contracts is an experimental project from Microsoft Research which provides a language-agnostic way to express coding assumptions in .NET programs, thus allowing for improved testability, static verification at compile time and automatic API documentation.
In this session we'll take a tour around Code Contracts, understand when and how we can use them, and show how correct usage of these constructs can significantly increase the quality of your code.
How to Troubleshoot Apps for the Modern Connected Worker
Enhance Your Code Quality with Code Contracts
1. Code Contracts
Enhance Your Code Quality with Code Contracts
Eran Stiller
Senior Software
Architect, Founder
March 20, 2013
2. About Me
• Software architect, consultant and instructor
• Technology addict
• 10 years of experience
• .NET and Native Windows Programming
• Specializes in large-scale, server-side, highly-concurrent systems
• Co-Founder at CodeValue Ltd.
• http://stiller.co.il/blog
3. Agenda
• Back to Basics
• Design by Contract
• Code Contracts
– Preconditions
– Postconditions
– Invariants
• Internals
• External Tools
5. The If…Throw Approach
public class MyClass
{
public void SomeMethod(string s, int i)
{
if (s == null)
throw new ArgumentNullException(
"s", "Your input string cannot be null!");
if (i < 0)
throw new ArgumentException(
"Your input must be non-negative!", "i");
// Your code here
}
}
6. The If…Throw Approach
• Issues
– Cumbersome – lots of code
– Indistinguishable from method code
– Does not apply to interfaces
– Does not apply across object inheritance
– Not visible at call site
– Not part of the contract
7. Design by Contract
“Design by contract (DbC) (…) is an approach for
designing software. It prescribes that software
designers should define formal, precise and verifiable interface
specifications for software components, which extend the
ordinary definition of abstract data types with preconditions,
postconditions and invariants. These specifications are referred
to as "contracts", in accordance with a conceptual metaphor
with the conditions and obligations of business contracts.”
(Wikipedia)
8. Enter Code Contracts
• Microsoft’s Design-by-Contract
implementation for .NET
– A subset of Spec#
• Language-agnostic way to express coding assumptions
• Checked documentation of your API
– Improved testability
– Static verification
– API documentation
9. Requirements
• .NET 4.0/4.5
• Visual Studio 2010/2012
– Any edition except “Express” edition
• Download and install from Microsoft Research
– http://research.microsoft.com/en-us/projects/contracts/
• Allowed for commercial use
• In addition
– Support exists for .NET 3.5 and VS 2008 as well
– Contracts API is part of an external assembly
10. Preconditions
• Define what your API expects
– Parameter validation
– Object state
– Environment condition
– Anything the caller can check!
11. Postconditions
• Define what the caller can expect from your API
– Return value constraints
– Exception constraints
– Object state after call
12. Invariants
• Define the rules which govern your object’s behavior
– Object state
– Field & property values
– Things that always hold true
14. Should I Use It? It Depends…
• Pros
– Reduce bugs
– Static verification
– API documentation
– Improved testability
– Shallow learning curve
• Cons
– Experimental project
– Static verifier less than perfect
– Some issues with VS2012
– Compilation time increase
15. There is a Middle Way!
public class MyClass
{
public void SomeMethod(string s, int i)
{
if (s == null)
throw new ArgumentNullException(
"s", "Your input string cannot be null!");
if (i < 0)
throw new ArgumentException(
"Your input must be non-negative!", "i");
Contract.EndContractBlock();
// Your code here
}
}
19. Code Contracts Editor Extension
• Visual Studio 2010 Only
– 2012 support still pending
– http://tinyurl.com/CCEditorExtensions
• Has some stability issues
20. Summary
• A contract is more than an Interface
• Design by Contract is a design approach
• Code Contracts enables Design by Contract on .NET
– Preconditions, Postconditions & Invariants
• An experimental project from Microsoft Research
• Rewrites your application’s IL code
• Integrates with Editor Extensions & Pex
• Give it a try!
21. Resources
• My Blog
– http://stiller.co.il/blog
• Code Contracts
– http://research.microsoft.com/en-us/projects/contracts/
– http://research.microsoft.com/en-us/projects/contracts/userdoc.pdf
– http://social.msdn.microsoft.com/Forums/en-US/codecontracts/threads
• Design by Contract
– http://en.wikipedia.org/wiki/Design_by_contract
• Pex
– http://research.microsoft.com/en-us/projects/pex/