I would argue that this is not ok. Passing null around is never a good practice, and in my opinion whenever a value has null it should be an exception. You might not always have control over how your data is passed, and in those cases you should check for null just in case it could occur. However if you have the option to control the arguments yourself, you should rather overload the method, instead of passing in null.
Consider the following scenario:
public string DoSomething(string a, string b)
{
// Returns something after a and b is processed.
}
In this example we're indicating that a and b are strings. They are objects of type char[] or however you would like to define a string.
Passing in a null in this method makes no sense at all, since we're expecting strings. A null is nothing - it's not an empty string - it's simply void.
The reason why I say that I don't think it's preferable to send in null
into a method is because there are never any uses for null
. It's simply a void left there as a placeholder for something that failed to instansiate. Therefore, calling the method above like this:
DoSomething("whatever", null) makes no sense at all.
We could argue that we could do something like this:
/// <summary>
/// Does something with string a or b
/// </summary>
/// <param name="a">The first string. If it's null, nothing is done with it</param>
/// <param name="b">The second string. If it's null, nothing is done with it</param>
/// <returns></returns>
public string DoSomething(string a, string b)
{
// Returns something after a and b is processed.
}
But that also makes the code less readable. Why not make an overload DoSomething(string a) ? Or maybe refactor the method completely since it can do something with a
and null
and still return a valid value. null
is not a valid value, so hence the result of an operation including null
as an argument should not return a valid value.
It's like using infinity in maths. You will always get infinity as a result if you add/subtract/multiply or divide something with it.
Disclaimer
These are my own opinions, and by no means the 'right' way of programming. But since the question has no right answer, I'll practice my right to free speech :)