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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With