身份证,身份证,还是身份证?
我在代码和数据库字段名称等中使用了驼峰命名法,但是对于末尾有 Id 的字段,它总是难以阅读。 例如,itemId
、teacherId
、unitId
等。在这些情况下,我考虑打破约定并编写 itemID、teacherID 或 unitID
只是为了提高可读性。
您会做什么以及处理此问题的一般最佳实践是什么?
I use camelCase
in my code and database field names, etc, but with fields that have Id
at the end, it always ends up being hard to read. For example, itemId
, teacherId
, unitId
, etc. In these cases I consider breaking convention and writing itemID, teacherID, or unitID
just for improved readability.
What do you do and what's a general, best practice of dealing with this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
Id 是缩写,不是首字母缩略词,所以我将其命名为“Id”。 UI 是一个首字母缩略词,而首字母缩略词——无论如何,都是短的——大写:“UI”。
Id is an abbreviation, not an acronym, so I case it "Id." UI is an acronym, and acronyms--short ones, anyway--get capitalized: "UI."
这是我所做的一个示例:
关键是保持一致。
Here's a sample of what I do:
The key is being consistent.
我做我想做的事。 一般来说,您的最佳实践是倾向于可读性而不是遵守某些抽象标准。
只要保持一致即可。
I do what I feel like. In general, your best practice is to err in favor of readability vs. compliance with some abstract standard.
Just be consistent.
其实我更喜欢“ID”。 但是,正如大家所说,一致性是最重要的。
史蒂夫
I actually prefer "ID". But, as everyone says, consistency is the most important thing.
Steve
Id,作为标识符。 Microsoft 建议的命名约定表明这是推荐的做法,并且通过代码分析进行编译将支持这一点。
如果 ID 代表分别以 I 和 D 开头的两个单词,那么您将使用 ID,并且您确实不应该在参数中使用以小写字母开头的名称。
Id, as for Identifier. Microsoft's suggested naming conventions indicate that that's the recommended practice and compiling with code analysis will support that.
You would use ID if it stood for two words starting respectively with I and D and you're really not supposed to use names starting with lowercase letters but in parameters.
没关系,只要您在整个计划中保持一致即可。
话虽这么说,我会选择“Id”。
It doesn't matter, just as long as you're consistent throughout your program.
That being said, I'd go with "Id".
我也使用驼峰命名法,如果名称以 Id 结尾,我也会使用它。
I use camelcase, too and I also use it if the name ends with Id.
Id
这是我对主观问题的主观回答。
不过我已将其标记为 CW。
Id
This is my subjective answer for the subjective question.
I have marked this as CW though.
我在所有表名中都使用下划线,因此 user_id. 即使 id 是独立的,它也是全小写的。
I use underscores in all of my table names, so user_id. Even if id is standalone, it is all lowercase.
我会在这里被投票,但我不在乎!
显然是id。 来自深处的想法!
I am I going to be voted down here, but I don't care!
it's obviously id. Ideas from the deep!
它的发音是“eye dee”,而不是“id,自我和超我中的id”,所以是ID,而不是Id。 无论如何,这是我的投票。 由于它的发音方式,它是缩写而不是首字母缩略词这一事实是无关紧要的。 它的发音就像是一个缩写词,所以也可以这样输入。 哦,驼峰式命名法是计算史上最糟糕的想法。:)
It's pronounced "eye dee", not "id as in id, ego and superego", so ID, not Id. That's my vote, anyhow. The fact that it's an abbreviation, rather than an acronym is irrelevant, due to the way that it's pronounced. It's pronounced as if it were an acronym, so it may as well be typed that way as well. Oh, and camel case is the worst idea in the history of computing.:)
数据库列和对象属性中的 ID。 参数中的id:
ID in database columns and object properties. id in parameters:
我认为可读性是最重要的,如果要使它更容易阅读,就应该超越你的惯例。 我认为,如果你不能轻松阅读它,那么坚持你的枪/惯例的弊大于利(就像我们程序员讨厌打破惯例和协议一样)。
I think that readability is of utmost importance, and should override your convention if it's going to make it that much easier to read. I think the cons outweigh the pros for sticking to your guns/convention if you can't read it easily (as much as we programmers hate to break conventions and protocols).
在 Machine SUIF 中,Mike Smith 和 Glenn Holloway 严格执行大小写约定,即大写字母标记新单词。 因此,尽管 CPS 是一个缩写词,但它是
transformToCps
而不是transformToCPS
。 我发现从长远来看,他们的方法比我们在 Quick C-- 中所做的更好,其中我们通常对 ID 和 CPS 等情况使用全部大写。In Machine SUIF, Mike Smith and Glenn Holloway rigidly enforce the case convention that a capital letter marks a new word. So even though CPS is an acronym it's
transformToCps
and nottransformToCPS
. I have found that in the long run, their method works better than what we did in Quick C--, where we usually did all caps for cases like ID and CPS.FxCop/代码分析会将 ID 标记为错误的缩写,因此如果您想避免禁用规则,您可以出于这个原因遵守并使用 Id。 但同样,正如其他人指出的那样,只要你保持一致,这并不重要。
FxCop/Code Analysis will flag ID as being a bad abbreviation, so if you want to avoid disabling a rule, you might conform and use Id for that reason. But again, this really doesn't matter, as long as you are consistent, as has been pointed out by others.
只要您在自己的计划中保持一致,无论您感到舒服什么。 (如果你在一个团队中工作,你应该遵守团队的规则或设置一些)。
没有提到的一点是获得好的字体的重要性。 这将对您的程序可读性产生奇迹,并且您的约定问题可能部分是字体问题:
如果您无法轻松地区分 Id 与 ld
( Id 和 ld)
,您应该更改字体,也许还应该更改字体大小。 就我个人而言,我喜欢 Consolas。Whatever you are comfortable with as long as you remain consistent within your own programs. (If you work in a team, you should follow the team's rule or set up some).
One point that hasn't been mentioned is the importance of getting a good font. That will do wonders to your program readability and it could be that your convention problem is partly a font problem:
If you can't easily distinguish Id from ld
(Id and ld)
, you should to change font and perhaps font size too. Personally, I love Consolas.我同意其他答案,最重要的是保持一致。
我想补充一点,如果您的特定代码库没有既定标准,您应该遵循您的语言或平台设置的约定。 在 Java 中,首字母缩略词和其他大写单词对于标识符来说是非大写的:
在 Objective-C 中,情况恰恰相反。 你可能有一个小写的“id”或“url”变量,但你也有这样的类:
所以在 Obj-C 中我会选择一个方法名称,例如:
I agree with the other answers that the most important thing is to be consistent.
I'd add that, if there isn't an established standard for your particular codebase, you should follow the conventions set by your language or platform. In Java, acronyms and other capitalized words are uncapitalized for identifiers:
In Objective-C, it's the opposite. You might have a lowercase "id" or "url" variable, but you also have classes such as:
So in Obj-C I would choose a method name like:
我更喜欢使用 Id,在我们公司,标准也是 Id 而不是 ID。 如果您发现 ID 更容易阅读,我认为使用 ID 没有问题。 编译器不关心:) 我在模式 Id 列中使用相同的约定。
I prefer to use Id and in our company the standard is also Id over ID. I don't think there is a problem using ID if you find it easier to read. The compiler doesn't care :) I use the same convention in my schema Id columns.
不同的语言有不同的指导方针。 对于 .NET,您可以阅读框架设计指南:可重用 .NET 的约定、习惯用法和模式图书馆。 对于 Java,您可以阅读Java 编程语言的代码约定。
Different languages have different guidelines. For .NET you may read Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries. For Java you may read Code Conventions for the Java Programming Language.
“id”是有歧义的。 它是人类心理“自我”/“超我”模型的一部分吗? 可能不是:您可能正在尝试缩写一个单词,例如“Identity”或“Identification”或“Identifier”。
通过确定您的意思然后不要缩写来消除歧义(以及大小写问题)。
"Id" is ambiguous. Is it part of the "ego" / "superego" model of the human psyche? Probably not: you probably are trying to abbreviate a word, like "Identity" or "Identification" or "Identifier".
Get rid of the ambiguity (and the casing question) by deciding what you mean and then don't abbreviate.