Consider these two methods
public int GetSomething(object obj)
{
Contract.Requires<ArgumentNullException>(obj != null);
...
}
public int GetSomethingWrapper(object anotherObj)
{
var obj = GetObj(anotherObj);
return GetSomething(obj);
}
Let's consider GetObj
to be safe i.e. it doesn't throw any exceptions.
Thus GetSomething
and GetSomethingWrapper
throw execption if obj is null. But in the latter case the origin of exception is GetSomething
method.
The question is whether I'd add checks for GetSomethingWrapper
?
One the one side: not its business to care about. On the other: both methods are public but caller of the wrapper method has no contract information.
One the one side: not its business to care about.
Yes it is - it's calling a method with a contract, so it ought to abide by that contract. The simplest way of abiding by that contract is to impose its own contract.
Then as you say there's the additional problem that currently GetSomethingWrapper
has no contract, so it should be legitimate to call it with an null argument... but it's really not.
So basically, yes - I'd add the contract to GetSomethingWrapper
too. The implementation detail that it calls GetSomething
shouldn't affect the public contract.