Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to call to an overloaded method with null params?

this is a question about programming best practices, I didn't know how express the question in the Title, sorry, here we go.

I had a method in a Manager, or Controller, this way:

public boolean myMethod(Param1 param1);

And, because a change in the application, I had to redefine it like this, because it calls to other method which needs param2 and param3:

public boolean myMethod(Param1 param1, Param2 param2, Param3 param3);

Now I realize that the method with 3 params "always" (for now, maybe in the future there is a change and I need to call it with non-null params) will be called with param2=null and param3=null, so in the implementation of 1st method I did:

public boolean myMethod(Param1 param1) {
  return this.myMethod(param1, null, null);
}

public boolean myMethod(Param1 param1, Param2 param2, Param3 param3) {
  /* Call to other methods that is needed to pass it param2 and param3 */
}

So the call to the method of the Manager, and the original way, is,:

boolean isTrue = myManager.myMethod(param1);

This is one option, the other option is to pass the null params from the call:

boolean isTrue = myManager.myMethod(param1, null, null);

And let only one method in my Manager:

public boolean myMethod(Param1 param1, Param2 param2, Param3 param3);

So, the real questions are: What is the best way to do this talking about best practices? Is it wrong if in the implementation of tha Manager I overload a method and call it with null params?

Thanks in advance!

Greetings.

like image 886
Alavaros Avatar asked Sep 24 '15 07:09

Alavaros


People also ask

Is it possible to overload a method with NULL parameter?

Since this is a compile time event, this can only happen when one intentionally passes null to this method. If this is done intentionally then it is better to overload this method again with no parameter or create another method altogether. Show activity on this post.

Why can't we pass Null as a parameter in Java?

Hence both Object and Integer are considered as Object instance. So when you pass null as a parameter than compiler gets confused that which object method to call i.e. With parameter Object or parameter Integer since they both are object and their reference can be null.

How do I call a method with a null argument?

Because each parameter type is a reference type. The following are the three ways to call one specific method of yours with null. doSomething ( (Object) null); doSomething ( (Integer) null); doSomething ( (char []) null); May I suggest to remove this ambiguity if you actually plan to call these methods with null arguments.

Why do I get a compile time error when passing nulls?

The reason why we get compile time error in the above scenario is, here the method arguments Integer and String both are not primitive data types 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.


1 Answers

The overloaded method with fewer parameters that just call the other method with more parameters, is a common practice in Java, and is the Java way of implementing "default" parameters.

Note that the default value doesn't have to be null, it can be any value.

Normally when calling a method with multiple parameters, the values passed in will give a clue to the nature of the parameter. Parameter constants like null, false, true, or 0 doesn't give a clue to the parameters meaning, which makes the code is less readable.

Usually a call with fewer parameter is more obvious, so overloading with "default" parameters is preferable to only one method with a lot of constant parameters.

like image 178
Andreas Avatar answered Sep 22 '22 06:09

Andreas