Java 中 id 的正确命名约定是什么?

发布于 2024-08-10 18:02:22 字数 60 浏览 2 评论 0原文

如果我有用户 id 变量,那么正确的 java 命名约定是什么?

用户身份 或者 用户身份

If I have user id variable, what is correct java naming convention for it?

userId
or
userID

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

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

发布评论

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

评论(7

幸福丶如此 2024-08-17 18:02:22

我想我在某处读到,最好的指导方针是始终将首字母缩略词的第一个字母大写(即,如果您有一个用户 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.

锦爱 2024-08-17 18:02:22

这是一个艰难的决定,就连 Sun 也不太确定该怎么做。看看JRE中的类,

java.net.URL
java.security.acl.Acl
java.net.HttpURLConnection
java.awt.dnd.DnDConstants
java.awt.color.ICC_Profile

就我个人而言,我更喜欢始终使用驼峰命名法以保持一致。

This is a tough decision, even Sun isn't so sure what to do. Look at classes in JRE,

java.net.URL
java.security.acl.Acl
java.net.HttpURLConnection
java.awt.dnd.DnDConstants
java.awt.color.ICC_Profile

Personally, I prefer to use camel-case always to be consistent.

一江春梦 2024-08-17 18:02:22

我认为 Joshua Bloch 的 Effective Java 中描述的项目在这方面具有权威性:

< strong>遵守普遍接受的命名约定

类和接口名称应由一个或多个单词组成,第一个单词
每个单词的字母大写 [..] 缩写
应避免使用,除了首字母缩略词和某些常见缩写(如 max)
和分钟。 [...] 强大
可以提出赞成仅将第一个字母大写的论点。 [...]您仍然可以分辨出一个单词在哪里开始,下一个单词在哪里结束。您希望看到哪个类名,HTTPURL 还是 HttpUrl?

因此,它应该是 userId

I consider the item described in: Effective Java by Joshua Bloch authoritative in this matter:

Adhere to generally accepted naming conventions

Class and interface names should consist of one or more words, with the first
letter of each word capitalized [..] Abbreviations
are to be avoided, except for acronyms and certain common abbreviations like max
and min. [...] a strong
argument can be made in favor of capitalizing only the first letter. [...] you can still tell where one word starts and the next word ends. Which class name would you rather see, HTTPURL or HttpUrl?

So, It should be userId

放低过去 2024-08-17 18:02:22

就像这样:

  1. 对于首字母缩略词,仅将第一个字母大写。确实。请忽略一些 JDK 方法不这样做的事实。
  2. 但“id”是该语言中最奇怪的单词之一——这是一种缩写,甚至没有人真正知道它到底是什么。有些人建议使用“身份证明文件”,但这很可能只是事后编造的。 “识别……某些东西”是我们拥有的最好的东西。不管怎样,如果你要使用userID,它表明一个普通的id将是iD,而你肯定不想这样做,所以获胜者是:

userId

(正如其他人所说)。

所以无论如何它最终都会被视为缩写词。

It's like this:

  1. For acronyms, uppercase only the first letter. Definitely. Ignore the fact that some JDK methods don't.
  2. But "id" is one of the strangest words in the language -- an abbreviation for which no one even really knows exactly what it's short for. Some have suggested "identifying document", but this is likely just made up after the fact. 'identifiying... something' is about the best we have. Anyway, if you were to use userID, it suggests that a plain id would be iD, 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.

孤单情人 2024-08-17 18:02:22

根据 Java 命名约定,首选第一个 (1) (2)

The first one is preferred according to Java naming convention (1) (2).

简单 2024-08-17 18:02:22

这主要取决于您的命名约定以及您的应用程序(如果有)。例如,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

梦太阳 2024-08-17 18:02:22

就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.

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