Imagine we have the following setup (let it be written in Python, the language is not essential though):
def f1_provider(vars, degree):
dim1, dim2, dim3 = set_dims(vars, degree)
def f1(*args1):
# ...
# use vars and dims
# ...
return f1
def f2_provider(vars, degree):
dim1, dim2, dim3 = set_dims(vars, degree)
def f2(*args2):
# ...
# use vars and dims
# ...
return f2
def f3_provider(vars, degree):
dim1, dim2, dim3 = set_dims(vars, degree)
def f3(*args3):
# ...
# use vars and dims
# ...
return f3
# then use them somewhere:
def f1_user():
# ...
f1 = f1_provider(vars1, degree1)
# ...
def f2_f3_user():
# ...
f2 = f2_provider(vars2, degree2)
f3 = f3_provider(vars2, degree2)
# ...
As you can see, this design is not so good at least because:
- The
dim1, dim2, dim3 = set_dims(vars, degree)line is being repeated over and over again. I might say the same about signatures of these provider methods - but let's not be so captious, I can bear with that at least :). - The
set_dimsfunction is applied one extra time: it's result is already known during thef2_provider(vars2, degree2)call. However, it runs once again during the third provider's call.
How can this be reworked in a functional way?
If we were in OOP one should have introduced a class like GeneralProvider, put the set_dims call in its constructor and made these providers public methods. Nope, not so interesting :).