No, I am not talking about legal contracts, I am talking about code contracts.

What is a "contract" in code? It is simply defining a piece of code (usually an interface but sometimes an abstract class) that defines what the implementation will do. Then every piece of code talks using the contract and not the actual implementation. This becomes very very useful when you need to extend/intercept/modify a class.

The first time I used contracts for development effectively was on a system that needed to find people. The interesting thing was that a person could exist in two different places. The first was our local customer database and the second was in the companies global repository of people that we could access via a com+ call. I found this clicked so well that I thought we would recreate it here.

The first thing I did was define an interface for finding people:

Public Interface IPersonFinder
    Function FindByIdentifier(ByVal identifier As String) As IPerson
    Function FindByName(ByVal firstName As String, ByVal lastName As String) As Ilist(Of IPerson)
End Interface

Then wrote out the code to access our local database that implements the contract of IPersonFinder:

Public Class LocalPersonFinder
    Implements IPersonFinder

    Public Function FindByIdentifier(ByVal identifier As String) As IPerson _
Implements IPersonFinder.FindByIdentifier Dim customerRetriever As New Data.CustomerRetrieiver Return customerRetriever.GetCustomerById(identifier) End Function Public Function FindByName(ByVal firstName As String, ByVal lastName As String) As _
System.Collections.Generic.IList(Of IPerson) Implements IPersonFinder.FindByName Dim customerRetriever As New Data.CustomerRetrieiver Return customerRetriever.GetByPartialName(firstName, lastName) End Function End Class

Then wrote out the code to access our global repository:

Public Class GlobalPersonFinder
    Implements IPersonFinder

    Public Function FindByIdentifier(ByVal identifier As String) As IPerson Implements _
IPersonFinder.FindByIdentifier Using comComponent As New MyCompany.PeopleFinder Dim criteria As New FindByIdCriteria(identifier) Dim people As IList(Of MyCompany.Entities.Person) = comComponent.FindPeople(criteria) If people.count = 0 Then Return Nothing Return ConvertGlobalPeopleToLocalPeople(people) End Using End Function Public Function FindByName(ByVal firstName As String, ByVal lastName As String) As _
System.Collections.Generic.IList(Of IPerson) Implements IPersonFinder.FindByName Using comComponent As New MyCompany.PeopleFinder Dim criteria As New FindByName(firstName, lastName) Dim people As IList(Of MyCompany.Entities.Person) = comComponent.FindPeople(criteria) If people.count = 0 Then Return New List(Of IPerson) Return ConvertGlobalPeopleToLocalPeople(people) End Using End Function End Class
Now we have two classes the operate in totally different ways internally but implement the same contract 
of IPersonFinder. This allows us to work with either finder and not care about which one we are working with.
All that we care about is that they implement a standard contract. Here is a simple implementation:
Public Class CustomerService
    Public Function DoesCustomerExist(ByVal identifier As String) As Boolean
        Dim finder As IPersonFinder = CreateFinderBasedOnIdentifier(identifier)
        If finder.FindByIdentifier(identifier) Is Nothing Then Return False
        Return True
    End Function

    Private Function CreateFinderBasedOnIdentifier(ByVal identifier As String) As IPersonFinder
        If identifier.Substring(0, 1) = "G" Then Return New GlobalPersonFinder
        Return New LocalPersonFinder
    End Function
End Class
Here we have a Factory Method called CreateFinderBasedOnIdentifier that determines which finder should be used 
(in this example any identifier that starts with "G" is in the global repository) and returns it. As you can
see the DoesCustomerExist() method does not know and does not care which finder it is using, it just uses it
and is quite happy doing so.
This is one of my favourite usages of a contract and is called the Strategy Pattern. I find it is the simplest
way to get a feel for developing to contracts with immediate results.