8

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.

Alavaros
  • 1,404
  • 6
  • 28
  • 47
  • 1
    Two quick ideas: 1. Varargs. Especially when all parameters are of the same type. 2. Decorator pattern. It allows to "enrich" your methods on the runtime. You can find plenty examples on the internet. Good luck;) – Tinki Sep 24 '15 at 07:27

5 Answers5

5

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.

Andreas
  • 138,167
  • 8
  • 112
  • 195
2

while we are at the topic:

Java 8 has introduced a new feature, specifically designed to support for multiple versions of an Interface: default methods. So let's say your Manager class is implementing MyInterface. So the first version of the Interface would be

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

then, with accordance to the ever changing world of technology, requirements change. so you need a new signature for myMethod(). with the default method feature, you could delegete the responsibility of backward compatilibity to 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 read more about Interface default methods in Oracle tutorial on the subject

Sharon Ben Asher
  • 12,476
  • 5
  • 25
  • 42
1

This question depends on whether the code you are calling expects some of the parameters to be null. Given the popularity of this question I would recommend that you avoid sending null values as much as possible.

In this case, since you are the owner of the method you are passing null values to I would recommend that you have 2 versions of the method, one which can work with 1 parameter and another one which works with 3 parameters.

Changing existing methods is usually not recommended since it can break existing code. Adding new functionality on the other hand should leave previous work intact. It would also allow you to construct relatively pin pointed test cases since you would test the new behaviour not the entire previous behaviour and the new one.

Community
  • 1
  • 1
npinti
  • 50,175
  • 5
  • 67
  • 92
1

I would propose to define the two methods you mentioned instead of providing only the 3-parameter method and forcing all users of the method to supply "null" as values. Providing the stripped-down 1-parameter method makes it explicit that the two other parameters are optional.

This is also recommended in the correct answer of this question.

Community
  • 1
  • 1
1

It's not inherently good nor bad practice. As it was mentioned before, this is the approach of introducing default parameters in Java.

The only potential problem with having an overload that takes only one parameter and behind the scenes calls another overload (and passes other parameters by default) is when this is not clear from the method's 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 */
}

You should document your first method properly in this case to make sure your users understand the implications of calling it. If you have an overload that takes parameters that you need to explicitly set to null, you don't have this "hidden" issue - however you should still document your method.

I think it's up to you really. Whatever you choose, make sure you document your API.

By the way, you may want to consider passing an object as parameter or employing some other patterns if the list of parameters grows and your are forced to have a huge number of overloads.

async
  • 1,541
  • 11
  • 28