I have to make changes in a class which has many members, which are set by a function which implements some algorithm, and then are read by some other function which implements some other algorithm.
class Clazz{
private int[] someArray1;
private string[] someOtherArray2;
.
.
.
.
private DataStructure[] someArray10;
public Output createOutput(Input input){
algorithm1(input);
return createOutput();
}
private void algorithm1(Input input){
//many conditions and loops which change the data structures someArray1 ... someArray10
}
private Output algorithm2(){
//many conditions and loops which read (or change?) the data from the data structures and finally produce another data structure.
}
}
I want to refactor this implementation so that as many functions as possible become "functional" - so that they don't have side effects and return a well defined result in the return value.
The straighforward solution I came up with is to create many nested Context
objects which contain the fields each function changes, and pass these contexts from one function to another.
class Context10 {
private DataStructure[] someArray10;
/*ctor, get, set*/
}
class Context2 {
private string[] someOtherArray2;
private Context10 context10;
/*ctor, get, set*/
}
.
.
.
class Context {
private int[] someArray1;
private Context2 context2;
/*ctor, get, set*/
}
class Clazz{
public Output createOutput(Input input){
Context context = algorithm1(input);
return createOutput(context);
}
private Context algorithm1(Input input){
Context10 context10 = foo(input);
Context2 context2 = bar(context10, input);
Context2 context2_1 = baz(context2, context10, input);
.
.
.
Context returnContext = createReturnContext(context2, context10);
return returnContext;
}
private Output algorithm2(Context context){
/* use the data structures in the context to produce the output. */
}
}
This solution seems to be cumbersome and not too efficient.
Is there some technique to deal with this kind of classes?