0

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.

4

1 回答 1

3

一方面:不关心它的业务。

是的,它正在调用带有合约的方法,因此它应该遵守该合约。遵守该合同的最简单方法是强加自己的合同。

GetSomethingWrapper然后,正如您所说,还有一个当前没有合同的附加问题,因此使用 null 参数调用它应该是合法的……但实际上并非如此。

所以基本上,是的 - 我也会添加合同GetSomethingWrapper。它调用的实现细节GetSomething不应影响公共合同。

于 2013-04-12T10:20:57.217 回答