Code contracts (Part 1): Introduction

During developing applications we have always use unwritten rule in programming or if it is written in design documents user should consider them in development. For example if developer wants to call a method which has an integer parameter and this parameter is used as an index of array he should be aware calling that method with negative value or greater than array lenght. Calling method with negative value causes runtime error.
There are lots of contracts that a developer should consider during development. Recently I came across new concept (However it is in research phase) named CodeContracts which is really wonderful. It seems that this concept will be in VS 2010. CodeContracts will organized all written and unwritten rules & contract in development. In order to clarify CodeContract usage imagine following code:
static void Swap(string[] arr, int itemIndex1, int itemIndex2)
{
string temp;
temp = arr[itemIndex1];
arr[itemIndex1] = arr[itemIndex2];
arr[itemIndex2] = temp;
}
What are possible errors when we want to call Swap method?
- None of itemIndex1 and itemIndex2 could be negative (both should be positive)
- None of itemIndex1 and itemIndex2 could be greater than arr.length
- Array arr should be not null
Before code contracts developer should always consider possible situation and avoid breaking rule. But CodeContracts provides rich methods by which you can add your Code Contracts to your method and after compiling application Contracts checker will warn about breaking rules. Let see how it should be implemented with CodeContracts
static void Swap(string[] arr, int itemIndex1, int itemIndex2)
{
Contract.Requires(arr != null);
Contract.Requires(itemIndex1 >= 0);
Contract.Requires(itemIndex1 < arr.Length);
Contract.Requires(itemIndex2 >= 0);
Contract.Requires(itemIndex2 < arr.Length);
string temp;
temp = arr[itemIndex1];
arr[itemIndex1] = arr[itemIndex2];
arr[itemIndex2] = temp;
}
I added five contracts to this method which mean this method needs not null array as first parameter, second and third parameter (itemIndex1, itemIndex2) should be greater or equal to zero and should be less than array length.
In the next post I will show how to implement this with CodeContracts
But now I want to introduce some resources:
- Microsoft Research – Code Contracts home page
- Tutorial Video in channel9
- Contract Checking and Automated Test Generation with Pex


_1372.png?psid=1)
Such a usefull Articles From you.
thanks alot.
i’m waitting for next.
Amir Mozaffarynia
June 24, 2009 at 4:38 pm
good. Learn something from these two articles.
john
June 24, 2009 at 4:39 pm
Hello! I tried to email you considering this post but, can’t seem to reach you. Please email me when get a minute of time. Thanks.
Popravilo računalnika
February 16, 2010 at 11:28 pm
That is it! I was searching for article like this one couse I need it for collage.
Servis računalnikov
February 18, 2010 at 12:05 am
Thank you for this great information, you write very well which i like very much. I really impressed by your post.
Energy Risk Management Software
December 1, 2010 at 11:16 pm
While reading your blog it seems that you research on this topic very much. I must tell you that your blog is very informative and it helps other also.
Energy Risk Management Software
December 1, 2010 at 11:21 pm