Should I test for nullity of values in the args array of Main

252 Views Asked by At

According to this post :

is this overkill for assessing Main(string[] args)

It seems that args can never be null in

static void Main(string[] args)

Ok good to know, however, supposing args.Length == 1 is true is it possible to have args[0] == null ? First observation : A string array can have null elements

Second observation : If the main is invoked with the command line I wouldn't know how to pass null and I don't think it is possible,

Third observation : on the other hand (this is a bit far-fetched) it is possible that some code calls

Program.Main(new string[]{null}) 

Thus, it is possible to produce a case where one gets null values in the args array.

So the question is :

Is it overkill or good practice to make tests for nullity in the main's arguments ? (Especially considering that the main called like in the third observation is an unlikely case)

For example : is the following correct ?

 static void Main(string[] args)
 {
     if(args.Length == 1)
     {
          var v = args[0].Replace("Foo", "Bar");//hope args[0] is not null

or should I rather do

 static void Main(string[] args)
 {
     if(args.Length == 1 && args[0] != null)
     {
         //...

Note: I tagged it C# but I guess this applies to other languages as well

3

There are 3 best solutions below

0
On BEST ANSWER

As written in your question, the static void Main() is private (because private is implicit if missing). Whoever calls it must use reflection and must know he is walking on tiny ice. He has the duty/obligation of passing correct parameters (where "correct" in this case is "no null values" anywhere). If he doesn't do it, and he receives a NullReferenceException, the fault is only his. So the answer is "you have no duty to check for null values" (and I wouldn't do it. It is overkill)

As you wrote (and as written in the referenced answer), you can't pass null from the command line, so this removes the other possibility.

1
On

it is possible that some code calls

Program.Main(new string[]{null})

A program could indeed call Program.Main(null), so args could indeed by null.

The question is, why would you do that? Why would you do either Main(null) or Main(new string[]{null})?

Indeed, why would you explicitly call Main at all?

Either you've done something foolish in your code, in which case having something go wrong and then realising you've done something foolish is a good thing, or somebody's done something foolish in their code using reflection to access yours, in which case it's even better to have things just throw exceptions and break and hopefully convince then Don't Do That Then!*.

(One could make an argument for well-considered calling of Main() via reflection in some specialised cases, but then passing in null arguments isn't well-considered and being so specialised I'd say the onus is on the caller to consider that possibility).

If such folly were likely one could make a case for testing and then throwing an exception sooner rather than later, but it's so unlikely that I'd be inclined just to let things break as they will.

*"Doctor! Doctor! It hurts when I do this!" "Don't do that then!"

0
On

The operating system will never pass null elements in the args array. If you are writing an application that is intended to be executed from the command line then you are entitled to rely on the operating system to fulfil this contract.

If somebody (or you) writes a caller to invoke your code then it is the caller's responsibility to either fulfil the contract or catch and handle any exceptions that might result from violating the contract.