Re: For Loops and Variables



Eric Sosman wrote:
Jason Cavett wrote On 04/05/07 11:47,:
This was a discussion my co-worker and I had awhile back and I was
curious what the group thought.

for(int i=0; i < someValue; i++) {
// do stuff
}

The discussion was that the use of the variable "i" was not good as it
should be named something that means something (arrayCounter or
whatever) so it is easier to understand the code. The reverse
argument was that using the "i" variable as a counter was an okay
practice and it is alright as long as it is the only variable doing
this (all other variables are well named). Convenience of typing "i"
versus "someLongVariableName" was the argument on this side.

I think the focus should be on convenience of reading, not convenience
of typing, but agree with the conclusion. I also think that if i is a
simple loop index, it is extremely well named.


....
When I myself read code, it seems easier to read short names
than long ones. On the other hand, short names can be cryptic;
my reading is not aided if I need to keep interrupting it to go
look for a comment on a variable declaration a hundred lines
distant. I've found that a pretty reasonable balance is struck
if I let the length of a variable name depend on the "size" of
its scope. I'll use short names for variables that are declared,
used, and abandoned in a "small" region of code, and longer names
for variables with more "staying power" whose visibility and
significance extend over wider spans.

I consider two things, size of scope and whether a longer identifier
would add significantly to the clarity of the code. Even in a small
scope, I prefer a slightly longer identifier if I have something worth
saying about the meaning of the variable.

For example, to find the dot product of two arrays:

double dotProduct = 0;
for(int i=0; i<input1.length; i++){
dotProduct += input1[i] * input2[i];
}
// use dotProduct.

There really isn't anything worth saying about i that is not already
said by how it is declared, and its scope is so short that any reader is
going to have that declaration in front of them when looking at a use.
There are many purposes that could lead to "double xxx = 0;", so even in
a short scope, it is worth using the identifier to indicate the purpose
of this particular initially-zero double.

Patricia
.



Relevant Pages

  • compiling kernel
    ... scripts/kconfig/qconf.h:51: error: `e' was not declared in this scope ... scripts/kconfig/qconf.h:73: error: `int updateList' redeclared as different ... scripts/kconfig/qconf.h:8: error: forward declaration of `class ConfigList' ... ConfigLineEdit' ...
    (alt.os.linux.suse)
  • Re: Check whether an argument in command line is a number
    ... as part or all of their makeup: The compound statement in its entirety ... declarators of ordinary identifiers with block scope, ... each time the declaration is reached in the order of execution, ... also a block whose scope is a strict subset of the scope of the ...
    (comp.lang.c)
  • Re: Declaration statement
    ... as declarations appearing at file and block scope... ... other declaration overrides it -- because the rules of the C ... way, and in the days before the first C standard (ANSI X3.159-1989, ... different C compilers had different rules. ...
    (comp.lang.c)
  • Re: type/rank mismatch error
    ... | procedures that are *NOT* in the same scope as the USE statement. ... See host association. ... declaration overrides the host-associated declaration. ...
    (comp.lang.fortran)
  • Re: Help on testing/picking an oscilloscope
    ... > My friend found for me what appears to be a cheap oscilloscope on sale ... > scope would display two traces from the other module. ... > Is it even worth considering or way too old to spend any money on? ...
    (sci.electronics.equipment)