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.

Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137

1 Answers1

3

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • That's clear. Thanks. But what if contract of the DoSomething() method will be weakened later? Thus my wrapper will impose more strict requirements. This is not terrible but not so easy to catch this situation. Unfortunately static checker doesn't warn about unnecessary checks. Any ideas concernign this problem? – Pavel Voronin Apr 12 '13 at 10:30
  • 1
    @voroninp: It's up to you whether you try to make one set of contracts follow the other. You could update them both at the same time - but you *may* not want to. Basically, this is what you get for exposing functionality in a redundant way - it comes with the responsibility of thinking about it when things change. That's not to say it's a bad idea, just that it comes with the territory. – Jon Skeet Apr 12 '13 at 10:32