C# Parameter Null Validation, Explained

C# Parameter Null Validation, Explained

hackernoon.com hackernoon.com2 years ago in #Dev Love58

It is a very basic practice to check the null value in the parameter. In this blog, I want to talk about why we should do this, and how we can do this. 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 catch 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 , 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) { this.CarCount = 1; this.CarNames.Add(car.Name); } 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.  » Read More

Like to keep reading?

This article first appeared on hackernoon.com. If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply