简体   繁体   中英

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

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).

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() .

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.

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.

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.

当结果不会改变时反复调用任何函数是一种浪费,所以我总是采用第一种方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM