samedi 3 octobre 2020

Minimizing a method's coupling while still keeping it flexible

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.

Aucun commentaire:

Enregistrer un commentaire