2

I have a method that takes 2 parameters today to complete the computation. The prospects of these 2 parameters becoming 50 in the future is pretty good. Given this, what is good?

  1. Write func(int a, int b) today. When parameters go beyond 7, then transition to 2 or 3 below.
  2. Write func(RootOfObjectGraphWith50Fields a) today(RootOfObjectGraphWith50Fields is used in other parts of the code base). This way when more parameters are needed, the method signature need not change. But this increases coupling (the method needs to know more to complete its business).
  3. Write func(InterfaceWith50Fields a) today (with only 2 fields) and have RootOfObjectGraphWith50Fields implement InterfaceWith50Fields. This way the method signature does not have to change every time a new value is required to complete the computation. The method is not coupled to the entire RootOfObjectGraphWith50Fields but only to those fields that it is interested in (expressed in InterfaceWith50Fields).

Besides recommendations, I appreciate pointers to basic design principles behind the recommendations.

Ram
  • 865
  • 1
  • 8
  • 20

1 Answers1

1

I'm actually going to recommend both options 1 and 3. I recommend the first option because if your current requirement is that the method take 2 parameters, then func(int a, int b) is completely appropriate, at least right now. If later on this method would require 50 parameters, then you can always just add an overloaded func(InterfaceWith50Fields a) version to the class.

Note that the third option does not offer very much over the second one other than that it avoids having to change the method signature. As the POJO you are passing in really only exists to wrap 50 fields of various types, the relevant methods/behaviors in any implementing class here would be the getters. As interfaces typically don't know anything about the data which their implementing classes would have, you might still have to rewrite some code as you upgrade from 50 to say 51 parameters.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360