属性“ID”的正确命名约定是什么? : 身份证还是身份证?

发布于 2024-07-08 13:44:47 字数 279 浏览 7 评论 0原文

非常简单的问题:当我有一个持久对象时,它通常有一个名为 ID 的属性(对于抽象类)。

那么..命名约定是ID还是Id?

例如。

public int ID { get; set; }

public int Id { get; set; }

干杯:)

PS。 顺便说一句,这是针对 .NET 的。 FXCop 一致性将是一个额外的好处。

Pretty simple question: When i have a persistable object, it usually has a property called ID (for abstract classes).

So .. is the naming convention ID or Id?

eg.

public int ID { get; set; }

or

public int Id { get; set; }

cheers :)

PS. This is for .NET btw. FXCop conformat would be a bonus.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

念三年u 2024-07-15 13:44:47

我通常使用标识符。 如果我确实想保持简短(例如,作为较长标识符的一部分),我会使用 Id,除非它是参数或私有成员。

.NET Framework 命名指南这样说:

首字母缩略词是由术语或短语中单词的字母组成的单词。 例如,HTML 是超文本标记语言的缩写。 仅当缩写词广为人知且易于理解时,才应在标识符中包含缩写词。 首字母缩写词与缩写词的不同之处在于缩写词会缩短单个单词。 例如,ID是标识符的缩写。 一般来说,库名称不应使用缩写。

可在标识符中使用的两个缩写是 ID 和 OK。 在 Pascal 大小写的标识符中,它们应显示为 Id 和 Ok。 如果用作驼峰式标识符中的第一个单词,它们应分别显示为 id 和 ok。

I usually go with Identifier. If I really want to keep it short (as part of a longer identifier, for example), I use Id, unless it's a parameter or private member.

The .NET Framework Naming Guidelines say this:

An acronym is a word that is formed from the letters of words in a term or phrase. For example, HTML is an acronym for Hypertext Markup Language. You should include acronyms in identifiers only when they are widely known and well understood. Acronyms differ from abbreviations in that an abbreviation shortens a single word. For example, ID is an abbreviation for identifier. In general, library names should not use abbreviations.

The two abbreviations that can be used in identifiers are ID and OK. In Pascal-cased identifiers they should appear as Id, and Ok. If used as the first word in a camel-cased identifier, they should appear as id and ok, respectively.

柠檬心 2024-07-15 13:44:47

无论你喜欢什么,只要保持一致即可。 ID不是一个单词,而是身份的缩写。 如何写缩写是人们争论了很长时间的一个问题。 例如,它

getResourceURL

getResourceUrl

实际上两者都可以在不同的框架中使用。 另一个流行的缩写。 UTF8 也有类似问题。

保持一致很重要,因为否则人们总是必须查找每个方法的正确大小写,如果每个方法都以不同的方式处理它。

我对此有自己的惯例。 如果缩写。 位于名称末尾,全部大写,如果在其他地方,则遵循驼峰表示法规则。 例如

getResourceURL
urlOfResource
compareUrlToString

为什么? 我喜欢缩写。 要大写。 大多数人希望 URL 或 UTF 是大写的。 但是,如果位于名称中间,则会破坏骆驼表示法的优势。 驼峰表示法的优点是您可以通过大写看到新单词的开头位置。 所以比较一下:

compareURLToString
compareUrlToString

在第一种情况下,我没有立即看到 URL 是一个单词而 To 是下一个单词。 T 可能是 URL (URLT) 的一部分并且是不同的缩写,因此我将使用第二种形式。 然而,如果它在最后,它就不会发挥作用,后面没有其他单词,因此我更喜欢大写形式。 我在所有代码中都遵循这一约定。

Whatever you like it to be, just be consistent. ID is not a word, it's an abbreviation of identity. How to write abbreviations is something people argue about for a long time. E.g. is it

getResourceURL

or

getResourceUrl

actually both can be found in use in different frameworks. Another popular abbr. with similar problem is UTF8.

It's just important to be consistent, because otherwise people always have to look up the correct capitalization for every method, if every method handles it in a different way.

I have my own convention for that. If the abbr. is at the end of the name, it is all capitalized, if it's somewhere else, it follows camel notation rules. E.g.

getResourceURL
urlOfResource
compareUrlToString

Why? I like abbr. to be capitalized. Most people expect URL or UTF to be capitalized. However, if in the middle of a name, it destroys the advantage of camel notation. The advantage of camel notation is that you see where a new word starts by capitalization. So compare:

compareURLToString
compareUrlToString

In the first case, I don't see immediately the URL is one word and To is the next one. The T might be part of URL (URLT) and be a different abbr., thus I'll use the second form. If it's at the end however, it won't play a role, no other word follows, thus I prefer the capitalized form. I stick to this convention throughout all of my code.

酒绊 2024-07-15 13:44:47

指南上写着“Id”。

幸运的是,.Net 的人很友善地为我们提供了“命名指南”而不是“命名法则”;),所以基本上,如果您强烈认为违反指南规则确实比遵循它增加了更多价值,那么无论如何都要打破它,如果他们理解你的原因(比如它提供了更多的可读性并强调了名字的含义),没有人会追究你的责任。

在我的店里,即使指南说“Id”,我们也使用“ID”,但没有人,真的对此感到内疚。

The guidelines say "Id".

Fortunately the the .Net guys were kind enough to give us "naming guidelines" and not "naming laws" ;), so basically if you strongly feel that breaking a guideline rule does add more value than following it, then by all means break it, no one will take you accountable for it if they understand your reasons (like it provides more readability and accentuates the meaning of the name)

In my shop we use 'ID' even if the guidelines say 'Id', but no one, really feels guilty about it.

平生欢 2024-07-15 13:44:47

正如其他人指出的那样,根据 .NET 约定,Id 是正确的,但不知何故感觉不太对,所以很多人使用 ID(并且仍然活着)。

我不知道您的字段的内容是什么(数字、字符串、guid),但如果您想跳过这个问题,您可以简单地使用另一个 .NET 约定作为对象标识符:名称

As others pointed out, Id is right according to .NET conventions, but somehow it doesn't feel right, so many people use ID (and still live).

I don't know what the content of your field will be (numbers, strings, guids), but if you want to skip the problem you may simply use another .NET convention for object identifiers: Name

墨离汐 2024-07-15 13:44:47

我们在内部使用 ID,因为 Id 在我看来就像心理学的说法,但 FxCop 抱怨道,因为 ID 不是缩写词,它是缩写(Identity/Identifier)。 根据 FxCop 规则,只有两个字符长的首字母缩略词才允许全部大写。 其他一切都应该是正确的情况。

我们在 ID 属性上添加了 SuppressMessage 属性,每个人都很高兴。

We use ID internally, because Id seems to me like psychology speak, but FxCop complains, as ID is not an acronym, its an abbreviation (for Identity/Identifier). According to the FxCop rule, only acronyms that are two characters long are allowed to be all caps. Everything else should be Proper case.

We put a SuppressMessage attribute on the ID property, and everyone is happy.

悸初 2024-07-15 13:44:47

“ID”是 FxCop 命名规则中硬编码的缩写之一,即使它们是其他单词的一部分,也始终被视为拼写错误。 我发现覆盖它的唯一方法是将“ID”添加到缩写词中作为 ():

<Dictionary>
<Acronyms>
   <CasingExceptions>
        <Acronym>ID</Acronym>
  </CasingExceptions>
</Acronyms>
</Dictionary>

它与 FxCop 1.36 一起使用。
PS 总的来说,我认为“Id”是一个更好的选择,但有时如果名称是基于我们无法控制的 XML 文件(或 Web 服务)生成的,则无法更改名称

"ID" is one of abbreviations hardcoded in FxCop naming rules that are always considered wrong spelled even if they are parts of other words. The only way I found to override it is adding "ID" to Acronyms as ():

<Dictionary>
<Acronyms>
   <CasingExceptions>
        <Acronym>ID</Acronym>
  </CasingExceptions>
</Acronyms>
</Dictionary>

It worked with FxCop 1.36.
P.S. In general, I think that "Id" is a better alternative but sometimes it's impossible to change a name if it's generated based on XML files (or web services) which we don't control

哭泣的笑容 2024-07-15 13:44:47

我更喜欢 ID,也可以与其他单词(例如 CustomerID)结合使用,但严格来说,这违反了常规命名约定。

I prefer ID, also in combination with other words (e.g. CustomerID), but strictly speaking that is against the regular naming conventions.

风吹雪碎 2024-07-15 13:44:47

我们在这里将“Id”或其他缩写词视为一个单词,“Id”是最简单的一个,因为在银行业务中我们确实会遇到很多奇怪的字母组合。 因此,我们将其视为一个单词,仅将第一个字母大写,这就是我们最终在内部编码约定文件中定义的方式。

但底线是:选择哪种约定对开发团队和其他查看源代码并坚持使用的人来说更舒服。

We around here see things like "Id" or other acronyms as a word, and "Id" is the easy one because in the banking business we do get lots of strange letter combinations. So we treat it as a word, only with the first letter capitalized, and that's how we ended up being defined in our internal coding conventions paper.

But the bottom line is: choose what convention is more comfortable for the developer team and other people that look at the source code and stick with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文