Design Question: Using Members To Share Data Between A Method and SubMethod?
Posted on 2010-09-02
Say we have a public method "method," which uses private "subMethod1" and private "subMethod2" in its calculation. These subMethods serve no other purpose than to break down "method" into more readable, encapsulated sub-parts.
We need each of these sub methods to operate on 2 or more primitive data types, which are local variables defined at the top of method. We cannot pass them into the sub methods by reference, since java does not allow primitives to be passed by reference, and anyway passing an argument in to be altered is not a preferred design idiom.
So our solution is to refactor, and change the local variables defined at the top of method to member variables, which can of course be read and written by both method and its sub-methods. The problem (maybe it is a problem) is that now we have member variables that don't really exist to store object state, but only exist, effectively, during the execution of method. Of course, in actuality they exist throughout the whole lifetime of the object, but they are only ever used during the execution of method, and they could vanish at other times without affecting the behavior of the object at all (keep in mind that method initializes the members to default values at the beginning of its method body).
So something about that design feels wrong. Is it? If so, what is a better way to refactor?