By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,325 Members | 1,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,325 IT Pros & Developers. It's quick & easy.

local var naming style

P: n/a
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.

Oct 7 '07 #1
Share this Question
Share on Google+
25 Replies

P: n/a
On Oct 7, 1:24 pm, guis...@gmail.com wrote:
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.

Oct 7 '07 #2

P: n/a
On Oct 7, 8:51 pm, Justin Spahr-Summers
<Justin.SpahrSumm...@gmail.comwrote:
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

Oct 7 '07 #3

P: n/a
gu*****@gmail.com writes:
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.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Oct 7 '07 #4

P: n/a
On Sun, 07 Oct 2007 18:24:40 +0000, guisep4 wrote:
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.
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Oct 7 '07 #5

P: n/a
On Sun, 07 Oct 2007 19:05:55 +0000, Yakov wrote:
On Oct 7, 8:51 pm, Justin Spahr-Summers
>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 ?
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.

--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Oct 7 '07 #6

P: n/a
On 7 Oct, 19:24, guis...@gmail.com wrote:
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?

Oct 7 '07 #7

P: n/a
On Oct 7, 7:24 pm, guis...@gmail.com wrote:
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

Oct 7 '07 #8

P: n/a
gu*****@gmail.com 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). :-)
--
Tor <torust [at] online [dot] no>

C-FAQ: http://c-faq.com/
Oct 8 '07 #9

P: n/a
Tor Rustad <to********@hotmail.comwrites:
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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Oct 8 '07 #10

P: n/a
On Oct 7, 2:05 pm, Yakov <iler...@gmail.comwrote:
On Oct 7, 8:51 pm, Justin Spahr-Summers
<Justin.SpahrSumm...@gmail.comwrote:
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.
Which would be the position in the array, correct?
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 ?
Others have already commented on the rather complicated rules for
this, but basically... yes, even just one preceding underscore can be
problematic.

Oct 8 '07 #11

P: n/a
Ben:
Tor :
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.

Doesn't everyone program in Courier? ;)

Martin

Oct 8 '07 #12

P: n/a
[comp.lang.c] Tor Rustad <to********@hotmail.comwrote:
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.

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
Oct 8 '07 #13

P: n/a
In article <fe**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@vinland.freeshell.orgwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Oct 8 '07 #14

P: n/a
Christopher Benson-Manica <at***@vinland.freeshell.orgwrote:
[comp.lang.c] Tor Rustad <to********@hotmail.comwrote:
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
Oct 9 '07 #15

P: n/a
[comp.lang.c] Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
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. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
Oct 9 '07 #16

P: n/a
[comp.lang.c] Richard Tobin <ri*****@cogsci.ed.ac.ukwrote:
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.)

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
Oct 9 '07 #17

P: n/a
Christopher Benson-Manica said:
[comp.lang.c] Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
>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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 9 '07 #18

P: n/a
Richard Heathfield wrote:
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?

--
Tor <torust [at] online [dot] no>

C-FAQ: http://c-faq.com/
Oct 9 '07 #19

P: n/a
gu*****@gmail.com wrote:
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
Oct 10 '07 #20

P: n/a
[comp.lang.c] Tor Rustad <to********@hotmail.comwrote:
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).

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
Oct 10 '07 #21

P: n/a
Christopher Benson-Manica wrote:
[comp.lang.c] Tor Rustad <to********@hotmail.comwrote:
>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.

--
Tor <torust [at] online [dot] no>

C-FAQ: http://c-faq.com/
Oct 10 '07 #22

P: n/a
In article <b8*********************@telenor.com>,
Tor Rustad <to********@hotmail.comwrote:
>>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.
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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Oct 10 '07 #23

P: n/a
dada wrote:
gu*****@gmail.com wrote:
.... snip ...
>
>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,
.... 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 11 '07 #24

P: n/a
[comp.lang.c] Richard Tobin <ri*****@cogsci.ed.ac.ukwrote:
In article <b8*********************@telenor.com>,
Tor Rustad <to********@hotmail.comwrote:
(Sorry to piggyback - I can't see Mr. Rustad's post, and given my news
server's history I may never see it.)
>>Perhaps with a background in computational physics, you might have seen
my point.
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. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
Oct 11 '07 #25

P: n/a
<gu*****@gmail.coma crit dans le message de news:
11**********************@y42g2000hsy.googlegroups. com...
>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.

--
Chqrlie.
Oct 20 '07 #26

This discussion thread is closed

Replies have been disabled for this discussion.