a style question

Stan Brown, Oak Road Systems browns at iccgcc.decnet.ab.com
Fri Oct 5 00:54:11 AEST 1990


In article <1990Oct2.163853.17004 at nntp-server.caltech.edu>, manning at nntp-server.caltech.edu (Evan Marshall Manning) writes:
> I wrote:
>>Don't ever use single-character variable names.  I use 'ix' for
>>loop index variables for which I can come up with no better name...
> 
> henry at zoo.toronto.edu (Henry Spencer) writes:
>>In what way is "ix" superior to "i"?  Both are meaningless names in
>>this context.  If the name is not going to be meaningful -- and in the
>>context of a short loop, it's not clear that making it meaningful is
>>either possible or particularly important -- then at least keep it short.
> 
> If Henry asks it must not be as self evident as I thought.  Hasn't this
> ever happened to you:  You're thinking that you could apply what you
> just learned on the net and speed up this time-critical bit of code by
> changing the original cout-up loop to a count-down loop.  "But wait," you
> say, "What if some later code depends on the value of 'i' after exiting
> the loop?  I'll just search for the letter 'i' through 100 lines of
> source."  You find that a lower case 'i' appears an average of about
> four times per comment line.  'ix' appears in the comments only when
> the comments are about variable 'ix'.  Or, similarly, you 'grep' 'i'.

I think the question here is not, "Should one-letter loop variable names
be allowed?"  The question, IMO, is "Should variable names (including
names of loop variables) be meaningful?"  If the counter is used only
within the context of the loop, _especially_ as an array subscript, then
I think "i" is a _good_ choice, not just an acceptable one.  Why? 
Because since Algebra I we've all been used to "i" as a subscript for
things like summation, where the result is some property of the array. 
But if the counter is used outside the loop, then I think "i" is a _bad_
choice.

Examples I would approve:
1.   for (total=0,i=0; i<array_size; ++i)
        total += array[i];
     (I think something like "array_index" instead of "i" would be worse.)

2.   for (location=0; location<array_size; ++location)
        if (array[location] == value_searched_for)
           break;
     if (location >= array_size)
        printf(... error message
     (In a real context, I'd use something more meaningful than
     "location".  But you get the idea)

BTW, I know 2 could be coded shorter in a one-line "for" followed by a
null statement, like this:

     for (location=0; location<array_size && array[location]!=value_searched_for; ++location)
        ;

But it obscures the marching through the array, it won't all fit on an
80-column line, and it requires me to test for the negation of the
condition I'm actually interested in.  And it's no more efficient since
it generates the same code as what I typed above.  So to my mind it's
inferior to what I typed in 2. above.

The above is my own opinion and not attributable to any other person or
organization.                        email: browns at iccgcc.decnet.ab.com
Stan Brown, Oak Road Systems, Cleveland, Ohio, U.S.A.    (216) 371-0043



More information about the Comp.lang.c mailing list