带有首字母缩略词的 Java 命名约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2236807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Naming Convention with Acronyms
提问by DD.
What is the correct name for the following Java class:
DVDPlayer
or DvdPlayer
?
以下 Java 类的正确名称是什么:
DVDPlayer
或DvdPlayer
?
采纳答案by Kevin Cathcart
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.
由于看起来答案是 Java 中没有针对此的单一标准,我想指出 .NET Framework 设计指南确实指定了这一点。
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.
现在在抨击我跑题之前,请记住 Java 和 .NET Framework 的类命名指南非常相似,这使得 .NET 指南可用作有说服力的参考。
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.
两个指南都建议仅在首字母缩略词广为人知且易于理解时才使用首字母缩略词。DVD 或 XML 就是很好的例子,虽然您会立即识别它们,但识别扩展版本需要更长的时间。
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).
.NET Framework 指南建议不要使用缩写词(而不是首字母缩写词),除非可以在标识符中使用两个常见的缩写词“ID”和“OK”。使用缩写时,Id
除了驼峰式标识符的第一个单词(与 PascalCase 标识符相反)外,始终使用大小写混合。
In Java this convention is followed only some of the time. Take a lookat how mixed the spellings getID
and getId
are in the JCL. (Scroll partway down that page). In the Java 8 versionthough, getId
is used more and more, which hints the PascalCase convention is preferred nowadays. It is best to just avoid abbreviations entirely when possible.
在 Java 中,仅在某些时候遵循此约定。看看JCL 中的拼写getID
和getId
是如何混合的。(向下滚动该页面的一半)。然而,在Java 8 版本中,getId
使用越来越多,这暗示 PascalCase 约定现在是首选。最好在可能的情况下完全避免使用缩写。
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
.
.NET Framework 指南规定,像“IO”这样的两个字母首字母缩略词,两个字母的大小写应该相同。因此,对于 PascalCase 标识符(例如类名),您会得到DBRate
,而对于驼峰式标识符(例如局部变量),您可能会得到ioChannel
.
This definitely seems to be the prevailing convention in Java as well.
这绝对似乎也是 Java 中的普遍约定。
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
.
.NET Framework 指南建议三个字母或更长的首字母缩略词对 PascalCase 和 camelCase 标识符使用混合大小写,camelCase 标识符的第一个单词除外。因此,您可能有一个类名XmlDocument
,而可能有一个局部变量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'.
Java 中并不总是遵循此约定。四个字符的首字母缩略词似乎通常使用混合大小写,但即使是 JCL 对三个字母的首字母缩略词也不一致。它们中的大多数似乎都是大写的,例如“URL”、“XML”、“SQL”和“DOM”,但也有一些例外,例如“Jar”。
Conclusion
结论
For Java:
对于 Java:
For 4+ letter acronyms, use mixed case. The standard library does this, and it just makes good sense.
对于 4 个以上字母的首字母缩略词,请使用大小写混合。标准库做到了这一点,这很有意义。
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.
对于 3 个字母的首字母缩略词,您可以像 JCL 一样使用全部大写字母,也可以像 .NET Framework 那样使用混合大小写。无论哪种方式,都要保持一致。
For 2 letter acronyms, use all uppercase.
对于 2 个字母的首字母缩略词,请全部使用大写。
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.
对于 2 个字母的缩写,Java 并没有真正的标准,但我建议使用大小写混合,除非与其他名称保持一致会使所有大写看起来更好。
回答by Tom Hawtin - tackline
DVDPlayer
is the standard, but DvdPlayer
is not uncommon.
DVDPlayer
是标准,但DvdPlayer
并不少见。
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.
你经常看不到getId
。这可能是因为认为 ID 是“身份”的缩写。它实际上是身件的首字母。
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).
HttpURLConnection
通常作为混合约定的示例给出。但是,在 URL 中用作协议名称的“http”应该是小写的(尽管通常接受大写)。
回答by JaakkoK
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
.
我见过它们都在野外使用,而 Sun 似乎很喜欢这种DVDPlayer
风格。DvdPlayer
不过,我更喜欢,因为这样即使有多个连续的首字母缩略词,单词边界的位置也很清楚,如HTTPURLConnection
.
回答by codaddict
From sun java docs:
来自sun java 文档:
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).
类名应该是名词,大小写混合,每个内部单词的第一个字母大写。尽量保持你的类名简单和描述性。使用完整的单词——避免首字母缩略词和缩写词(除非缩写词的使用比长格式更广泛,例如 URL 或 HTML)。
回答by flybywire
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+Tand pick classes by the first letter of each word.
因此我更喜欢DvdPlayer
. 这是更有益,因为在Eclipse中,你可以做Ctrl+ Shift+T和每个单词的第一个字母挑班。
回答by Peter Perhá?
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
.
因此,如果我使用DVDPlayer
,我会怎么称呼它的实例?dVDPlayer
? 因此,我会选择DvdPlayer
类名,因此您可以将实例命名为dvdPlayer
.
回答by Bozho
Some examples from the JavaSE classes, apache commons and spring:
来自 JavaSE 类、apache commons 和 spring 的一些示例:
HttpURLConnection
HTTPAddress
UrlPathHelper
AopProxy
ISBNValidator
HttpURLConnection
HTTPAddress
UrlPathHelper
AopProxy
ISBNValidator
So - it doesn't really matter.
所以 - 这并不重要。
回答by duffymo
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.
Sun 在命名包含“URL”和“HTML”的类的方式上是一致的,但我在 javadoc 中看到 HTTP 使用全部大写和驼峰式大小写。
Personally, I'd prefer DvdPlayer.
就个人而言,我更喜欢 DvdPlayer。
回答by Brian Harris
Effective Javaseems to prefer DvdPlayer.
Effective Java似乎更喜欢 DvdPlayer。
回答by Steven Benitez
As others have indicated, its a style thing that differs across projects. Google projects such as Guava and GWT prefer the DvdPlayer
style.
正如其他人所指出的,它的风格因项目而异。Guava 和 GWT 等谷歌项目更喜欢这种DvdPlayer
风格。
https://google.github.io/styleguide/javaguide.html#s5.3-camel-case
https://google.github.io/styleguide/javaguide.html#s5.3-camel-case