3

I'd like to know in which cases you would prefer to create some kind of "helper" variable to access a value that might be used 2 or more times in a method.

I came around this question during following snipped: what is better: two times accessing the size value of a List, or creating an Integer variable that holds the size of the list?

List<String> list;

private myIndexHelper() {
    if (list.size % 2 == 0) {
        return PREFIX + list.size;
    }
    return "";
}

private myIndexHelper() {
    int size = list.size;
    if (size % 2 == 0) {
        return PREFIX + size;
    }
    return "";
}

I know this might probably be over-optimization. But also in general (neglecting performance): would you rather access the list.size property numerus times, or create a variable that hold the size?

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • 1
    It makes sense when that list.size transforms into half of line of mumbo jumbo method calls. Also, you would rather call a method once, if that method is resource consuming. – CosminO Nov 06 '13 at 16:08
  • 2
    Readability IMHO. For example `int documentLength` makes more sense than `list.size`. – Yury Tarabanko Nov 06 '13 at 16:10
  • 1
    It seems to be against the "don't repeat yourself" principle if you dont do it. – Ingo Nov 06 '13 at 16:15
  • This is an optimization that makes no difference. However, in a multi-threaded design, using `list.size()` might produce different results each time it is called. By using a local reference the result will at least be consistent with the algorithm used. – The Coordinator Nov 11 '13 at 05:55

3 Answers3

2

There us no "better" approach:

  1. when you call list.size or a local size parameter it has the same performance
  2. According to Doug Lea, declaring a local parameter as final might lead to performance improvement
  3. The only aspect which is "easiness of use" is when you want a "shorter" parameter so by using: a local size parameter instead of myListOfReallyNiceIntegers.size would be easier to read/write (with for-loops etc).
Community
  • 1
  • 1
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

In this case I'd say readability, if it's readable then either way is fine. If on the other hand it was

private myIndexHelper() {
    if (dataStricture.subDataStructure.list.size % 2 == 0) {
        return PREFIX + dataStricture.subDataStructure.list.size;
    }
    return "";
}

Then I'd go with the variable approach but otherwise it's no big deal.

If, however this was a loop or time critical method then calling list.size inside the loop (or inside the time critical method) would just waste extra time doing method calls and should be avoided.

Like I said though, in this code, no big deal.

Ross Drew
  • 8,163
  • 2
  • 41
  • 53
0

As there is usually plenty of memory available today it may be good to add a helper-variable, as it may increase readability of the code (although it depends, as I personally find function-calls more readable in such cases - unless they are like object.ref.function(object2.ref2.anotherFunction()) or worse).

I think it is also worth mentioning that sometimes it is a matter of good practice: when you use a value many times in a loop, and this value changes, it is better to have this value stored in a variable rather than re-calculate it every time (in a loop the actual number of processor operations may grow quite quickly.) Good compilers often detect such cases of re-calculating the same value in a loop and create a variable themselves, but it is not a good practice to depend on a compiler in case of efficiency. Especially if you can achieve it by just slightly changing the code.

3yakuya
  • 2,622
  • 4
  • 25
  • 40