Question

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?

Was it helpful?

Solution

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]++;
// ...

OTHER TIPS

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
...
*/

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top