用于 C# 代码的 Pascal 套管或 Camel 套管?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/149491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pascal casing or Camel Casing for C# code?
提问by Gustavo Rubio
I've been arguing with my coworkers about Pascal casing (upper camel case) vs. lower CamelCasing. They are used to lower camel casing for everything from table names in SQL databases to property naming in C# code but I like Pascal casing better, lower camel casing for variables and Pascal casing for properties:
我一直在与我的同事争论 Pascal 外壳(大驼峰外壳)与下部CamelCasing。它们用于降低从 SQL 数据库中的表名到 C# 代码中的属性命名的所有内容的驼峰大小写,但我更喜欢 Pascal 大小写,变量的低驼峰大小写和属性的 Pascal 大小写:
string firstName;
public string FirstName {
...
}
But they are used to this:
但他们已经习惯了:
string _firstname;
public string firstName {
...
}
I try to keep up with their "standard" so the code looks the same but I just don't like it.
我试图跟上他们的“标准”,所以代码看起来一样,但我只是不喜欢它。
I've seen that at least the .NET framework uses this convention and that is how I try to keep my code, e.g.:
我已经看到至少 .NET 框架使用这个约定,这就是我尝试保留我的代码的方式,例如:
System.Console.WriteLine("string")
What do you use/prefer and why? I'm sorry if somebody else asked this question but I searched and did not find anything.
你使用/喜欢什么,为什么?如果其他人问了这个问题,我很抱歉,但我搜索并没有找到任何东西。
Update:I've given a method example and not a property but it's the same. As I stated in the first paragraph my colleagues use the Pascal convention for everything (variables, methods, table names, etc.)
更新:我给出了一个方法示例而不是一个属性,但它是相同的。正如我在第一段中所述,我的同事对所有内容(变量、方法、表名等)都使用 Pascal 约定。
采纳答案by Greg Hurlman
I use what the Framework uses, as it's the de-facto best practice. However, so long as the code in your company is consistentlyusing their style, then you're much better off getting used to it. If every developer has their own standard, then there's no standard at all.
我使用框架使用的东西,因为它是事实上的最佳实践。但是,只要您公司的代码始终使用他们的风格,那么您最好习惯它。如果每个开发人员都有自己的标准,那么根本就没有标准。
回答by Dennis S.
I (and my team) prefer to reserve initial capitals for class names.
我(和我的团队)更喜欢为类名保留首字母大写。
Why? Java standards propagating, I think.
为什么?我认为 Java 标准正在传播。
回答by Charles Graham
Pascal casing should be used for Properties. As far as varible names go, some people use _ and some poeple use m_ and some people just use plain old camel casing. I think that as long as you ae consistant here, it shouldn't matter.
属性应该使用 Pascal 大小写。就变量名称而言,有些人使用 _,有些人使用 m_,有些人只使用普通的旧骆驼外壳。我认为只要你在这里保持一致,就没有关系。
回答by Oli
That example of .NET you posted was a function. The adopted "standard" for methods/functions is A capped camel-case (or Pascal, if you want to call it that).
您发布的 .NET 示例是一个函数。方法/函数采用的“标准”是加盖的驼峰式(或 Pascal,如果你想这样称呼它)。
I stick to camel case where I can. It lets you easily know the difference between a variable and a method.
我尽可能坚持使用骆驼套。它让您轻松了解变量和方法之间的区别。
Additionally, I'm a fan of sticking an underscore in front of local class variables. E.g.: _localVar
.
此外,我很喜欢在本地类变量前添加下划线。例如:_localVar
。
回答by Ian G
For public interfaces you should stick with the MS .NET framework design guidelines: "Capitalization Conventions".
对于公共接口,您应该遵循 MS .NET 框架设计指南:“大写约定”。
For non-exposed members then whatever you and your colleagues can agree on.
对于非公开成员,无论您和您的同事都同意什么。
回答by JeeBee
I guess you have to put up with what the coding standard says for your place of work, however much you personally dislike it. Maybe one day in the future you will be able to dictate your own coding standards.
我想你必须忍受编码标准对你的工作场所所说的话,无论你个人多么不喜欢它。也许在未来的某一天,您将能够规定自己的编码标准。
Personally I like databases to use names of the form "fish_name", "tank_id", etc for tables and fields, whereas the code equivalent of the database model would be "fishName" and "tankID". I also dislike "_fooname" naming when "fooName" is available. But I must repeat that this is subjective, and different people will have different ideas about what is good and bad due to their prior experience and education.
我个人喜欢数据库对表和字段使用“fish_name”、“tank_id”等形式的名称,而数据库模型的等效代码是“fishName”和“tankID”。当“fooName”可用时,我也不喜欢“_fooname”命名。但我必须重申,这是主观的,不同的人会因之前的经验和教育而对好与坏有不同的看法。
回答by Joel Coehoorn
A link to the official design guidelinesmight help. Specifically, read the section on Capitalization styles.
官方设计指南的链接可能会有所帮助。具体来说,请阅读有关大写样式的部分。
In the grand scheme of things, Pascal vs Camel doesn't matter that much and you're not likely to convince anyone to go back over an existing code base just to change the case of names. What's really important is that you want to be consistent within a given code base.
从总体上看,Pascal 与 Camel 并没有那么重要,而且您不太可能说服任何人为了更改名称的大小写而返回现有代码库。真正重要的是您希望在给定的代码库中保持一致。
I'm just happy as long as you're not using Hungarian.
只要您不使用匈牙利语,我就很高兴。
回答by dguaraglia
Actually, there's no "standard" convention on this. There's a Microsoft edited guideline somewhere, and as with with any other naming convention guideline, surely there's another one refuting it, but here's what I've come to understand as "standard C# casing convention".
实际上,对此没有“标准”约定。某处有一个 Microsoft 编辑的指南,与任何其他命名约定指南一样,肯定还有另一个反驳它,但这是我所理解的“标准 C# 大小写约定”。
- PerWordCaps in type names (classes, enums), constants and properties.
- camelCase for really long local variables and protected/private variables
- No ALL_CAPS ever(well, only in compiler defines, but not in your code)
- It seems some of the system classes use underscored names (_name) for private variables, but I guess that comes from the original writer's background as most of them came straight from C++. Also, notice that VB.NET isn't case sensitive, so you wouldn't be able to access the protected variables if you extended the class.
- 类型名称(类、枚举)、常量和属性中的 PerWordCaps。
- camelCase 用于非常长的局部变量和受保护/私有变量
- 从来没有 ALL_CAPS (嗯,只在编译器定义中,而不是在你的代码中)
- 似乎有些系统类使用下划线名称 (_name) 作为私有变量,但我猜这来自原始作者的背景,因为它们中的大多数直接来自 C++。另外,请注意 VB.NET 不区分大小写,因此如果您扩展该类,您将无法访问受保护的变量。
Actually, FxCopwill enforce a few of those rules, but (AFAIK) it ignores whatever spelling you use for local variables.
实际上,FxCop会强制执行其中的一些规则,但是(AFAIK)它会忽略您对局部变量使用的任何拼写。
回答by Tanj
I like the coding conventions laid out in the Aardvark'dproject spec
我喜欢Aardvark项目规范中规定的编码约定
回答by Anthony
You should have a look at Microsoft's new tool, StyleCopfor checking C# source code. Also keep an eye on FxCopfor checking compiled .Net assemblies. FxCop focuses more on the details of what the code does, not the layout, but it does have some naming rules related to publicly visible names.
您应该看看 Microsoft 的新工具StyleCop,用于检查 C# 源代码。还要注意FxCop以检查已编译的 .Net 程序集。FxCop 更关注代码功能的细节,而不是布局,但它确实有一些与公开可见名称相关的命名规则。
StyleCop defines a coding standard, which is now being promoted by Microsoft as an industry standard. It checks C# source code against the standard. StyleCop adheres to your PascalCase style.
StyleCop 定义了一种编码标准,现在微软正在将其作为行业标准进行推广。它根据标准检查 C# 源代码。StyleCop 遵循您的 PascalCase 风格。
Getting people onto StyleCop (or any other standard for that matter) can be hard, it's quite a hurdle, and StyleCop is quite exhaustive. But code should be to a uniform standard - and a personal standard is better than none, company standard is better than a personal one, and an industry standard is best of all.
让人们使用 StyleCop(或任何其他与此相关的标准)可能很困难,这是一个很大的障碍,而且 StyleCop 非常详尽。但是代码应该有一个统一的标准——个人标准总比没有好,公司标准总比个人标准好,行业标准是最好的。
It's a lot easier to convince people when a a project starts - team is being formed and there is no existing code to convert. And you can put tools (FxCop, StyleCop) in place to break the build if the code does not meet standards.
当一个项目开始时,说服人们要容易得多 - 团队正在形成并且没有现有的代码可以转换。如果代码不符合标准,您可以放置工具(FxCop、StyleCop)来破坏构建。
You should use the standard for the language and framework - SQL code should use SQL standards, and C# code should use C# standards.
您应该使用语言和框架的标准——SQL 代码应该使用 SQL 标准,而 C# 代码应该使用 C# 标准。