I guess I'll try to explain my question through 2 code snippets:
// snippet 1
class FooBar
{
private int value;
public int DetermineSomeResult()
{
PerformSomeCalculationOnValue();
PerformSomeMoreStuffOnValue();
return value;
}
public int GetCachedValue()
{
return value;
}
}
The first implementation basically has methods which operates on the private integer member.
Here's the second implementation
// snippet 2
class FooBar2
{
private int value;
public int DetermineSomeResult()
{
int tempvalue =GetCachedValue();
tempvalue = PerformSomeCalculationOnValue(tempvalue);
tempvalue = PeformMoreStuffOnValue(tempvalue);
SetValue(tempvalue);
return tempvalue;
}
public int GetCachedValue()
{
return value;
}
}
For the second implementation, no change is made to the internal state until all the calculations have finished. Design-wise which one is better?
I tend to prefer the second one as it is clearer, shows the dependencies. However, since the class itself already stores a class member intended for the calculated value, it seems silly not to use it.
Any thoughts on this?