Java 中 id 的正确命名约定是什么?
如果我有用户 id 变量,那么正确的 java 命名约定是什么?
用户身份 或者 用户身份
If I have user id variable, what is correct java naming convention for it?
userId
or
userID
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我想我在某处读到,最好的指导方针是始终将首字母缩略词的第一个字母大写(即,如果您有一个用户 TTL,其中 TTL 是项目中的一些随机首字母缩略词,那么最好写入 userTtl)。
这是有道理的,因为它解决了一些问题。例如,假设您想要一个用户 ID 计数器。 userIDCounter 的可读性比 userIdCounter 差。对于更长的缩写,情况会变得更糟。
I think I read somewhere that the best guideline is to always capitalize only the first letter of an acronym (i.e., if you have a user TTL where TTL is some random acronym in your project, then it's best to write userTtl).
This makes sense, since it solves some problems. For example, say you want a user id counter. Readability of userIDCounter is worse than userIdCounter. For a longer acronym, it gets even worse.
这是一个艰难的决定,就连 Sun 也不太确定该怎么做。看看JRE中的类,
就我个人而言,我更喜欢始终使用驼峰命名法以保持一致。
This is a tough decision, even Sun isn't so sure what to do. Look at classes in JRE,
Personally, I prefer to use camel-case always to be consistent.
我认为 Joshua Bloch 的 Effective Java 中描述的项目在这方面具有权威性:
< strong>遵守普遍接受的命名约定
因此,它应该是
userId
I consider the item described in: Effective Java by Joshua Bloch authoritative in this matter:
Adhere to generally accepted naming conventions
So, It should be
userId
就像这样:
userID
,它表明一个普通的id将是iD
,而你肯定不想这样做,所以获胜者是:userId
(正如其他人所说)。
所以无论如何它最终都会被视为缩写词。
It's like this:
userID
, it suggests that a plain id would beiD
, and you sure as heck don't want to do that, so the winner is:userId
(as others have said).
So it ends up being treated like an acronym anyway.
根据 Java 命名约定,首选第一个 (1) (2)。
The first one is preferred according to Java naming convention (1) (2).
这主要取决于您的命名约定以及您的应用程序(如果有)。例如,userId 是 Java 约定的首选
It mainly depends on your naming convention together with your application (if there are any). For instance, userId is a preferred one with Java convention
就Java惯例而言,在很多书中都有每个单词的第一个字母大写,所有其他字母都是小写字母,所以你有像AcademicStudent这样的类,还有UserId。
As far as the Java convention is concerned its there in lot of books that the first letter of each word is capatailsed and the all other letters are in small letters, So you have classes like AcademicStudent, and so is the UserId.