I find defensive coding a really great way to improve the quality of a codebase.
Code contracts is an implementation of Design by contract (dbc). Dbc was introduced in 1986 by Bertrand Mayer for use with Eiffel.
The concept is simple, check that the input to a method is valid and that the return values are valid. What valid means is set up in pre- and post conditions. As simple as it sounds – in most projects I have seen this is sparsely used. I am not sure why. perhaps many developers are to lazy? I know I can be sometimes. It is easy to fall into the old bad habit of thinking “this code will change, I will improve it later”. As we all know – LATER IS NEVER.
A primitive and often used way of implementing dbc is obviously using an if statement and throwing an exception. Sometimes this is referred to as a guard.
public long Divide(int x, int y)
{
if(y == 0)
throw new ArgumentEcxeption();
return x/y;
}
This tends to get rather verbose over time. I have used this project on CodeProject in a few projects and found it helpful. The code looks a lot better.
public long Divide(int x, int y)
{
Check.Require(y != 0);
long result = x / y;
Check.Ensure(result < 15); // whatever you want to check...
return result;
}
The code is much cleaner, this however relies on a static class and a bunch of flags – not very flexible but fully understandable.
Microsoft has developed their own integrated solution called Code Contracts. If you use Visual Studio (for Code Contracts Standard Edition) or Visual Studio 2008 Team System or Visual Studio 2010 Premium Edition or Visual Studio 2010 Ultimate Edition (Premium Edition) you can download it at http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx
The use of Code Contracts is very similar to the last solution:
public long Divide(int x, int y)
{
Contract.Requires(y != 0);
Contract.Ensures(result < 15); // whatever you want to check...
return x / y;
}
After installing Code Contracts you get a new tab in project settings:

Runtime contract checking enables checking of the contracts.
If you have the right version of Visual studio you also get to check static contract checking. This is in my opinion a killer feature. It means that you will get warnings at compile time if a call to a method violates the contract or if the result of a method violates the post condition. It will also give suggestions on where you should put contracts and check for nulls etc.
The downsides I see with Code Contracts is that it is a separate download from Microsoft and that sometimes creates problems when maintaining legacy applications. The pain to get all new and old add-ons and libraries to get a project up and running is sometimes a big problem. I always prefer a project I can include in the build and include in version control and build on build servers. However – I have not yet integrated Code Contracts with a build server. it would be very nice to get a report in Teamcity.
It also tends to get a bit verbose when coming to more complicated stuff as interfaces and inheritance, most things are solved using attributes.
There are a bunch of other stuff you can do with Code Contracts like turning on / off checking on methods via attributes and tweak settings.
I believe that if we start using defensive coding more rigorously we will produce higher quality software and save ourselves some headache. Furthermore I see this as a really good compliment to TDD, if we don’t have to write tests for all the things that should not happen we can focus on verifying behavior which I find more interesting, fun and productive!