a style question

Mark A Terribile mat at mole-end.UUCP
Sat Oct 20 14:16:41 AEST 1990


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

> >In what way is "ix" superior to "i"?  Both are meaningless names ...
> >If the name is not going to be meaningful ... then at least keep it short.
 
 
> My attitude is just the opposite of yours: to me, the shorter the loop
> is, the less excuse there is to choose a name because of it's length.
> If you're only going to be typing the name three or four times, what's
> the advantage keeping it a single character?  Readability?


This is a tough case to argue, but I'll take it up.

Put simply, data in a program allow long-range communication between one
part of the program and a (potentially) textually unrelated part.  In this
respect it differs from structured flow-of-control operations, which define
relationships between textually related parts (all cases except ``invokes,''
e.g. function call) or tightly limit the effect of remote code (``invokes,''
which must return).

In reading a variable name, you have to mentally relate the operation being
performed (read, write, compute a value for or with ...) with the defined
meaning of the variable and the likely interpretation of the variable.  It's
work and it slows you down.

For a variable used in a small context (like a loop) there are two other
ways of achieving the understanding you need when you read the name of
the variable in code.  First, a variable whose name is excruciatingly short
forces you to look to the other uses of the variable for its interpretation.
If the entire scope of the variable fits on a screen or a page (and I *LIKE*
blocks whose only purpose is to limit such variables) and involves not many
other variables, this may actually be faster and easier than the ``guessing''
process by which you interpret a variable name.

The rapid interpretation of a variable from its context in a very small scope
depends heavily on there not being more than one or two such variables in
that scope.

For a short variable that is used as the iteration variable of a loop (integer
progression, list walking, etc.,) the strength of the common idiom may also
help to make the variable's use and meaning clear.  Again, this generally
only works if the code fits on a screen or a page.

This latter effect is strong enough that in a somewhat larger loop, it may
actually be clearer to use a variable like  ``i_record'' instead of
``record_num''.
-- 

 (This man's opinions are his own.)
 From mole-end				Mark Terribile



More information about the Comp.lang.c mailing list