Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!abvax!iccgcc!browns From: browns@iccgcc.decnet.ab.com (Stan Brown, Oak Road Systems) Newsgroups: comp.lang.c Subject: Re: a style question Message-ID: <1278.270b06c4@iccgcc.decnet.ab.com> Date: 4 Oct 90 14:54:11 GMT References: <7341@darkstar.ucsc.edu> <8660@ncar.ucar.edu> <1990Sep30.220839.20183@nntp-server.caltech.edu> <1990Oct1.174625.22061@zoo.toronto.edu> <1990Oct2.163853.17004@nntp-server.caltech.edu> Lines: 59 In article <1990Oct2.163853.17004@nntp-server.caltech.edu>, manning@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@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) 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