How to avoid coupling two methods that have a similar implementation now, but which may change in the future?
<p>I've got two methods in a class that currently share a very similar implementation, but an implementation that is quite costly in terms of performance.</p>

<p>Sample:</p>

<pre><code>class Example
{
    public void process(Object obj)
    {
        boolean someFact = getFirstFact(obj);
        boolean someOtherFact = getSecondFact(obj);

        //use the two facts somehow
    }

    public boolean getFirstFact(Object obj)
    {
         boolean data = someExpensiveMethod(obj);
         //return some value derived from data
    }

    public boolean getSecondFact(Object obj)
    {
         boolean data = someExpensiveMethod(obj);
         //return some other value derived from data
    }

    public boolean someExpensiveMethod(Object obj){...}
}
</code></pre>

<p>Ive thought about somehow caching the result of someExpensiveMethod, but that seems wasteful, given that objects tend to come in, be processed and then discarded. It also seems clunky - methods need to know about a cache, or I need to cache results in ssomeExpensiveMethod.</p>

<p>Even short term cache could be bad news, since literally millions of objects get processed every day. </p>

<p>My concerns are twofold - firstly, there is no guarantee that these two methods will always depend on the third, so any solution should e transparent from their POV, and secondly that the obvious solution (cahcing inside someExpensiveMethod), could be very costly in terms of space for results that arent needed to be kept long term.</p>
