This may be mostly a question of style but when defining code contracts for non-void interface members, which approach is best:
Interface:
[ContractClass(typeof(IFooContract))]
public interface IFoo
{
object Bar();
}
Contract option 1:
[ContractClassFor(typeof(IFoo))]
public abstract class IFooContract : IFoo
{
object IFoo.Bar()
{
Contract.Ensures(Contract.Result<object>() != null);
throw new NotImplementedException();
}
}
Contract option 2:
[ContractClassFor(typeof(IFoo))]
public abstract class IFooContract : IFoo
{
object IFoo.Bar()
{
Contract.Ensures(Contract.Result<object>() != null);
return default(T);
}
}
Most of the literature I've seen tend towards option 2 but I feel that option 1 is better as it is clearer that this is purely about the contract (and option 2 is technically breaking the contract it just defined).
Are there any scenarios where option 2 is preferred over option 1?