local var naming style

C

Christopher Benson-Manica

[comp.lang.c] Tor Rustad said:
Why people want to focus on an index, I don't get. The important thing
is the object name, i.e. the vector/array/matrix etc. IMO, a single
letter index make a complex constructs/formula far more readable.

It makes it more likely to fit on one line, but I personally find that
readibility is directly proportional to the ratio of letters to
punctuation (up to a certain point, of course). I also find that
variables named "idx" and such clearly identify themselves as loop
control variables; a variable "i" could be anything.
I have sometimes committed the sin of using 'idx', but it doesn't make
much of a difference on bad editors for me, why should we search for an
index really?

To make sure you haven't inadvertently done something silly with it
(as I have done on a regrettable number of occasions).
 
T

Tor Rustad

Christopher said:
[comp.lang.c] Tor Rustad said:
Why people want to focus on an index, I don't get. The important thing
is the object name, i.e. the vector/array/matrix etc. IMO, a single
letter index make a complex constructs/formula far more readable.

It makes it more likely to fit on one line, but I personally find that
readibility is directly proportional to the ratio of letters to
punctuation (up to a certain point, of course).

Perhaps with a background in computational physics, you might have seen
my point.
 
C

CBFalconer

dada said:
(e-mail address removed) wrote:
.... snip ...

.... snip ...

I'll probably get dinged for this, but I like to use names that
mean something, like rowIndex or colIndex, radioIndex, etc,
especially if there are more than one index in the same bit of code.

What should be recognized is that the shadow warning is totally
meaningless UNLESS the shadowed index variable is used within the
foo function.
 
R

Richard Tobin

It makes it more likely to fit on one line, but I personally find that
readibility is directly proportional to the ratio of letters to
punctuation (up to a certain point, of course).
[/QUOTE]
Perhaps with a background in computational physics, you might have seen
my point.

It's certainly true that in mathematics the use of single letters for
variables is almost invariable, often using the same letter in
different fonts for different purposes. Partly no doubt this is
related to the use of simple adjacency for multiplication and
composition, which would be ambiguous with multi-letter names.

-- Richard
 
C

Christopher Benson-Manica

[comp.lang.c] Richard Tobin said:

(Sorry to piggyback - I can't see Mr. Rustad's post, and given my news
server's history I may never see it.)

I've always disliked the math involved in physics, so I am happy to
concede the point to you. In any case, there's probably no more
ground to cover on this particular religious matter.
 
C

Charlie Gordon

I have unusual "problem" with naming local variable "index".
It all began when I added -Wshadow [1] to gcc commandline.

The code is like this:
#include <string.h>
int foo(void) {
int index; /* WARNING here */
for(index = 0; index < MAX; index++) { ...

With '-Wshadow', this innocent code spits the warning:

file.c:4: warning: declaration of 'index' shadows a global
declaration
/usr/include/string.h:304: warning: shadowed declaration is here

I want to keep -Wshadow so I need to rename the local var. But I
like the word "index" in this case. So I am looking for a legal
variation,
like _index or index_. Uppercase letters are out. My question is,
according to the common unix coding style (not hungarian and not
CamelCase), would index_ or _index be acceptable ?

I suggest we take a poll and I'll count the votes afterwards :)

Which other modification of "index" word would you
suggest to avoid name collision in this case ?

[1]
-Wshadow
Warn whenever a local variable shadows another local variable,
parameter or global variable or whenever a built-in function is
shadowed.

Faced with the very same issue, I have come up with an ugly hack confined to
the Makefile:

CFLAGS += -Wshadow -D"index(s,c)=index__(s,c)"

I know it is ugly to the max, but I fixes the problem and lets me name local
variables ``index'' if I want. I never use the POSIX index string function,
I use Standard C strchr instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top