Java Naming Convention with Acronyms

Java Naming Convention with Acronyms

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.

Naming convention for upper case abbreviations

There is no one correct answer. This wiki extract is helpful:

Programming identifiers often need to contain acronyms and initialisms
which are already in upper case, such as "old HTML file". By analogy
with the title case rules, the natural camel case rendering would have
the abbreviation all in upper case, namely "oldHTMLFile". However,
this approach is problematic when two acronyms occur together (e.g.,
"parse DBM XML" would become "parseDBMXML") or when the standard
mandates lower camel case but the name begins with an abbreviation
(e.g. "SQL server" would become "sQLServer"). For this reason, some
programmers prefer to treat abbreviations as if they were lower case
words and write "oldHtmlFile", "parseDbmXml" or "sqlServer".

Java naming convention with acronyms

Both are correct. Use your preferred way, and be consistent with it throughout your code.

Java: Naming convention for plural acronyms

The first (findAllDvds). The second (findAllDvd) is simply incorrect, "all" implies more than one, but "Dvd" is singular in English.

Re your edit:

the confusing part here is that findAllDvds can imply a new acronym DVDS, and it can be considered confusing

Since the "all" implies multiple, the "s" on "Dvds" reads as a plural, not part of the acronym. If it really were DVDS, the name would be findAllDvdss or similar.

It's said that in computer science, there are three hard problems: Cache invalidation, and naming things. (Off-by-one errors are just common, not hard.)

Kotlin and Java naming convention for isUHD

While UHD is written in all uppercase in English, Java naming conventions “win” in Java: They say we should use camel case, isUhd.

It’s not that clear-cut, though. Even old JDK classes tend to keep all uppercase for abbreviations that are part of class or method names, for example Character.isISOControl. Newer additions to JDK rather apply the naming conventions more strictly and use camel case, for example IsoChronology (class in java.time.chrono, since Java 8) or ZoneId.getAvailableZoneIds​() (where ID is written in all uppercase in English). Modern usage is camel case.

The comment by Krzysztof Atłasik supports the same. Google Java Style Guide is gaining recognition as the official Java conventions are not being maintained as the language evolves. Google is clear about camel case and gives this example among others: "supports IPv6 on iOS?" becomes supportsIpv6OnIos.

Link: Google Java Style Guide

What is correct Java naming convention for id?

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.

How to handle acronyms in class names?

Camel Case (XmlParser) is the preferred way because it is easier to read

Naming Conventions For Class Containing Acronym

Java conventions seem lately to favor treating well-known acronyms like words, so: "XmlWriter"...

http://www.ibm.com/developerworks/library/ws-tip-namingconv.html

Java Naming Convention with Acronyms <-dupe question?

http://geosoft.no/development/javastyle.html

But nobody seems to be very consistent. Take JavaScript's XMLHttpRequest for example. What a trainwreck!

What is the right naming for VPNAPIs class

VpnApis is correct.

As per java's naming convention,

All the classes, interfaces should start with uppercase letter and be
a noun/adjective

Every Java class name must start by Capital Letter meanwhile if
sub-word appear then it's also start by Capital.



Related Topics



Leave a reply



Submit