Search code examples
javanaming-conventionscamelcasing

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"


Solution

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