java - 当我们在类名中使用 "i"作为单词时,我们应该将 "iOS"大写吗?

标签 java naming-conventions camelcasing

有些品牌名称实际上并不喜欢将其首字母大写,更改可以使其看起来完全不同,尤其是“iOS”。还有更多类似“mCore”、“macOS”等。当涉及到类名时,我应该将第一个字母大写还是保留原样?

例如“iOSCompatProxy”与“IOSCompatProxy

最佳答案

对于这种类型的类名,没有普遍遵循的约定。即使像“XML”这样的常规大写缩写也会导致不一致;查看the official JDK class list ,我看到名为 XMLSomething 的类与 XmlSomething 一样多。

Google 在 Google Java Style Guide 中处理此主题,他们给出了将文本转换为类名的方案:

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.

他们的方案产生 IosCompatProxy 作为类名,iosCompatProxy 作为变量名。他们故意无视原始大小写,转而采用更基于规则的驼峰式大小写形式。

也许这不是最漂亮的形式,但如果您正在四处寻找可以遵循的规则,而您工作的地方没有这样的规则,Google 的风格指南是尽可能好的规则,因为它特别提到了“iOS”。我倾向于高度评价他们的约定,因为他们维护着大量使用它们的 Java(截至 2018 年有 3 亿行 [1])。

关于java - 当我们在类名中使用 "i"作为单词时,我们应该将 "iOS"大写吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54473245/

相关文章:

java - unix中java命令的含义

Java "ANSI constants"

c# - 使用 StringEnumConverter 将枚举值序列化为驼峰式字符串

json - RestSharp 在驼峰式大小写中序列化 JSON

java - 如何确定使用lambda实现泛型FunctionalInterface的Bean的类型参数?

java - 如何通过二分查找获取数组中的最后一个字符

java - 带有日期的负参数的奇怪结果

python - Python 中单下划线 "_"变量的用途是什么?

c# - 私有(private)字段的命名约定

c# - 避免特定方法的 CamelCasePropertyNamesContractResolver