I have some code like this:
public void someMethod (Object x)
{
if (x == null) throw new ArgumentNullException ();
// more code...
}
Should I now do this:
public void myMethod (Object z)
{
someMethod (z);
}
Or this:
public void myMethod (Object z)
{
if (z == null) throw new ArgumentNullException ();
someMethod (z);
}
Note: I prefer speed of execution in this class
In my opinion, all public methods should validate their inputs, even if it is somewhat redundant. Having said that, I often compile the more expensive validation and code contracts using conditional compilation directives so that release builds only perform the most critical diagnostics and validation.
Checking for a null argument is an important and inexpensive operation which will have only the smallest performance impact over millions of calls.
Overloaded methods that all call a common internal method might perform less checking with the knowledge that their actual implementation performs full validation.
For what it’s worth, most of the BCL source code I’ve seen from Microsoft errs on the side of more validation, diagnostics, and contract adherence rather than less.