LP on .NET

December 7, 2009

Checking for Null Arguments

Filed under: .NET,C#,Software Development — Larry Parker @ 10:26 pm

Checking for null arguments in a method is not a very glamorous programming task, but it can be worth the time investment.  Instead of your code reacting to a null argument at some unexpected place (and throwing a NullReferenceException), you can be more proactive and check for null arguments at the beginning of the method and throw an ArgumentNullException yourself.

For example, say we have a method called PostIt that takes two arguments – a URI of a web page, and an XML document to post to it.  In this case, neither of these arguments should be null, so we can check for valid arguments like this:

public void PostIt(Uri uri, XmlDocument document)
{
    if (uri == null)
        throw new ArgumentNullException("uri");

    if (document == null)
        throw new ArgumentNullException("document");

    // Arguments are good -- continue...
}

There’s nothing wrong with this approach, but writing code like this everywhere can become a bit tedious.  Furthermore, the above code is not immune to refactoring.  Renaming the method’s arguments will not change the literal passed into the exception’s constructor.

To make things a bit easier and more immune to refactoring, I created a static helper method that takes one or more lambda expressions, checks each for null, and throws an ArgumentNullException if it encounters one.

This is what the PostIt method looks like using the helper method:

public void PostIt(Uri uri, XmlDocument document)
{
    Helper.CheckArgumentNull(() => uri, () => document);

    // Arguments are good -- continue...
}

Instead of four lines of code, we have one.  Furthermore, renaming the arguments will rename the variables in the lambda expression.

Here’s the code:

public static void CheckArgumentNull(
    params Expression<Func<Object>>[] exprs)
{
    foreach (var expr in exprs)
    {
        if (expr.Compile().Invoke() == null)
            throw new ArgumentNullException(
                (expr.Body as MemberExpression).Member.Name);
    }
}

The CheckArgumentNull method uses the params keyword to allow for a variable number of parameters to be passed in.  The type of the parameter is a lambda expression that has no parameters and returns an object.  This works well for passing in arguments from the calling method.

The helper method iterates through each lambda expression, and for each expression it compiles it and executes it.  If it evaluates to null, an ArgumentNullException is thrown.  We want to pass in the name of the argument into ArgumentNullException’s constructor, so to do this we need to decompose the lambda expression.

The expression’s body is actually a member expression, so we cast to that, get the Member object, access its Name property, and finally pass this value to the constructor.

Of course there is a nominal performance cost with this approach (nothing is for free), so if your method will be called in a tight loop then it might make sense to just code the null check yourself.

But my performance testing showed the overhead to be very acceptable, and the time spent executing CheckArgumentNull was usually minimal in the context of executing the entire method.

Hope you find this useful.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: