Is it good practice to call an overloaded method with zero parameters?

This is a question about the best programming methods, I did not know how to express the question in the name, sorry, here we go.

I had a method in the dispatcher or controller as follows:

public boolean myMethod(Param1 param1); 

And, since the change is in the application, I had to redefine it like this because it calls another method that needs param2 and param3:

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

Now I understand that a method with 3 parameters is "always" (at the moment, maybe there is a change in the future, and I need to call it with non-zero parameters) it will be called with param2=null and param3=null , therefore in the implementation of the 1st The 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 */ } 

Thus, the call to the Manager method and the original method:

 boolean isTrue = myManager.myMethod(param1); 

This is one option, another option is to pass null parameters from the call:

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

And let there be 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 talk about best practices? Is it wrong if in the tha Manager implementation I overload the method and call it with zero parameters?

Thanks in advance!

Hey.

+7
java model-view-controller
source share
5 answers

An overloaded method with fewer parameters, which simply calls another method with more parameters, is a common practice in Java and is a Java way to implement default parameters.

Please note that the default value must not be null , it can be any value.

Usually, when calling a method with several parameters, the values ​​passed to give the key to the nature of the parameter. Parameter constants, such as null , false , true or 0 , do not give hints to parameter values, which makes the code less readable.

Usually a call with a smaller parameter is more obvious, so overloading with default parameters is preferable to only one method with many constant parameters.

+4
source share

while we are in the subject:

Java 8 introduced a new feature specifically designed to support several versions of Interface : default methods. So, let's say your Manager class implements MyInterface . Thus, the first version of the interface will be

 public Interface MyInterface { public boolean myMethod(Param1 param1); } 

then, in accordance with the ever-changing world of technology, requirements are changing. so you need a new signature for myMethod() . with the default method function, you can remove the responsibility of backward compatibility with the interface:

 public interface MyInterface { // v1 with default implementation default boolean myMethod(Param1 param1) { return myMethod(param1, null, null); } // new v2 - pure virtual public boolean myMethod(Param1 param1, Param2 param2, Param3 param3); } 

You can learn more about the default interface methods in the Oracle tutorial on this subject.

+2
source share

This question depends on whether the code you call will expect some of the parameters to be null . Given the popularity of this question, I would recommend that you not send null values ​​as much as possible.

In this case, since you are the owner of the method, you pass zero values, I would recommend that you have 2 versions of the method, one of which can work with 1 parameter, and the other with 3 parameters.

Modifying existing methods is generally not recommended, as it may violate existing code. Adding new features on the other hand should leave the previous work intact. It would also allow you to build tests with relatively pointed conclusions, since you would test the new behavior, not all the previous behavior and the new one.

+1
source share

I would suggest defining the two methods you mentioned instead of providing only a three-parameter method and forcing all users of the method to supply "null" as values. Providing a truncated 1-parameter method makes it clear that the other two parameters are optional.

It is also recommended in the correct answer to this question.

+1
source share

This is not inherently good or bad practice. As mentioned earlier, this is the approach of entering default parameters in Java.

The only potential overload problem, which takes only one parameter and behind the scenes, causes another overload (and passes the other default parameters) when this is not clear from the method documentation.

 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 */ } 

In this case, you should correctly document your first method to make sure your users understand the consequences of calling it. If you have an overload that accepts parameters that must be explicitly set to null , you do not have this “hidden” problem, however you should still document your method.

I think this is true. No matter what you choose, make sure you document your API.

By the way, you may want to pass the object as a parameter or use some other templates if the list of parameters grows and you are forced to have a huge number of overloads.

+1
source share

All Articles