Posted on Friday, July 09, 2010 5:27 PM
This is the kick-off for my series on Code Contracts, a new addition to the .NET framework, built-in from version 4 and back ported to .NET 3.5 by use of the Microsoft.Contracts assembly. In this first post I’ll try to give a general introduction to the Code Contracts. Just what is it and what purpose does it serve.
I guess many developers have had there time writing classes for some wanna-be framework and trying to defend themselves against all kind of bogus input. So on method entry all parameters are checked for non-null values, for proper values within a predefined range, etc. Throwing ArgumentExceptions and ArgumentNullExceptions all around. An occasional ArgumentOutOfRangeException might also be on the list. And you might conclude you have your method sealed and locked down. No further hassling with the parameters, everything works perfect.
But what about the callers of your locked-down method? Are they going to obey your contract? Or are they just going to put the whole thing in a try-catch bloc and catch the ArgumentExceptions? Which IMHO is always wrong, but that’s a different issue. The compiler won’t complain about a null being passed, even though the method that is called will check for it and throw an ArgumentNullException. It will only appear as such at runtime. And this sums up just two issues we face today when designing and implementing validation checks on method entry.
Just come to think of it, what happens when your precious method gets overloaded in a derived class? You could argue that such a thing should not be possible in a valid interface design and inheritance should be on some protected method and not on a private one. But still, there is no way you can enforce your ‘contract’ to the derived method. It is not obligated to call the base method, and even if it would, it is not ensured that it would happen directly on entry of the method body. Sometimes that would even be the complete opposite of the desired way of coding.
So what’s next? There is no ‘silver-bullet’. Not for parallelism in coding, but also not for ensuring a public contract on a method. But now there is Code Contracts from Microsoft. It’s not the definite answer, but it comes a long way. The issues I mentioned above about runtime failure, inheritance and such do not apply to Code Contracts. Code Contracts get inherited by derived classes and overridden methods. And since the Code Contracts can be statically checked during compile time, it will help developers to deliver better code, with fewer runtime failures.
But what is Code Contracts? As defined at the Microsoft site:
“Code Contracts provide a language-agnostic way to express coding assumptions in .NET programs. The contracts take the form of preconditions, post conditions, and object invariants. Contracts act as checked documentation of your external and internal APIs. The contracts are used to improve testing via runtime checking, enable static contract verification, and documentation generation.”
I could not come up with a better phrase. As can be read from this definition, the Code Contracts take the form of preconditions, post conditions and object invariants.
The following gives a quick overview of each of these:
In the following posts in this series I plan to be more specific on each of the mentioned methods. For now, I’ll leave it to this, with some useful links to conclude with:
That’s it for now
Meile