3

What is the right way to write the name of a class "VPNAPIs"? Is it VpnApis, VPNAPIs or VpnAPIs?

VPN: Virtual Private Network.
API: Application Program Interface.

hex
  • 515
  • 2
  • 4
  • 13

2 Answers2

3

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.

Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55
  • 1
    Have you noticed the classes URI, URL, HttpURLConnection, etc. in the standard Java libraries? Apparently, acronyms are an exception to this rule. Although I personally skip this exception because it makes naming properties impossible when the acronym is followed by a normal word. – Erwin Bolwidt Mar 28 '16 at 13:26
  • 1
    Acronyms are not exceptions to the rule. The classes you've named are old and were written before the standard was standardized. For example, your DAO class would be EntityDao -- even through DAO is an acronym. VpnApis is absolutely correct. – Software Engineer Mar 28 '16 at 13:29
  • 1
    @EngineerDollery Do you have any reference for that? I recall that there formerly were some classes that spelled Url with lowercase r and l and they were changed to uppercase. Even in JDK1.5, `ClassLoadingMXBean` was added (and a few more *MXBeans) Update: enum `CRLReason` was added as recently as JDK1.7, so I think that this refutes what you said. – Erwin Bolwidt Mar 28 '16 at 13:36
  • @ErwinBolwidt In your opinion the correct name is VPNAPIs, am I correct? if so may be we choose to break this rule since this name looks like constant, what is your opinion? – hex May 18 '16 at 19:57
  • @hex I believe that there is no strict rule for this - the standard API's sometimes capitalize acronyms and sometimes not. To this day. I prefer "VpnApis". It is more consistent (only the V and the A start a new word, otherwise every capital letter could be the start of a new camelcased word) and pragmatically, it also helps with the autocompletion in IDE's (just type "VA" then control-space) – Erwin Bolwidt May 19 '16 at 00:49
2

The answer is that all three alternatives are legitimate1, depending on which coding standard you follow, and how you chose to interpret it.

My advice:

  1. Talk to your colleagues.
  2. Pick one style guide, and agree on how strict you need to be in your compliance.
  3. Use your common sense. The purpose of style guides is to maximize readability. They are not an end in their own right.

Original 1997 Sun Java Code Conventions (no longer "maintained").

"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)."

Important notes:

  • The above does not say that only the first word of an internal word should be uppercase.
  • While it recommends against using abbreviations in general, it does not mandate the way that acronyms should be written.

Therefore:

  • VpnApis complies
  • VPNAPIs complies (ugh!)
  • VpnAPIs complies

This interpretation is reinforced by various examples of classnames using acronyms in the Java SE class library. Many of them were added well after 1997, belying the argument that they are "historical exceptions". (Note that 1997 was when JDK 1.1 was released!)


Google Java Style Guide

"Beginning with the prose form of the name:

  1. Convert the phrase to plain ASCII and remove any apostrophes. For example, "Müller's algorithm" might become "Muellers algorithm".
  2. Divide this result into words, splitting on spaces and any remaining punctuation (typically hyphens).
    • Recommended: if any word already has a conventional camel-case appearance in common usage, split this into its constituent parts (e.g., "AdWords" becomes "ad words"). Note that a word such as "iOS" is not really in camel case per se; it defies any convention, so this recommendation does not apply.
  3. Now lowercase everything (including acronyms), then uppercase only the first character of:
    • ... each word, to yield upper camel case, or
    • ... each word except the first, to yield lower camel case
  4. Finally, join all the words into a single identifier."

This gives:

  • VpnApis correct,
  • VPNAPIs incorrect
  • VpnAPIs incorrect

which is reinforced by examples in the document.

Note however, these rules clearly would call out many Java SE class names as being incorrect.


1 - For what is is worth, my preferences would be 1) pick a different class name (this one does not clearly indicate the classes purpose ... though it might if there was more context), 2) use VpnApis, 3) use VpnAPIs.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216