Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to validate method's arguments?

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..
    }
}  
like image 846
mj82 Avatar asked Jun 17 '12 18:06

mj82


People also ask

How do you validate an argument in C#?

1) Check one each parameter and throw an exception when it is wrong: public void Method(object parameter1, object parameter2) { if (parameter1 == null) { throw new ArgumentNullException("parameter1"); } if (parameter2 == null) { throw new ArgumentNullException("parameter2"); } ... }

How do you validate an argument in Javascript?

Usually in javascript if you want to validate required arguments, you would do something similar to this: function Person(water, food, candy) { if (! water || ! food) { throw new Error('water and food are required for Person'); } // Do something with water and food... }

Why do we need to validate arguments?

When you're in the middle of an argument, validating the person you're arguing with is probably the last thing on your mind. But it should actually be your top priority. If you validate your counterpart's perspective, expertise, and feelings, you will keep the conflict focused on the issue.


2 Answers

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.

like image 61
Matthew Avatar answered Sep 27 '22 20:09

Matthew


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.

like image 26
Jason De Oliveira Avatar answered Sep 27 '22 20:09

Jason De Oliveira