You can have different naming convention for class members, static objects, global objects, and structs. Some of the examples of them are as below.
对于类成员,静态对象,全局对象和结构,您可以有不同的命名约定。其中一些例子如下。
_member
m_member
or in Java case, the usage of this.member
.
或者在Java情况下,this.member的用法。
But is there any good technique or naming convention for function variables scope that conveys when a single variable has complete function scope or a short lifespan scope?
但是,当单个变量具有完整的功能范围或短寿命范围时,是否存在功能变量范围的任何良好技术或命名约定?
void MyFunction()
{
int functionScopeVariable;
if(true)
{
//no need for function variable scope naming convention
}
}
10 个解决方案
#1
0
We tend to use an l_ prefix in our functions for "local." And, that's worked pretty well.
我们倾向于在“local”函数中使用l_前缀。而且,这很有效。
#2
9
I actually encourage delegating this task to the IDE/editor you use.
我实际上鼓励将此任务委派给您使用的IDE /编辑器。
No, I'm not actually talking about naming variables, that is still best done by a human. But the underlying task of such naming strategies is to show you which type of variable any one name represents.
不,我实际上并没有谈论命名变量,这仍然是人类最好的。但是这种命名策略的基本任务是向您显示任何一个名称所代表的变量类型。
Pretty much every IDE worth its salt can define different styles (colors, fonts, font types, ...) to different variable types (instance member, static member, argument, local variable, ...) so letting the IDE tell you what type of variable it is actually frees you from having to type those (otherwise useless) pre- or suffixes every time.
几乎每个值得盐的IDE都可以为不同的变量类型(实例成员,静态成员,参数,局部变量......)定义不同的样式(颜色,字体,字体类型......),所以让IDE告诉你什么变量的类型实际上使你不必每次都输入那些(否则是无用的)前缀或后缀。
So my suggestion: use meaningful names without any prefix or suffix.
所以我的建议是:使用没有任何前缀或后缀的有意义的名称。
#3
4
One method is to follow the guideline that the larger the scope of the variable, the longer the name. In this way, global variables get long descriptive names while scope-limited things like loop index variable can be as small as single letters.
一种方法是遵循指南,即变量的范围越大,名称越长。通过这种方式,全局变量获得长描述性名称,而范围有限的事物(如循环索引变量)可以像单个字母一样小。
#4
3
I use prefixes or special naming conventions on global, static and member variables so I don't have to use prefixes on locals. I prefer having the option of using short local variable names, especially for loop variables.
我对全局变量,静态变量和成员变量使用前缀或特殊命名约定,因此我不必在本地变量上使用前缀。我更喜欢使用短局部变量名,尤其是循环变量。
#5
1
There's an argument that you shouldn't have 'large scope functions' so there shouldn't be a problem with naming - just use the 'small scope function' variable naming conventions.
有一个论点,你不应该有“大范围函数”,因此命名应该没有问题 - 只需使用'小范围函数'变量命名约定。
#6
1
The guidance from MSFT and other style guides for private instance fields is _memberName
(camel case notation prefixed with "_"
). This is also the convention used in the source code of many recent Microsoft tutorials.
MSFT和私有实例字段的其他样式指南的指导是_memberName(带有“_”前缀的驼峰式表示法)。这也是许多最近的Microsoft教程的源代码中使用的约定。
I use it because it's shorter, not Hungarian, and R# supports it as the default rule for private instance fields.
我使用它是因为它更短,而不是匈牙利语,R#支持它作为私有实例字段的默认规则。
I also like it because it sort of obscures the private fields from Intellisense, as it should, since you should prefer to access your public members first. If I want to access the property Name and I start typing "Na
" the first suggestion is the Pascal-cased public instance property Name. In the rare cases that I want to access the private field directly this forces me to make a conscious decision to start typing "_
", then I get the full list of my private fields in the Intellisense popup.
我也喜欢它,因为它会掩盖Intellisense中的私有字段,因为您应该首先访问您的公共成员。如果我想访问属性Name并且我开始键入“Na”,则第一个建议是Pascal-cased公共实例属性Name。在极少数情况下,我想直接访问私有字段,这迫使我有意识地决定开始键入“_”,然后我在Intellisense弹出窗口中获取我的私有字段的完整列表。
I have also seen guidance that says it should be _MemberName if it is the backing field for a public property named MemberName (Pascal-case notation prefixed with "_
") I personally don't like that because I think the capital M is redundant, adds unnecessary keystrokes and does not add any extra information.
我也看到指导它应该是_MemberName,如果它是名为MemberName的公共属性的支持字段(Pascal-case符号前缀为“_”)我个人不喜欢这样,因为我认为大写M是多余的,添加不必要的击键,不添加任何额外信息。
#7
0
It really all comes down to whatever the style guidelines for the language suggest if there are any.
这真的都归结为语言的风格指导方针,如果有的话。
#8
0
I guess anything is fine as long as it conveys the meaning regarding its use.
我猜任何事情都可以,只要它传达了它的使用意义。
#9
0
I prefer to keep it simple, I use:
我更喜欢保持简单,我使用:
m_varname - Class member variables
g_varname - Global variables
#10
0
I use the same convention I use for class members. The IDE should take care of finding your declaration. If a function is that large and confusing that it becomes a prblem, there is a gretaer issue needs to be addresses.
我使用与类成员相同的约定。 IDE应该注意查找您的声明。如果一个函数很大而且容易混淆,那么它就成了一个问题,那么就需要有一个gretaer问题是地址。
#1
0
We tend to use an l_ prefix in our functions for "local." And, that's worked pretty well.
我们倾向于在“local”函数中使用l_前缀。而且,这很有效。
#2
9
I actually encourage delegating this task to the IDE/editor you use.
我实际上鼓励将此任务委派给您使用的IDE /编辑器。
No, I'm not actually talking about naming variables, that is still best done by a human. But the underlying task of such naming strategies is to show you which type of variable any one name represents.
不,我实际上并没有谈论命名变量,这仍然是人类最好的。但是这种命名策略的基本任务是向您显示任何一个名称所代表的变量类型。
Pretty much every IDE worth its salt can define different styles (colors, fonts, font types, ...) to different variable types (instance member, static member, argument, local variable, ...) so letting the IDE tell you what type of variable it is actually frees you from having to type those (otherwise useless) pre- or suffixes every time.
几乎每个值得盐的IDE都可以为不同的变量类型(实例成员,静态成员,参数,局部变量......)定义不同的样式(颜色,字体,字体类型......),所以让IDE告诉你什么变量的类型实际上使你不必每次都输入那些(否则是无用的)前缀或后缀。
So my suggestion: use meaningful names without any prefix or suffix.
所以我的建议是:使用没有任何前缀或后缀的有意义的名称。
#3
4
One method is to follow the guideline that the larger the scope of the variable, the longer the name. In this way, global variables get long descriptive names while scope-limited things like loop index variable can be as small as single letters.
一种方法是遵循指南,即变量的范围越大,名称越长。通过这种方式,全局变量获得长描述性名称,而范围有限的事物(如循环索引变量)可以像单个字母一样小。
#4
3
I use prefixes or special naming conventions on global, static and member variables so I don't have to use prefixes on locals. I prefer having the option of using short local variable names, especially for loop variables.
我对全局变量,静态变量和成员变量使用前缀或特殊命名约定,因此我不必在本地变量上使用前缀。我更喜欢使用短局部变量名,尤其是循环变量。
#5
1
There's an argument that you shouldn't have 'large scope functions' so there shouldn't be a problem with naming - just use the 'small scope function' variable naming conventions.
有一个论点,你不应该有“大范围函数”,因此命名应该没有问题 - 只需使用'小范围函数'变量命名约定。
#6
1
The guidance from MSFT and other style guides for private instance fields is _memberName
(camel case notation prefixed with "_"
). This is also the convention used in the source code of many recent Microsoft tutorials.
MSFT和私有实例字段的其他样式指南的指导是_memberName(带有“_”前缀的驼峰式表示法)。这也是许多最近的Microsoft教程的源代码中使用的约定。
I use it because it's shorter, not Hungarian, and R# supports it as the default rule for private instance fields.
我使用它是因为它更短,而不是匈牙利语,R#支持它作为私有实例字段的默认规则。
I also like it because it sort of obscures the private fields from Intellisense, as it should, since you should prefer to access your public members first. If I want to access the property Name and I start typing "Na
" the first suggestion is the Pascal-cased public instance property Name. In the rare cases that I want to access the private field directly this forces me to make a conscious decision to start typing "_
", then I get the full list of my private fields in the Intellisense popup.
我也喜欢它,因为它会掩盖Intellisense中的私有字段,因为您应该首先访问您的公共成员。如果我想访问属性Name并且我开始键入“Na”,则第一个建议是Pascal-cased公共实例属性Name。在极少数情况下,我想直接访问私有字段,这迫使我有意识地决定开始键入“_”,然后我在Intellisense弹出窗口中获取我的私有字段的完整列表。
I have also seen guidance that says it should be _MemberName if it is the backing field for a public property named MemberName (Pascal-case notation prefixed with "_
") I personally don't like that because I think the capital M is redundant, adds unnecessary keystrokes and does not add any extra information.
我也看到指导它应该是_MemberName,如果它是名为MemberName的公共属性的支持字段(Pascal-case符号前缀为“_”)我个人不喜欢这样,因为我认为大写M是多余的,添加不必要的击键,不添加任何额外信息。
#7
0
It really all comes down to whatever the style guidelines for the language suggest if there are any.
这真的都归结为语言的风格指导方针,如果有的话。
#8
0
I guess anything is fine as long as it conveys the meaning regarding its use.
我猜任何事情都可以,只要它传达了它的使用意义。
#9
0
I prefer to keep it simple, I use:
我更喜欢保持简单,我使用:
m_varname - Class member variables
g_varname - Global variables
#10
0
I use the same convention I use for class members. The IDE should take care of finding your declaration. If a function is that large and confusing that it becomes a prblem, there is a gretaer issue needs to be addresses.
我使用与类成员相同的约定。 IDE应该注意查找您的声明。如果一个函数很大而且容易混淆,那么它就成了一个问题,那么就需要有一个gretaer问题是地址。