0

I like programming in a functional style. But it occurred to me that passing functions as arguments may sometimes create stronger coupling between components than would be created with non-function argument passing.

For instance, in the (contrived) example below: A, B, and C are coupled by the output type of C's function argument, by the type of C's int argument, and also by the arity and input type of C's function argument. If A wants to change its function to a BiFunction, B and C must both change.

public class Functional {

  public void A() {
    C(i -> String.valueOf(i + 2), 123);
  }

  public void B() {
    C(i -> String.valueOf(i + 1), 123);
  }

  private String C(Function<Integer, String> f, int a) {
    int len = String.valueOf(a).length();
    return f.apply(len);
  }
}

Contrast this with an example where A and B compute the result of the function up front, then pass the String result and the second int argument to C. Here A, B, and C are coupled only in two places – the two arguments to C. The function to compute the first argument is free to change.

Obviously passing functions has its place, but it seems that this kind of problem can compound and create rigid code when things get more complex. I'd love to hear any rules of thumb you use to decide when coupling in this way is ok vs. not.

1
  • 1
    That’s a strange consideration. 1) “If A wants to change its function to a BiFunction” since when can a caller decide to change a method’s parameter type? C offers a functionality and defines the prerequisites; A doesn’t have to “want” anything here. 2) If there’s no requirement for C to have a function as parameter type, it shouldn’t demand to get a function. If the offered functionality demands it, well, then it’s out of discussion. In your example, as contrived as it is, it is impossible for the callers to compute the result of the function up-front. But they could afterwards.
    – Holger
    Commented Aug 26, 2016 at 17:41

2 Answers 2

3

In your example I would recommend to follow principle of Single Responsibility

Gather together the things that change for the same reasons. Separate those things that change for different reasons.

So choosing in what way to couple A,B with C depends on why they are going to change.


Another principle that comes to mind from your example is Rule of least power

Given a choice of solutions, pick the least powerful solution capable of solving your problem

in your simplified example function C doesn't need a function as an argument. That function can be called in A,B side.

2

If you're replacing a simple value with a function argument, you're doing something seriously wrong. Functions should only be replacing something with tighter coupling, usually an application-specific Interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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