I am working on a project that contains a lot of preexisting code. I see one particular
pattern used all across the code base and that is driving me crazy. Here is an example:
1. In the constructor of a service class, it creates an the object of another class that implements an algorithm and initializes that object.
2. Another method of this service class takes in the actual data upon which the algorithm has to operate. This method uses the previously initialized object and calls the functional method on it passing in the data.
The code is actually hundreds of lines but I have compressed it to illustrate the general idea. The problem that I face is while debugging.
For example, there is a exception while persisting the amortizations and I need to find out what is wrong. It could be because of the initialization parameters given while constructing the Amortizer or it something is being incorrectly computed in the compute method. Since Amortizer is constructed well in advanced and in a different
thread, it is very difficult to find how were the parameters that were used to create Amortizer created. Similarly, since the compute method computes the whole set before persist is attempted, it is not possible to stop the debugger to check the computation for the case which causes persist to fail later.
So what I have to do is, first figure out which persist failed and get its id. Now put an if condition in the compute method:
if(id="the one that fails in persist"){
sout("..."); put a break point here
}
and run the damn thing again so that it would wait at that point where it starts computing for the problematic id and i can inspect the parameters.
I imagine it would be a lot simpler to debug if it were coded like this
So basically, what I am getting at is :
1. I am not liking member variables too much. They increase the distance between their creation time and their usage time. This becomes very confusing.
2. I don't like the pattern where you initialize an object separately outside of a constructor using multiple setter or even using an initializer method.
3. I would prefer passing as many method parameters as possible in main business method instead of setting them using multiple methods and then calling the business method.
Am I thinking right? Or the code that I have is actually well written and it is me who is thinking wrong? Is there is a name for this pattern?
thank you!