1

What's the best way to read multiple numbers/strings in one array/struct/union, which itself is nested in one or more parent arrays/structs/unions?

1st example without temporary variable:

printf("%d %d\n", a[9][3], a[9][4]);

1st example with temporary variable:

int *b = a[9];
printf("%d %d\n", b[3], b[4]);

The temporary variable in the first example above is quite silly I'm sure, but in the second example below it makes sense and looks better to use one, right?

2nd example without temporary variable:

foo[i]->bar.red[j][0]++;
foo[i]->bar.red[j][1]++;
foo[i]->bar.red[j][2]++;
foo[i]->bar.red[j][3]++;

2nd example with temporary variable:

int *p = foo[i]->bar.red[j];
p[0]++;
p[1]++;
p[2]++;
p[3]++;

So where do you draw the line? I realize that compilers are smart enough to insert any indirection needed to produce assembly of optimal efficiency in this way, but (hypothetically assuming extremely performance critical code) maybe there are exceptions? And from a code clarity/maintainability point of view, what's your rule of thumb, if any?

Eldar Abusalimov
  • 24,387
  • 4
  • 67
  • 71
  • 1
    Your first example is interesting, because when I read the first version I assumed that `a` was a two-dimensional array of `int`s, whereas the second version makes obvious that `a` must be a one-dimensional array of pointers to arrays of `int`s. (This is also potentially true in the second example, but by the time I got there I'd already read the whole first example, so I don't know what I would have assumed otherwise.) But I don't think that, in itself, is a reason to use a temporary variable. Hopefully the nature of `a` or `foo[i]->bar.red` would be clear in context, anyway! – ruakh Jan 06 '12 at 03:56
  • Oh, hrm, weird. O.K., yeah, that makes sense. Somehow I find that very unnatural -- my instinct is to write `int *b = &a[9][0];` -- but your version is totally valid. Never mind. :-) – ruakh Jan 06 '12 at 15:12
  • Yeah, it makes sense, and I'm very much used to (say) `int d[20]; int *e = d;` and even `int a[20][20]; int (*f)[20] = a;`. Just somehow it never occurred to me to let something like `a[10]` decay to a pointer. But my instincts are probably just weird and atypical; all my *team* development experience is in other languages, so the only C++ idioms I'm used to seeing are the ones I use myself. Please ignore me. :-) – ruakh Jan 07 '12 at 03:50

3 Answers3

3

First, I believe that this is purely a matter of code readability/maintainability, that is an individual preference. Compilers are smarter than us today (joke). :-)

Personally I usually think about extracting a temporary variable (or a function, this applies to them as well) in two cases:

  • When a can name it so that its name is self-explanatory and tells more than initial expression, or
  • If I have to repeat some piece of code at least three times.

Thus, your first example I'd leave as is:

printf("%d %d\n", a[9][3], a[9][4]);

And the second one would be:

int *p = foo[i]->bar.red[j];
p[0]++;
// ...

Or, (often, but not always) better:

int *bar_red = foo[i]->bar.red[j];
bar_red[0]++;
// ...
Eldar Abusalimov
  • 24,387
  • 4
  • 67
  • 71
2

My rule of thumb is : add a temporary variable if you can give it a clear a meaningful name that makes the code easier to understand. A second advice would be : enclose the (small) part that uses this variable in braces. This way the intent is clear, and the code is almost ready if a refactoring is needed (the code in braces will be easy to,extract into a function).

In your second sample, this would lead to:

/*previous code
...
*/
int *quadruplet = foo[i]->bar.red[j];
{ /*intentionaly meaningless braces that prepare the introduction 
  of a function (incrementQuadruplet) in a later refactoring if needed*/
  quadruplet[0]++;
  quadruplet[1]++;
  quadruplet[2]++;
  quadruplet[3]++;
}
/*following code
...
*/
Pascal T.
  • 3,866
  • 4
  • 33
  • 36
1

This doesn't answer your Question in an exact manner but I cant help ut stress on this since this is a trap most programmers fall too often.

Rule of thumb is: Avoid premature optimizations.
Modern day compilers are celever enough to perform trivial optimizations on their own.
Write a code which is easy to understand, maintain and which follows coding standards/practices of the team you work in.
Get your working correctly as per the requirememnt, Profile your code for bottle necks and only then try to optimize the code which is found to be the bottle neck.

Alok Save
  • 202,538
  • 53
  • 430
  • 533