0

What is the common practice of adding parameter to a method of a java class?

Normally if I am modifying an equivalent method written in older python (without type support), I naturally put the new parameter last to avoid making changes to existing code.

However here is my situation:

class A {
   public void doSomething(File infile, File workingDir) {
     ...
   }
}

Now I want to introduce an Enum type that is related to the type of infile and it needs to be passed to the method doSomething:

public Enum FileType {
  int DATA,
  int IMAGE
}

In this case, should I put the parameter FileType fileType to the very end

class A {
   public void doSomething(File infile, File workingDir, FileType fileType) {
     ...
   }
}

The pro is: it is predictable. Every new parameter will be added to the end.

However I feel that in my case, infile and fileType are more closely related to each other, they should be grouped together.

class A {
   public void doSomething(File infile, FileType fileType, File workingDir) {
     ...
   }
}

I think the second approach is suitable to a statically typed language like Java. I have not been able to find much about parameter ordering in common style guide.

So what is the right thing to do in my example? What is the best practice in Java?

closed as primarily opinion-based by gnat, Jörg W Mittag, BobDalgleish, Laiv, Bart van Ingen Schenau Jun 19 at 13:36

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Use wrapper object - relying on params order is not a good idea and you can easily get confused when using such a method. So in your case e.g. TypedFile can aggregate File and FileType. – Tomasz Maciejewski Jun 10 at 13:11
  • Do you have any reference which suggests putting new parameters at the end? I've personally never heard of this. Also, when you want to keep parameters closely together because they're related, it might be a hint that there's a new class that should be created which encapsulates those concepts. – Vincent Savard Jun 10 at 13:12
  • 1
    Not sure if it makes sense for you, but an alternative would be to create two versions of doSomething: doSomethingWithData() and doSomethingWithImage(). Underneath they can call some common code with the extra parameter, but this may keep your public API clearer. (Frankly, I'd add the 3rd parameter as you did but this is an option to consider) – user949300 Jun 10 at 15:46
2

You should generally order parameters in order of importance. Critical parameters are in the beginning, and optional parameters are generally towards the end. This allows you to create potentially new method signatures containing fewer optional parameters which simply calls the method with greater optional parameters with default values, without reordering parameters. The advantage is that you follow the "No Surprises" paradigm where otherwise rearranging parameters of the same type could be called incorrectly without realizing it.

That said, three parameters is already plenty. If you start looking at four or more, you should consider trying to group parameters by a class representative of all the information contained within, such as using Coordinate rather than x, y, and z, or FileFilter which when called can return a list of files to handle (which is both flexible and potentially reduces many possible method signatures you could have based on different ways of determining files to select).

If you still have many parameters, consider using a Builder pattern to simplify and allow your class to deal one or two method signatures with the instance containing all the information you require.

Not the answer you're looking for? Browse other questions tagged or ask your own question.