In C#, are there any good reasons (other than a better error message) for adding parameter null checks to every function where null is not a valid value? Obviously, the code that uses s will throw an exception anyway. And such checks make code slower and harder to maintain.
void f(SomeType s) { if (s == null) { throw new ArgumentNullException("s cannot be null."); } // Use s }
Pro-drop parameter or Null subject parameter is the parameter which determines whether a language is a pro-drop language or not. A positive setting of the parameter allows an empty pro-element to be identified by its governor.
A null indicates that a variable doesn't point to any object and holds no value. You can use a basic 'if' statement to check a null in a piece of code. Null is commonly used to denote or verify the non-existence of something.
You can pass NULL as a function parameter only if the specific parameter is a pointer. The only practical way is with a pointer for a parameter. However, you can also use a void type for parameters, and then check for null, if not check and cast into ordinary or required type.
The String class in the System namespace provides the IsNullOrEmpty() method to check if a string is null or an empty string(""). This is a handy method to validate user input.
Yes, there are good reasons:
NullReferenceException
Now as for your objections:
And for your assertion:
Obviously, the code that uses s will throw an exception anyway.
Really? Consider:
void f(SomeType s) { // Use s Console.WriteLine("I've got a message of {0}", s); }
That uses s
, but it doesn't throw an exception. If it's invalid for s
to be null, and that indicates that something's wrong, an exception is the most appropriate behaviour here.
Now where you put those argument validation checks is a different matter. You may decide to trust all the code within your own class, so not bother on private methods. You may decide to trust the rest of your assembly, so not bother on internal methods. You should almost certainly validate the arguments for public methods.
A side note: the single-parameter constructor overload of ArgumentNullException
should just be the parameter name, so your test should be:
if (s == null) { throw new ArgumentNullException("s"); }
Alternatively you can create an extension method, allowing the somewhat terser:
s.ThrowIfNull("s");
In my version of the (generic) extension method, I make it return the original value if it's non null, allowing you to write things like:
this.name = name.ThrowIfNull("name");
You can also have an overload which doesn't take the parameter name, if you're not too bothered about that.
There is a new method in .NET API which simplifies null
check syntax.
ArgumentNullException.ThrowIfNull(someParameter);
I agree with Jon, but I would add one thing to that.
My attitude about when to add explicit null checks is based on these premises:
throw
statements are statements. if
is a statement.throw
in if (x == null) throw whatever;
If there is no possible way for that statement to be executed then it cannot be tested and should be replaced with Debug.Assert(x != null);
.
If there is a possible way for that statement to be executed then write the statement, and then write a unit test that exercises it.
It is particularly important that public methods of public types check their arguments in this way; you have no idea what crazy thing your users are going to do. Give them the "hey you bonehead, you're doing it wrong!" exception as soon as possible.
Private methods of private types, by contrast, are much more likely to be in the situation where you control the arguments and can have a strong guarantee that the argument is never null; use an assertion to document that invariant.
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