本地var命名风格 [英] local var naming style

查看:69
本文介绍了本地var命名风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不寻常的问题命名局部变量index。

这一切都是从我向gcc命令行添加-Wshadow [1]开始的。


代码如下:

#include< string.h>

int foo(void){

int index; / *警告这里* /

for(index = 0; index< MAX; index ++){...


带''-Wshadow'' ,这个无辜的代码吐出警告:


file.c:4:警告:声明''索引''阴影全球

声明
/usr/include/string.h:304:警告:阴影声明在这里


我想保留-Wshadow所以我需要重命名本地var。但是我赞成b $ b这个词就像索引一样。在这种情况下。所以我正在寻找合法的

变体,

如_index或index_。大写字母已经出来。我的问题是,

根据常见的unix编码风格(不是匈牙利语而不是
CamelCase),index_或_index是否可以接受?


我建议我们进行民意调查,然后我会计算投票数:-)


index的其他修改在这种情况下,你会这么说吗?b $ b建议避免姓名冲突吗?


[1]

-Wshadow

当局部变量影响另一个局部变量,
参数或全局变量或内置函数为
阴影时发出警告。

解决方案

10月7日下午1点24分,guis ... @ gmail.com写道:


我有不寻常的问题命名局部变量index。

这一切都是从我向gcc命令行添加-Wshadow [1]开始的。


代码如下:

#include< string.h>

int foo(void){

int index; / *警告这里* /

for(index = 0; index< MAX; index ++){...


带''-Wshadow'' ,这个无辜的代码吐出警告:


file.c:4:警告:声明''索引''阴影全球

声明
/usr/include/string.h:304:警告:阴影声明在这里


我想保留-Wshadow所以我需要重命名本地var。但是我赞成b $ b这个词就像索引一样。在这种情况下。所以我正在寻找合法的

变体,

如_index或index_。大写字母已经出来。我的问题是,

根据常见的unix编码风格(不是匈牙利语而不是
CamelCase),index_或_index是否可以接受?


我建议我们进行民意调查,然后我会计算投票数:-)


index的其他修改在这种情况下,你会这么说吗?b $ b建议避免姓名冲突吗?


[1]

-Wshadow

当局部变量影响另一个局部变量,
shadowed时发出警告。



根据我的本地联机帮助页,index()和rindex()是POSIX

函数。如果您使用严格的C90或C99进行编程(或者只是使用POSIX而不是
),我不会担心阴影。如果你仍然觉得你有一些理由,可以考虑将变量重命名为

" pos"或位置或位置。或具有类似含义的东西。包括变量名后的
下划线是相当奇怪的编码风格...

包括变量名前的下划线可导致未定义

行为如果你避风港记住标识符中前面

下划线的来龙去脉。


10月7日晚上8:51, Justin Spahr-Summers

< Justin.SpahrSumm ... @ gmail.comwrote:


根据我的本地联机帮助页,索引()和rindex()是POSIX

函数。如果您使用严格的C90或C99进行编程(或者只是使用POSIX而不是
),我不会担心阴影。



是的,我正在为POSIX编码。


如果你还是觉得

就像你有一些理由,也许可以考虑将变量重命名为

" pos"或位置或位置。或具有类似含义的东西。



这不是位置。它是数组的索引。


包括

变量名后面的下划线是相当奇怪的编码风格...


包括变量名之前的下划线可能导致未定义

行为如果你没有记住前面的来龙去帖

在标识符中下划线。



var名称前面的两个下划线是afaik,而不是一个,不是吗?

var名称前面的一个下划线没有保留,不是吗?


雅科夫


gu ***** @ gmail.com 写道:


使用''-Wshadow'',这个无辜的代码吐出警告:


file.c:4:警告:声明''索引''影响全球

声明

/ usr /include/string.h:304:警告:阴影声明在这里



我的解决方案是不使用-Wshadow。它太吵了,因为你可以看到。
$ blockquote class =post_quotes>
对index的其他修改在这种情况下,你会建议避免名字冲突吗?



" idx"和indx很受欢迎。

-

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)开关(0 [p]& 3)情况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;}}}


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.

解决方案

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.


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


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;}}}


这篇关于本地var命名风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆