relaxxx
relaxxx

Reputation: 7824

repeated calling - coding practice

which one do you prefer? (of course getSize doesn't make any complicated counting, just returning member value)

void method1(Object & o)
{
    int size = o.getSize();

    someAction(size);
    someOtherAction(size);
}

or

void method2(Object & o)
{
    someAction(o.getSize());
    someOtherAction(o.getSize());
}

I know I can measure which one is faster but I want some comments... Not just executing time related... eg. if you are prefer method2, how many times maximally do you use o.getSize and what is the number what make you use method1 way? Any best practices? (imagine even different types then int) TY

Upvotes: 11

Views: 276

Answers (6)

johnsyweb
johnsyweb

Reputation: 141780

Since you don't want size to change when you call someAction() or someOtherAction() (as it cannot when it is the return value of a function), consider:

void method3(const Object& o)
{
    const int size = o.getSize();

    someAction(size);
    someOtherAction(size);
}

getSize() may be simple, or it may be doing a costly calculation. Also, the size of o may be changed by another thread between your calls to someAction() and someOtherAction().

Upvotes: 9

Alnitak
Alnitak

Reputation: 339786

I would go for method 1 not just because it's probably marginally faster, but mostly because it means I don't have to worry about whether the called method has any side effects.

Also, if this is called in a multi-threaded program this ensures that I'm always using my value of size - otherwise it might have changed between the two calls. Of course there may be cases where you explicitly might want to notice that change, in which case use method 2.

(and yes, per other answers, make size a const int to ensure that it's not modified if it's passed by reference to something else).

Upvotes: 10

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22064

The first one will eliminate unnecessary calls to a function, therefore I prefer method1() as the code also looks a bit cleaner.

However, you should be aware that depending on context, they may produce different results. Say, if size changes in someAction(), and you use value stored in size variable, you may not get the desired result.

Upvotes: 1

Adrian Marinica
Adrian Marinica

Reputation: 2201

When I call a function that returns something multiple times (about more than 2-3 times) I usually save the returned value in a local variable. That's because I appreciate program speed more than memory saving. Not that memory wouldn't be important. It just depends on the situations. Calling a function that doesn't take a lot of time to execute isn't time consuming, but a function with several loops being called a large number of times would send your program into long waits.

Upvotes: 1

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361302

I would prefer the first approach. Calling a function repeatedly doesn't seem good to me, especially if the returned value is same everytime. The first approach also avoids the overhead of calling a function repeatedly.

Upvotes: 1

BugFinder
BugFinder

Reputation: 17858

Repeatedly calling any function when the result wont change is a waste, so I would always go with the first method.

Upvotes: 0

Related Questions