Java 命名约定及缩写词

发布于 2024-08-20 13:16:20 字数 75 浏览 1 评论 0原文

以下 Java 类的正确名称是什么: DVDPlayer 还是 DvdPlayer

What is the correct name for the following Java class:
DVDPlayer or DvdPlayer?

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

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

发布评论

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

评论(10

〆一缕阳光ご 2024-08-27 13:16:20

由于看起来答案是 Java 中没有单一的标准,因此我想指出 .NET Framework 设计指南确实指定了这一点。

现在,在批评我离题之前,请记住,Java 和 .NET Framework 的类命名指南非常相似,这使得 .NET 指南可以作为有说服力的参考。

一般规则

两个指南都建议仅当缩写词广为人知且易于理解时才使用缩写词。 DVD 或 XML 就是很好的例子,因为虽然您会立即识别它们,但识别扩展版本需要更长的时间。

缩写

.NET Framework 指南建议不要使用缩写(而不是首字母缩略词),但标识符中可以使用两个常见缩写 IDOK 。使用缩写时,始终使用混合大小写的 Id,除了驼峰命名法标识符(与 PascalCase 标识符相反)的第一个单词外。

在 Java 中,仅在某些时候遵循此约定。 看看如何混合拼写 getIDgetId 位于 JCL 中。 (向下滚动该页面的一半)。但在 Java 8 版本 中, getId 使用得越来越多,这暗示 PascalCase 约定现在是首选。最好尽可能完全避免缩写。

简短的首字母缩略词

.NET Framework 指南规定,像 IO 这样的双字母首字母缩略词,两个字母的大小写应该相同。因此,对于 PascalCase 标识符(如类名),您将获得 DBRate,而对于 CamelCase 标识符(如局部变量),您可能会获得 ioChannel

这似乎也是 Java 中的普遍约定。

长首字母缩略词

.NET Framework 指南建议对于 PascalCase 和 CamelCase 标识符,三个字母或更长的首字母缩略词使用混合大小写,但 CamelCase 标识符的第一个单词除外。因此,对于类名,您可能会使用 XmlDocument,而局部变量可能会命名为 httpRequest

Java 中并不总是遵循此约定。四字符缩写词似乎通常使用混合大小写,但即使是 JCL 对于三字母缩写词也不一致。其中大多数似乎都是大写的,例如 URLXMLSQLDOM,但也有一些例外,例如 Jar

结论

对于 Java:

对于 4 个以上字母的首字母缩略词,请使用大小写混合。标准库就是这样做的,而且它很有意义。

对于 3 个字母的首字母缩略词,您可以像 JCL 一样使用全部大写,也可以像 .NET Framework 一样使用大小写混合。无论哪种方式,都要保持一致。

对于 2 个字母的缩写词,请全部使用大写。

对于 2 个字母的缩写,Java 并没有真正的标准,但我建议使用混合大小写,除非与其他名称保持一致会让全部大写看起来更好。

Since it looks like the answer is that there is no single standard for this in Java, I'd like to note that the .NET Framework Design Guidelines do specify this.

Now before slamming me for being off topic, please remember that the class naming guidelines for Java and the .NET Framework are quite similar, which makes the .NET guidelines useful as a persuasive reference.

General Rules

Both guidelines recommend only using acronyms when the acronym is widely known and well understood. DVD or XML are excellent examples of this, as while you will recognize them immediately, it would take a bit longer to recognize the expanded version.

Abbreviations

The .NET Framework Guidelines recommend not to use abbreviations (as opposed to acronyms), except that two common abbreviations ID and OK may be used in identifiers. When using an abbreviation, mixed case Id is always used except for the first word of a camelCase identifier (as opposed to a PascalCase identifier).

In Java this convention is followed only some of the time. Take a look at how mixed the spellings getID and getId are in the JCL. (Scroll partway down that page). In the Java 8 version though, getId is used more and more, which hints the PascalCase convention is preferred nowadays. It is best to just avoid abbreviations entirely when possible.

Short Acronyms

The .NET Framework Guidelines say that two letter acronyms like IO, should have the same case for both letters. So for PascalCase identifiers (like a class name) you would get DBRate, while for a camelCase identifier (like a local variable) you might have ioChannel.

This definitely seems to be the prevailing convention in Java as well.

Long Acronyms

The .NET Framework guidelines recommend that acronyms three letters or longer use mixed case for PascalCase and camelCase identifiers, except for the first word of a camelCase identifier. Thus for a class name you might have XmlDocument, while a local variable might be named httpRequest.

This convention is not always followed in Java. Four character acronyms do seem to usually use mixed case, but even the JCL is not consistent about three letter acronyms. Most of them seem to be all uppercase, like URL, XML, SQL, and DOM, but there are some exceptions like Jar.

Conclusion

For Java:

For 4+ letter acronyms, use mixed case. The standard library does this, and it just makes good sense.

For 3 letter acronyms, you can use all uppercase like the JCL, or you can use mixed case like the .NET Framework does. Either way, be consistent.

For 2 letter acronyms, use all uppercase.

For 2 letter abbreviations, Java does not really have a standard, but I suggest using mixed case, unless consistency with other names would make all uppercase look better.

我们只是彼此的过ke 2024-08-27 13:16:20

没有“正确”答案。只是一组可以更好地与其他工具配合使用的实践和约定。

因此我更喜欢 DvdPlayer。它更有帮助,因为在 Eclipse 中您可以执行 Ctrl+Shift+T 并按每个单词的第一个字母选择类别。

替代文字

There is no "correct" answer. Just a set of practices and conventions that better play with your other tools.

Therefore I prefer DvdPlayer. It is more helpful as in Eclipse you can do Ctrl+Shift+T and pick classes by the first letter of each word.

alt text

遮了一弯 2024-08-27 13:16:20

我见过它们都在野外使用过,Sun 似乎更喜欢 DVDPlayer 风格。不过,我更喜欢 DvdPlayer,因为这样即使有多个连续的首字母缩略词,单词边界也很清楚,如 HTTPURLConnection 中所示。

I've seen both of them used in the wild, and Sun seems to go for the DVDPlayer style. I prefer DvdPlayer, though, because that way it is clear where the word boundaries are even if there are multiple consecutive acronyms, as in HTTPURLConnection.

醉殇 2024-08-27 13:16:20

我喜欢用以下方式定义类的单独实例:

Catalogue catalogue;
Person person;

因此,如果我使用 DVDPlayer,我会如何称呼它的实例? dVDPlayer?因此,我选择 DvdPlayer 类名,这样您就可以将实例命名为 dvdPlayer

I like to define individual instances of classes in the following fashion:

Catalogue catalogue;
Person person;

Therefore, if I used DVDPlayer, what would I call an instance of that? dVDPlayer? Hence I'd choose the DvdPlayer class name, so you can name the instances like dvdPlayer.

超可爱的懒熊 2024-08-27 13:16:20

来自 JavaSE 类、apache commons 和 spring 的一些示例:

  • HttpURLConnection
  • HTTPAddress
  • UrlPathHelper
  • AopProxy
  • ISBNValidator代码>

所以 - 这并不重要。

Some examples from the JavaSE classes, apache commons and spring:

  • HttpURLConnection
  • HTTPAddress
  • UrlPathHelper
  • AopProxy
  • ISBNValidator

So - it doesn't really matter.

も星光 2024-08-27 13:16:20

Effective Java 似乎更喜欢 DvdPlayer。

Effective Java seems to prefer DvdPlayer.

愿与i 2024-08-27 13:16:20

正如其他人所指出的,它的风格因项目而异。 Guava 和 GWT 等 Google 项目更喜欢 DvdPlayer 风格。

https://google.github.io/styleguide/javaguide.html #s5.3-camel-case

As others have indicated, its a style thing that differs across projects. Google projects such as Guava and GWT prefer the DvdPlayer style.

https://google.github.io/styleguide/javaguide.html#s5.3-camel-case

街角迷惘 2024-08-27 13:16:20

来自 sun java 文档

类名应该是名词,混合大小写,每个内部单词的第一个字母大写。尽量保持类名简单且具有描述性。使用整个单词 - 避免使用首字母缩略词和缩写词(除非缩写词比长形式使用更广泛,例如 URL 或 HTML)。

From sun java docs:

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

夢归不見 2024-08-27 13:16:20

DVDPlayer 是标准,但 DvdPlayer 并不罕见。

您经常会看到 getId。这可能是因为认为 ID 是“Identity”的缩写。它实际上是身份证件的姓名缩写。

HttpURLConnection 通常作为混合约定的示例给出。但是,在 URL 中用作协议名称的“http”应该是小写(尽管通常接受大写)。

DVDPlayer is the standard, but DvdPlayer is not uncommon.

You more often than not see getId. That's probably due to thinking ID is a shortening of "Identity". It is actually the initials of Identity Document.

HttpURLConnection is often given as an example of mixed convention. However, "http" used as protocol name in a URL should be lower case (although upper case is often accepted).

尛丟丟 2024-08-27 13:16:20

这里没有“正确”,只有偏好。

Sun 在命名包含“URL”和“HTML”的类方面保持一致,但我在 javadoc 中看到 HTTP 使用全部大写和驼峰式大小写。

就我个人而言,我更喜欢 DvdPlayer。

There is no "correct", only preferences here.

Sun is consistent in the way they name classes containing "URL" and "HTML", but I see HTTP using both all caps and camel case in the javadocs.

Personally, I'd prefer DvdPlayer.

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