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..
}
}
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"); } ... }
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... }
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With