开发者

Is passing null in to a method acceptable

Null is an odd data type for me, it seems as though it is wrong to ever use, maybe its the null pointer errors i got so often as a beginner that now have me associating any instance of 开发者_高级运维null to some kind of evil!

Anyway my question is

in some cases is it ok to use null as a parameter? for example, a method may need a and b to do a task, but in some cases it may only need a. Is parsing in null ok for b in those odd instances and checking if(b==null) then we know what called it?

Or am i way of the mark here?

i must admit, what got me wondering whether this was acceptable was, the method in question could be overlaoded, but it would have a difference of possibly 5 or 6 lines out of the whole method. This made me worried about code duplication.


It is absolutely fine to pass null to methods. However, if you have a csae where you might not need to pass a variable, consider overloading.

The reason why you might pass a null is because you might have a method with quite a few parameters and you do not wan to implement all possible overloads.

I personally would not use optional parameters - there are many discussions on this very subject because of the issue with the method as Contract which is outside the scope of this question.


UPDATE

Correct way to do it is not to duplicate code but let overloads call each other:

public void Foo(A a)
{
    Foo(a, null);
}
public void Foo(A a, B b)
{
        // .......
}

This will tell client of this code:

  • You can call method with A only
  • You can call method with A and B

If I have only the second method, the client would not know if it is OK to pass null.


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 :)


If you don't use some parameters you can use Overloading or Optional Parameters in C# 4.0


Are you saying "is it ok to pass a null in as a parameter to a method?"

If so - yes - it is perfectly acceptable.

However, in the .Net 4.0 world, you may want to consider using optional parameters so that you don't even need to pass the null in, you can just omit it.

In the pre .Net 4.0 world, if it is a common scenario to pass this null into a method, you may want to create an overload that doesn't take the argument at all.


That is perfectly acceptable. If you are using C# 4.0 you can also make the parameter optional so that the caller doesn't need to specify the default value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜