views:

83

answers:

2

Hi All,

I'm using the Code Contracts classes in the System.Diagnostics.Contracts namespace to define some contracts for my objects and I'm wondering how others name their contract classes when the contract is defined against a base interface. Let me illustrate with a small example:

[ContractClass(typeof(AnimalContract))]
public interface IAnimal
{
    // definition here
}

[ContractClassFor(typeof(IAnimal))]
public class AnimalContract : IAnimal
{
    // explicit interface implementation here
}

In this example I've called the Contract Class AnimalContract, but is there a better name? Because the contract is defined against an interface I almost want to name the class IAnimalContract to illustrate it's a contract for the IAnimal interface. This way when I see the two items in Solution Explorer they visually "tie-up", which helps to keep things nice and clean in my head. Of course, the contract itself is not an interface, so naming it in such a way just smells bad to me.

How do you guys (and gals) name your Contract classes in these cases? Is there a generally accepted convention?

+1  A: 

Personally I'm used to naming them in the 'IAnimalContract' fashion. But your point taken, I'd say name them 'ContractForIAnimal', and keep them in a folder named 'Contracts'.

This way you can easily find your contracts, and you won't have that bad smell about your naming conventions =)

KoMet
+2  A: 

The Code Contracts User Manual uses both the IAnimalContract format (see section 2.8) and the ContractForIAnimal format (see section 4.1). Therefore, there is no one specific style that the code contracts team themselves wish for you to follow.

However, I see the IAnimalContract style used the most often, and following a common naming convention is definitely important. Additionally, I think it's more useful to be able to quickly find the contract class by looking for the interface type name than it is to just find all contract classes together.

Personally, I keep my contract class in the very same file as the interface, which is something I rarely do otherwise. I feel the contracts are part of the interface and should be kept as close as possible.

Rich