Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should we capitalize "i" when we use "iOS" as a word in a class name?

Some brand names don't actually like to capitalize on their first letter, and changing can make it looks completely different, especially for "iOS". There's more like "mCore", "macOS", and so on. When it comes to class names, should I capitalize the first letter or leave it original?

e.g. "iOSCompatProxy" vs "IOSCompatProxy"

like image 633
iry Avatar asked Feb 01 '19 05:02

iry


2 Answers

There is no universally followed convention for this type of class name. Even a regular upper-case abbreviation like "XML" causes inconsistency; looking over the official JDK class list, I see as many classes named XMLSomething as XmlSomething.

Google treat this topic in the Google Java Style Guide, where they give a scheme for turning text into class names:

Sometimes there is more than one reasonable way to convert an English phrase into camel case, such as when acronyms or unusual constructs like "IPv6" or "iOS" are present. To improve predictability, Google Style specifies the following (nearly) deterministic scheme.

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.

Their scheme yields IosCompatProxy as the class name, and iosCompatProxy as the variable name. They deliberately disregard the original capitalization in favor of a more rule-based camel case form.

Maybe it's not the prettiest form to look at, but if you're looking around for a rule to follow, and don't have a rule for this where you work, Google's style guide is as good a rule as you'll get, because it specifically mentions "iOS". I tend to regard their conventions highly because of the immense amount of Java they maintain using them (300 million lines as of 2018 [1]).

like image 52
Boann Avatar answered Sep 30 '22 15:09

Boann


Generally speaking, in Java class names always the first letter and all subsequent letters that begin a new word are capitalized, such as ThisIsAClass. This is to discriminate them from variable and method names. Thus, I believe IOSCompatProxy would be the proper class name based upon my interpretation of the Oracle style guide, which is the general Java style guide, seen here (I don't believe any major style changes were made since this was last updated):

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

Moreover, because iOS is an acronym that is much more widely used than 'Internet and Operating System', I would say keep the entire acronym capitalized. If you have a different interpretation, such as one that matches what @Boann wrote, use that.

However, I discourage you from following Google's style guide unless you work for Google (general syntactical following is probably OK assuming Google bases their style guide on the Oracle one) only because their styling may be different from a company you work for in the future. Instead, I recommend following the Oracle style guide, found here: https://www.oracle.com/technetwork/java/codeconvtoc-136057.html

like image 23
M_Dragon Avatar answered Sep 30 '22 15:09

M_Dragon