0
bool FunctionA (const ObjectA& a)
{
    // ... some code ...
    const ObcjectB& b = getObjectBFromSomewhere(...);
    // ... code heavily reliant on b ...
    return boolean_val;
}

bool FunctionB (const ObjectA& a)
{
    bool boolean_val = FunctionA(a);

    // ... some code ...
    const ObcjectB& b = getObjectBFromSomewhere(...);
    // ... code that only needs one tiny bit of b ...
    return boolean_val;
}

FunctionA gets called just once. FunctionB also only gets called once. Both get called in different flows.

The code that "getObjectBFromSomewhere" is memory heavy so I want to only call it once.

What's the best way to refactor this code?

Joseph D.
  • 11,804
  • 3
  • 34
  • 67
solarflare
  • 423
  • 3
  • 14

2 Answers2

0

Thanks for the ideas, this is how I solved my problem:

bool FunctionA (const ObjectA& a, bool& bSomeLittleBit = false)
{
    // ... some code ...
    const ObcjectB& b = getObjectBFromSomewhere(...);
    bSomeLittleBit = b.getSomeLittleBit();
    // ... code heavily reliant on b ...
    return boolean_val;
}

bool FunctionB (const ObjectA& a)
{
    bool bSomeLittleBit = false;
    bool boolean_val = FunctionA(a, bSomeLittleBit);

    // ... rest of the code as normal ...

    return boolean_val;
}
solarflare
  • 423
  • 3
  • 14
  • 1
    you may want to make `bSomeLittleBit` parameter a reference – Lorence Hernandez Jun 20 '18 at 05:50
  • @LorenceHernandez it is. I edited. Making it a reference actually also caused other problems - you cant have a default value parameter for a reference. So I introduced a dummy static bool and assigned it as the default value. Works well now. – solarflare Jun 20 '18 at 06:03
0

your question seems to imply that there is one canonical objectB which doesn't move in memory however for whatever reason getObjectBFromSomewhere() is hard on memory but will always return a reference to this same object?

perhaps you're looking something like this:

//static means only available in this compilation unit
static const ObcjectB *obj = nullptr;

bool FunctionA (const ObjectA& a)
{
    // ... some code ...
    if(!obj)
       obj = &getObjectBFromSomewhere(...);
    const ObcjectB& b = *obj;
    // ... code heavily reliant on b ...
    return boolean_val;
}

bool FunctionB (const ObjectA& a)
{
    bool boolean_val = FunctionA(a);

    // ... some code ...
    if(!obj)
       obj = &getObjectBFromSomewhere(...);
    const ObcjectB& b = *obj;
    // ... code that only needs one tiny bit of b ...
    return boolean_val;
}

if you're referring to the fact that functionA is called by functionB and therefore performs redundant work by calling getObjectBFromSomewhere() you could change the signature of functionA to accept a pointer to the object which it will use instead of calling getObjectFromSomewhere by itself i.e.

FunctionA(const ObjectA& a, ObcjectB *obj = nullptr)
{
   if(!obj)
      obj = &getObjectBFromSomewhere(...);
   const ObcjectB& b = *obj;
}

and then change functionB to pass this param:

FunctionB (const ObjectA& a)
{
    //move the expensive call up to the start of the method
    const ObcjectB& b = getObjectBFromSomewhere(...);
    bool boolean_val = FunctionA(a, &b);

    // ... some code ...

    // ... code that only needs one tiny bit of b ...
    return boolean_val;
}
5Volt
  • 36
  • 6
  • This looks like an odd implementation of [the singleton pattern](https://stackoverflow.com/questions/43292897/is-implementation-of-double-checked-singleton-thread-safe), including all its race conditions for threaded code. – Bo Persson Jun 20 '18 at 09:20
  • The first sample is the singleton pattern straight up. The second isn't really, I don't think an optional variable really counts as a design pattern at all. Either way, OP didn't ask for threadsafe, but you could just bang a mutex on it, no? – 5Volt Jun 21 '18 at 23:38