15

I'm wondering, where - and how often - in the code validate method's arguments.

In the example class below (a .dll library), what do you think is the best way? Suppose I want to validate, that some object cannot be null (but it can be any other validation required for method to run properly). Is it better to check it only once in point 1., in public method avaible for user, and later "trust myself", that in other, private, methods it will not be null or, better be a little paranoid and check it every time it's going to be used (in points 2. 3. and 4.)

Checking it just before using the object (in points 2, 3, 4) protects me in the future, if I decide to change something in the class, using these private methods, and "forget" to pass valid object. Also I don't have to remember about validation if I add some new public method in the future. On the other hand - it's checking for the same condition over and over again. Or maybe you have some other suggestions?

public class MyClass()
{
    public MyClass()
    {
    }

    public void ProcessObject(SomeObject obj)
    {
        //1. if (obj == null) throw new ArgumentException("You must provide valid object.");

        DoSomething(obj);
        DoMore(obj);
        DoSomethingElse(obj);
    }

    private void DoSomething(SomeObject obj)
    {
        //2. if (obj == null) throw new ArgumenException("You must provide valid object.");
        //do something with obj...
    }

    private void DoMore(SomeObject obj)
    {
        //3. if (obj == null) throw new ArgumentException("You must provide valid object.");
        //do something with obj...
    }

    private void DoSomethingElse(SomeObject obj)
    {
        //4. if (obj == null) throw new ArgumentException("You must provide valid object.");
        //do something with obj..
    }
}  

3 Answers 3

12

If it's an API that you're exposing for other developers to consume, then on each of your methods you should indeed throw either ArgumentException or ArgumentNullException.

If it's internal classes or methods that other developers will not be interacting with directly, I would use Debug.Assert, so in debug mode you get additional debugging information, and in release mode it gets nopped away and you receive the performance benefit of not doing all these argument checks everywhere.

1
5

In a good software design methods need to be independant from each other. You must be able to use them in any order and separated from each other. So yes you have to check the same condition in all of the methods over and over again.

Edit: I would use CodeContract with a Pre-Condition that checks that obj is not null. Since this seems to be a pre-condition for the methods to work correctly.

1
  • That how I was thinking as well, but it seems to be in opposite to other answers... Let's wait for other opinions.
    – mj82
    Jun 17, 2012 at 19:01
3

Each public method should validate its input.

Private methods can rely IMHO on validation of other public methods in the class if you're just passing a previously validated object. Since private methods are encapsulated in the class the responsibility of validation stays in the class.

However, if you have a private method that takes some calculated parameters (not the ones passed to the public method), than additional validation should be in place.

Your unit tests will be testing public interface of the class, and they will fail if your SomeObject is null if your validation is only in ProcessObject. In that case I wouldn't duplicate validation in private methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.