paint-brush
C# Parameter Null Validation, Explainedby@callmewhy
2,547 reads
2,547 reads

C# Parameter Null Validation, Explained

by Haiyang WangJune 25th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

C# Parameter Null Validation, Explained by Haiyang Wang Newbie Programmer. It is a common practice to check for null in parameters, but why? What would happen if we don't check the parameters? What's the benefit of parameters null validation? In most cases, it's necessary and useful in most cases. Validate null parameters can avoid execution which shouldn't happen, and it can specify the responsibility of the unexpected exception. It's a good choice to enable the CA1062 Warning, we needn't care about when to validate the parameters.

Company Mentioned

Mention Thumbnail
featured image - C# Parameter Null Validation, Explained
Haiyang Wang HackerNoon profile picture

It is a very common practice to check for null in parameters:

public void DoSomething(Foo foo)
{
    if (foo is null)
    {
        throw new ArgumentNullException(nameof(foo));
    }
}

In this blog, I want to talk about parameter null validation: why, when, and how.


Why

It's suggested in many coding style rules, it's followed in many open source projects, but why? What would happen if we don't check the parameters? What's the benefit of parameters null validation?

Let's talk about the following C# code:

public void PrintCar(Car car)
{
    if (car == null)
    {
        throw new ArgumentNullException(nameof(foo));
    }
    Console.WriteLine(car.Name);
}

In this case, if we don't check whether the parameter is null, it will raise a NullReferenceException; if we check the parameter, it will raise a ArgumentNullException. It looks like the NullReferenceException is missing, but actually we get a new ArgumentNullException. The caller still have to handle it. The number of exceptions doesn't change at all.

Referring to Voldemort as "You-Know-Who" cannot kill him.

If a method catches an exception and doesn't has the ability to handle it, is it needed to check the null parameters?

Different people have different opinions.

In the blog <Why I Never Null-Check Parameters>, the author made a point that it doesn't make sense if you catch the exception and ignore it. The bug needs to be visible to be easily corrected.

In my opinion, parameters null validation is necessary and useful in most cases.

The first reason is that, NullReferenceException is different from ArgumentNullException. The name of exceptions can help us find the responsible party. If you call the method and you get a ArgumentNullException, you may figure out there is something wrong with the parameters. Just like 40x and 50x status code in HTTP. Bad Request is totally different from Server Error.

The second reason is that, most methods in our service are not stateless. In these stateful methods, an exception in the middle of the method will mess the states up and we cannot roll back easily.

Take a look at this example:

public void CarCounter(Car car)
{
    // #1
    this.CarCount += 1;
    // #2
    this.CarNames.Add(car.Name);
}

When car is null, it will raise an exception in line #2. However, there is a state update in line #1. This may modify the count incorrectly. We need a mechanism like transaction in DB to roll back the executed part of the method. It may be very complicated. And for some cases like notification, the actions are irrevocable.

As a conclusion, in most cases parameter null validation is necessary and useful. It can avoid the execution which shouldn't happen, and it can specify the responsibility of the unexpected exception.

When

As we talked above, it's meaningful to check the null parameters. But should we check all method parameters in our projects?

It depends.

For public method, we don't know what parameters the caller will pass, we should check null parameters before the business logic.

For private methods, we needn't check because we are supposed to know how and where this method is called. In this situation, the null variables should be checked outside of the method before calling it.

It's a good choice to enable the CA1062 Warning. With this warning, we needn't care about when to validate the parameters. Just think about which access modifier should be used, public or private.

How

The following question is, how to check null parameters. The code for validation is verbose and useless. Validate null parameters every where will make the code less human-readable.

In the blog <C# Futures: Simplified Parameter Null Validation>, the author went through many ways to validate the parameters. The C# Proposal #2145 about bang operator is very interesting and I am really looking forward to it.

Given the Code Contract is deprecated, wrapping the validation into a Helper is a better choice:

internal static class ThrowIf
{
    public static class Argument
    {
        public static void IsNull<T>(T argument, string name)
        {
            if (argument is null)
            {
                throw new ArgumentNullException(name);
            }
        }
    }
}
public void DoSomething(Foo foo)
{
    ThrowIf.Argument.IsNull(foo, nameof(foo));
}


Please let me know if you have another solution. Thanks.

Thanks.

References: