Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is passing null in to a method acceptable

Tags:

c#

null

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.

like image 726
ricki Avatar asked Mar 31 '11 10:03

ricki


People also ask

Can you pass null to a method?

You cannot pass the null value as a parameter to a Java scalar type method; Java scalar types are always non-nullable. However, Java object types can accept null values.

Is it good to pass null?

Here the function is called with a nullable argument, and if the argument was null, the function will return null, and if not it will return a proper value.

Can we pass null to main method in Java?

That means they accept null values. When we pass a null value to the method1 the compiler gets confused which method it has to select, as both are accepting the null. This compile time error wouldn't happen unless we intentionally pass null value.

What happens when you call a method on a null object?

If you call a static method on an object with a null reference, you won't get an exception and the code will run. This is admittedly very misleading when reading someone else's code, and it is best practice to always use the class name when calling a static method.


2 Answers

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.

like image 70
Aliostad Avatar answered Oct 11 '22 22:10

Aliostad


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

like image 43
Yngve B-Nilsen Avatar answered Oct 11 '22 23:10

Yngve B-Nilsen