The following is an extract from an interface contract.
My question is: should the former method repeat the pre-conditions of the latter method? As the contracts are public, would it be acceptable to omit the repetition? In this case it's simply a non-null check, but I can imagine the situation where a lot of code is repeated and performance suffers as identical checks are repeated at runtime.
public int CommandConsumerCount(IWriteCommand writeCommand)
{
Contract.Requires(writeCommand != null); // redundant?
Contract.Requires(this.IsOwnerOf(writeCommand));
Contract.Ensures(Contract.Result<int>() >= 0);
return default(int);
}
public bool IsOwnerOf(IWriteCommand writeCommand)
{
Contract.Requires(writeCommand != null);
return default(bool);
}
It kind of depends on whether that condition (writeCommand != null
in this case) is a requirement for both the CommandConsumerCount method as well as the IsOwnerOf method, or if it is only a requirement for the IsOwnerOf method.
If that condition is only really required by the IsOwnerOf method, then it would be ok to omit it from the CommandConsumerCount method.
However, if that condition is required by both methods, then I stand by my original answer below:
I think that because both of your methods are public that the contract requirements should be repeated. If you had a private method that did the actual work, which both the IsOwnerOf method and the CommandConsumerCount method called (instead of the CommandConsumerCount method calling the IsOwnerOf method), then it would be fine to omit the call to Contract.Requires inside of that private method.
As far as performance goes...I wouldn't be concerned about the performance impact of these checks, unless the logic for the checks themselves is very complicated. You can set up the compiler to exclude those calls to Contract.Requires from the compiled output in the "Code Contracts" section of the project properties (assuming you have the necessary plugin installed).
Perhaps I'm missing the point of your question, though. Are you asking if it's OK to completely omit the call to IsOwnerOf inside of the CommandConsumerCount method? In that case I would leave the call in place, but if it's a performance concern, then I would configure the project to exclude that call for Release builds, assuming that I've done sufficient testing with a Debug build to ensure that this condition is satisfied.
EDIT: after re-reading the question, it seems clear that you're asking about the writeCommand != null
check, so I've struck out the above paragraph.
Code example below of adding a private method that performs the actual work of the IsOwnerOf method.
// you may want to choose a different name for this method
private bool _IsOwnerOf(IWriteCommand)
{
// actual work is done here in this private method
return default(bool);
}
public bool IsOwnerOf(IWriteCommand writeCommand)
{
Contract.Requires(writeCommand != null);
// call the private method to perform the actual work
return _IsOwnerOf(writeCommand);
}
public int CommandConsumerCount(IWriteCommand writeCommand)
{
Contract.Requires(writeCommand != null);
Contract.Requires(_IsOwnerOf(writeCommand)); // call the private _IsOwnerOf method instead of the public method
Contract.Ensures(Contract.Result<int>() >= 0);
return default(int);
}