23
votes

I’m refactoring some code and adding a method which will replace a (soon-to-be) deprecated method. The new method has the following signature:

FooResult Foo(FooArgs args) { ... }

The deprecated method contained a growing list of parameters. These parameters are now properties on the FooArgs class. The deprecated method has several guard conditions which checked for null values with the following structure:

if (parameter1 == null)
    throw new ArgumentNullException(“parameter1”);
if (parameter... == null)
    throw new ArgumentNullException(“parameter...”);
if (parameterN == null)
    throw new ArgumentNullException(“parameterN”);

Now that the parameters have been collapsed into the FooArgs class should I throw an ArgumentNullException for the individual properties of the FooArgs parameter:

if (args.Property1 == null)
    throw new ArgumentNullException(“args.Property1”);
if (args.Property... == null)
    throw new ArgumentNullException(“args.Property...”);
if (args.PropertyN == null)
    throw new ArgumentNullException(“args.PropertyN”);

Or to throw a more general ArgumentException for the entire FooArgs parameter:

if (args.Property1 == null)
    throw new ArgumentException(“Property1 cannot be null.”, “args”);
if (args.Property... == null)
    throw new ArgumentException(“Property... cannot be null.”, “args”);
if (args.PropertyN == null)
    throw new ArgumentException(“Property2 cannot be null.”, “args”);

Thanks!

4
Did the Closers really read the question? - Henk Holterman
Not sure why this has been closed. Seems like a perfectly reasonable question to me. Anyway, I'd like to add this: if you have control of the FooArgs class, and if the properties should never be null, you could delegate the checking to the class itself. - Matthew Strawbridge
I've removed the words "best practice" from the title and the question itself in hopes of elimnating the concerns that closed it. - John Laffoon
Isn't this exactly what code contracts are for in .Net 4.0? msdn.microsoft.com/en-us/library/dd264808.aspx - dash
have you considered a solution where parameter are valid and consistent objects with no nulls? example class may be ProcessStartInfo where you can set FileName to null and it will stil return empty string not null. This is some kind of null object pattern. i'm asking because this approach for more complicated objects may be inconvenient - wiero

4 Answers

28
votes

You need to add a check for the args itself to be non-null. The ANE is not appropriate for individual components, so you need to use more general AE, like this:

if (args == null)
    throw new ArgumentNullException(“args”);
if (args.Property1 == null)
    throw new ArgumentException(“Property1 cannot be null.”, “args”);
if (args.Property... == null)
    throw new ArgumentException(“Property... cannot be null.”, “args”);
if (args.PropertyN == null)
    throw new ArgumentException(“Property2 cannot be null.”, “args”);
10
votes

While I agree completely with dasblinkenlight's answer, you may also want to consider moving the validation for FooArgs into the FooArgs class itself. If this class is designed specifically to move arguments around, it is likely not valid for it to have null proeprties, in which case, I would allow its constructor to do its validation.

2
votes

In this case, it may be best to check for a null reference of the FooArgs parameter inside that method, and throw an ArgumentNullException if a null reference has been passed in. Then if other methods or sections of code use the parameters contained within the args class, they should be the ones to check this and throw exceptions as necessary. However, if your method that takes in the args class is the one to use all the arguments, then it would be better to check for valid parameters in that method, as you suggested.

Also, use ArgumentNullException only for arguments that are null references. If it's simply an invalid value (for example an empty string), then you should use the more generic ArgumentException.

2
votes

This kind of depends on your tooling and how you feel about your tooling (resharper, fxcops and the like). Some static code analysis tools accept this:

throw new ArgumentNullException(“args.Property...”,"args");

and reject this

throw new ArgumentNullException(“args.Property...”,"args.Property");

So if you want to use the tooling, then assertions of null-hood against a parameter property must throw an ArgumentException

It's also valid to just make it up as you go along. What ever communicates the right message to the maintenance developer to help him pass the parameters correctly is the correct message.