Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In C and C++ (and I guess other languages that allow taking a "reference" to an array element or something similar), when you have an array like type, accessing individual elements of such an array can be done "directly" or via a temporary reference or pointer.

Example:

// Like this:
for(int i=0; i<n; ++i) {
   theArray[i].frobnicate();
   theArray[i].shrub();
   if(i % 2) {
      theArray[i].modulize();
   }
}

// Or like this:
for(int i=0; i<n; ++i) {
   TheType& elem = theArray[i];
   elem.frobnicate();
   elem.shrub();
   if(i % 2) {
      elem.modulize();
   }
}

Obviously if the loop body becomes too complex some refactoring should be used and the whole access moved to a separate function/method that would naturally operate on a single element of the array, but for cases where there are few acesses that are only used in one loop, it may be more convenient to keep them directly in the loop.

We have been debating which form we should prefer in our team and opinions are mixed, therefore I'm interested in whether there exist some guidelines, especially wrt. C++.

Edit: This is purely about style if you will. Performance is irrelevant, as our C++ compiler actually should generate exactly the same code in both versions.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

Each approach has advantages.

The temporary reference approach has the advantage that you get to name the reference. This can provide additional information to help make the code more self-explanatory. It also eliminates having multiple instances of the same expression to access the variable.

The no-reference approach is one-line shorter. And if you only access the member once or twice, the benefit of eliminating multiple instances is much less.

I've written coding practices documents. If you feel you must have a policy on this, I would suggest noting the advantages of each and recommending the no-reference approach where the code is simple and the reference approach where either the expression would be repeated many times or having the reference named adds to code readability.

You don't have to have a rule for everything, especially where there's no clear major advantage of either approach and no specific problem you're trying to avoid.

share|improve this answer

There is nothing wrong with taking those temporary references - in fact it may be preferable in some cases because you can name the reference. If that particular reference has a particular role that may not be immediately obvious from the array index then the name will add clarity.

If the discussion centers around "performance", then the guideline is the same as for most micro-optimizations: try it, profile it and/or look at the emitted assembly code. Make your decisions based on hard facts there, not on fuzzy guidelines.

share|improve this answer
    
+1 for naming, and (if I could) +1 for “profile it”. –  Donal Fellows Aug 10 '11 at 14:32
    
+1, in any language if I'm iterating through an array (or more than one) by an i indexed for loop then the first thing I generally do in the loop body is assign the elements at i to named reference or pointer variables (usually not copies). That way you know where you are. 99.99% of the time the performance delta will be zero irrespective of what your compiler does. –  user23157 Aug 10 '11 at 15:39

It isn't a style I've often seen (even if it remembers me the Pascal's with), but I think it is just a question of familiarity, I've nothing strong against it. Two points:

  • it seems fragile if the index or the array is modified during the loop, that I tend to consider such modification worse than that use of reference,

  • it is near in effect to the new for syntax in C++0X -- I'd not be surprised seeing in the future macros expanding to something like this or the new for syntax for the sake of an easy transition.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.