命名局部常量:UpperCamelCase 还是 lowerCamelCase?
在 C# 中,您对本地常量使用哪种命名约定?为什么?
const int Pi = 3;
const int pi = 3;
似乎需要在表示范围有限的小驼峰式命名法和更具可读性且更容易迁移到类级别的大驼峰式命名法之间进行权衡。我注意到 StyleCop 更喜欢大驼峰式。
Which naming convention do you use for local constants in C# and why?
const int Pi = 3;
const int pi = 3;
It seems the trade-off is between lower camel-case indicating restricted scope, and upper camel-case being more readable and easier to move to a class level. I've noticed StyleCop prefers upper camel-case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们使用小写(驼峰式大小写),因为局部常量几乎是局部变量,当然你不能修改它们。 (当然,我们对局部变量使用驼峰式大小写......)
We use lower-case (camel casing) because local constants are almost local variables except of course you cannot modify them. (And we use camel casing for local variables of course...)
我习惯于除了变量和字段之外的所有内容都使用大写(帕斯卡大小写)。全局常量是这些字段的例外,我不知道为什么,可能是因为它们在某些情况下是公共的。局部常量也是小写的。
在我看来,这只是一个品味问题。当然,在产品/团队内部,应该有一个协议。
另一方面,我们的编码指南要求常量全部大写,在本例中为
PI
。我不喜欢这样,因为大写字母很难阅读并且需要下划线来分隔(这违反了代码分析规则)。没有人再遵循这个指导方针了。I'm used to upper case (pascal case) for everything except variables and fields. Global constants are an exception to the fields, I don't know why, probably because they are public in some cases. Local constants are also lowercase so.
It's just a matter of taste imo. Of course, within a product / team, there should be an agreement.
On the other hand, our coding guideline requires full uppercase for constants, this would be
PI
in this case. I don't like this because upper cases are hard to read and need underlines for separation (which is against code analysis rules). Nobody follows this guideline anymore.