I don’t know how many times I have seen a method which I have been trying to debug with a whole stack of parameter checking code at the start of the method to ensure nothing weird happens with unexpected input. This subsequently caused the nicely abstracted methods to be less abstract than originally intended. Thankfully .Net 4.0 makes life for the developer, and subsequent debuggers a lot easier with Code Contracts.
Code contracts come under the namespace
and the most used class in this is the Contract class.
There are some different flavours of code contracts, the first is Pre-conditions. This will be similar to the checks done to insure your method hasn’t been passed some incorrect parameters.
e.g. a simple divide example which requires that the divisor isn’t 0
if this method is called with b as 0, when compiling and doing static analysis in Visual Studio 2010 (depending on the code contract options set on the project options found in the standard project settings in Visual Studio) will either show as an error, therefore not allowing the project to build, or show an error. There is an override on the Requires method which allows you to put your own warning/error message if the contract is not met:
this analysis is pretty clever and knows when you are trying to trick it 😉
These conditions inform on the finishing state of a method, properties of the return types, objects which should not have been modified etc.
now if a NullReferenceException is thrown the code contract will ensure that nothing has been added or removed from the collection.
Invariants are rules for objects which should only be modified in a certain way throughout their scope. e.g. collection must not be empty, a value must not be null, a counter must be non-negative…
For these type of rules you need to mark your method with the attribute:
Now inside the method we can set up our rules.
this will ensure that the collection is initialized, always has at least 1 non-null entry.
Code contracts seem extremely useful and I look forward to writing code and debugging using them.