local var naming style

G

guisep4

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

Justin Spahr-Summers

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.

According to my local manpage, index() and rindex() are POSIX
functions. If you're programming in strict C90 or C99 (or just not
using POSIX), I wouldn't worry about the shadowing. If you still feel
like you have some reason to, maybe consider renaming the variable to
"pos" or "position" or something with a similar meaning. Including
underscores after variable names is rather odd coding style...
including underscores before variable names can lead to undefined
behavior if you haven't memorized the ins and outs of preceding
underscores in identifiers.
 
Y

Yakov

According to my local manpage, index() and rindex() are POSIX
functions. If you're programming in strict C90 or C99 (or just not
using POSIX), I wouldn't worry about the shadowing.

Yes, I'm coding for POSIX.
If you still feel
like you have some reason to, maybe consider renaming the variable to
"pos" or "position" or something with a similar meaning.

It's not position. It's index into array.
Including
underscores after variable names is rather odd coding style...
including underscores before variable names can lead to undefined
behavior if you haven't memorized the ins and outs of preceding
underscores in identifiers.

Two underscores in front of var name are resrved afaik, not one, no ?
One underscore in front of var name is not reserved, no ?

Yakov
 
B

Ben Pfaff

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

My solution is to not use -Wshadow. It is too noisy, as you can
see.
Which other modification of "index" word would you
suggest to avoid name collision in this case ?

"idx" and "indx" are popular.
 
A

Army1987

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 ?
It is customary for integer loop indices to be called i.
(As for _index, identifiers starting with an underscore are
reserved at file scope, and everywhere if they start with two
underscores or an underscore and a capital letter, so avoid using
them.)

Btw, if you don't need headers such as string.h to declare
functions not in the C standard such as index(), you can compile
with the -ansi switch, which will disable those declarations.
 
A

Army1987

On Oct 7, 8:51 pm, Justin Spahr-Summers

Two underscores in front of var name are resrved afaik, not one, no ?
One underscore in front of var name is not reserved, no ?
7.1.3:
— All identifiers that begin with an underscore and either an
uppercase letter or another underscore are always reserved for
any use.
— All identifiers that begin with an underscore are always
reserved for use as identfiers with file scope in both the
ordinary and tag name spaces.

So, you can use _index at block scope, but you'd better avoid
using identifiers with leading underscores than always trying to
figure out whether you're allowed to use them.
 
G

gw7rib

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 ?

Would calling it "myindex" be too tacky?
 
M

Martin Wells

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 ?


This problem has been faced in human civilisations for millennia. If a
foreigner comes in with the same name as a native, then one of them
must change their name. Obviously, in the case of C, the global name
stays the same, so it's the local that must change. If there's a
foreigner called "index", then maybe go with "local_index" or
"my_index", or just "i".

This assumes that you *actually* use the global variable in your
function. If you don't converse with the foreigner at all, then let
the local stay as "index".

Martin
 
T

Tor Rustad

(e-mail address removed) wrote:

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

For index, I prefer a single letter:

i,j,k,l,m,n, ...

For matrix computations, I liked to loop over "k", so that the resulting
matrix had (i,j). :)
 
B

Ben Pfaff

Tor Rustad said:
For index, I prefer a single letter:

i,j,k,l,m,n, ...

I usually avoid using "l" as a variable name, to avoid confusing
programmers whose fonts make "l" and "1" similar or identical.
 
J

Justin Spahr-Summers

It's not position. It's index into array.

Which would be the position in the array, correct?
Two underscores in front of var name are resrved afaik, not one, no ?
One underscore in front of var name is not reserved, no ?

Others have already commented on the rather complicated rules for
this, but basically... yes, even just one preceding underscore can be
problematic.
 
M

Martin Wells

Ben:
Tor :


I usually avoid using "l" as a variable name, to avoid confusing
programmers whose fonts make "l" and "1" similar or identical.


Doesn't everyone program in Courier? ;)

Martin
 
C

Christopher Benson-Manica

[comp.lang.c] Tor Rustad said:
For index, I prefer a single letter:
i,j,k,l,m,n, ...

I have found that using single letters for loop variables makes
finding their usages with a text editor (as opposed to an IDE)
unnecessarily difficult, and so I prefer idx, jdx, kdx, etc. MMV.
 
R

Richard Tobin

Christopher Benson-Manica said:
I have found that using single letters for loop variables makes
finding their usages with a text editor (as opposed to an IDE)
unnecessarily difficult, and so I prefer idx, jdx, kdx, etc. MMV.

I see your point, but how much searching do you have to save to
outweigh typing all those extra letters?

-- Richard
 
R

Richard Bos

Christopher Benson-Manica said:
[comp.lang.c] Tor Rustad said:
For index, I prefer a single letter:
i,j,k,l,m,n, ...

I have found that using single letters for loop variables makes
finding their usages with a text editor (as opposed to an IDE)
unnecessarily difficult,

Time to get a real text editor, then. Searching on whole words only
should be possible in all of them.

Richard
 
C

Christopher Benson-Manica

[comp.lang.c] Richard Bos said:
Time to get a real text editor, then. Searching on whole words only
should be possible in all of them.

One doesn't always have the luxury of a real editor, unfortunately.
The editor at the company where I picked up this habit was MultiEdit,
which certainly could handle whole word searching, but such was the
coding standard anyway.
 
C

Christopher Benson-Manica

[comp.lang.c] Richard Tobin said:
I see your point, but how much searching do you have to save to
outweigh typing all those extra letters?

I've gotten used to those extra letters to the point where I hardly
notice them. The extra typing overhead is pretty small anyway, since
the 'd' and 'x' are left-hand keys whereas the 'i' and the punctuation
characters that are likely to be involved in this situation are
right-hand keys. (I can't comment on what the situation might be like
on a DVORAK keyboard.)
 
R

Richard Heathfield

Christopher Benson-Manica said:
[comp.lang.c] Richard Bos said:
Time to get a real text editor, then. Searching on whole words only
should be possible in all of them.

One doesn't always have the luxury of a real editor, unfortunately.
The editor at the company where I picked up this habit was MultiEdit,

....which rocks. Even the demo mode of the shareware version (which is
written entirely in MultiEdit macros) rocks.
which certainly could handle whole word searching, but such was the
coding standard anyway.

Yes, it's a sensible standard. If I'm writing a silly little program where
I need a hatful of ints and I know that the program will end up in the bit
bucket within the half-hour, I might conceivably use l as part of an i, j,
k, l, m thing. But I wouldn't do it in "real" code.
 
T

Tor Rustad

Richard said:
Christopher Benson-Manica said:
[...]
which certainly could handle whole word searching, but such was the
coding standard anyway.

Yes, it's a sensible standard. If I'm writing a silly little program where
I need a hatful of ints and I know that the program will end up in the bit
bucket within the half-hour, I might conceivably use l as part of an i, j,
k, l, m thing. But I wouldn't do it in "real" code.

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.

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?
 
D

dada

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.

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.

Joe
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top