开发者

Call method/function twice Vs. Saving into a variable and call once

I often come to this question when coding.

Which of the following examples is a better practice? I am aware that other factors will influence whether one or the other one is better. But in general, what are the advantages of one over the other.

if(object.getA().Value != null) {
    return object.getA().Value;
}
return null;

Vs.

string x = string.null;
x = object.getA().Value;
return (x != null) ? x : null;

Here is another similar example:

var a = object.metho开发者_运维知识库d(x).Value;
var b = object.method(x).Key;

Vs.

var y = object,method(x);
var a = y.Value;
var b = y.Key;

In other words my question is:

Is it better to call a method twice and have one less variable? or Is it better to save it into a variable and call the method twice?

Of course if the method results in a lot of processing it might be smart to call it once, but for general cases where the method is not too demanding and the space of the variable is not too big, which one is better and why? or which are the advantages of one or the other?

The difference between them might not make a big difference but I am trying to find better practices and will like to hear the input of some experienced programmers.

Many thanks


Caching the value in a variable is a basic optimization (related to memoizing).

When it becomes truly necessary is if the second call to the function is on the stack a significant percent of the time.

For example, if that second call is on the stack 10% or 20% of the time, then that's how much overall time you can save by caching the first result.

You can keep doing things like that, until the code is as fast as possible.

If I can give an example, ages ago I worked on an app that had code like this:

if (!Done()){
  do some stuff
}
....
if (!Done()){
  do some other stuff
}

Since Done() was such a short, clean, and simple function to call, it got called a lot. Never mind that it did a lot including querying a ton of stuff from a DB and throwing most of it away. Stackshots found the problem instantly.


It depends if you want to be thread safe and if the function could change between calls.

e.g. with

if(object.getA().Value != null) {
    return object.getA().Value;
}
return null;

if the implementation of the property getter Value returned a null on the second call you would have a different answer. It could return null on the second call either by implementation of the method or if another thread casused an update between the if and the return statement that made the result of the property null.

This test is actually redundant because you are returning null if it is null. I'm guessing that you meant if (object.getA() != null). Then the previous paragraph still applies but to getA() instead of Value but the if body would throw an null reference exception if getA() returned null on the second call.

So its all down to whether you are worried about the values changing between calls.


General rule: Avoid extra variables (needlessly introduces states). (Break the rule if calling the function twice adds too much overhead)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜