gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:06:21 27 4
gpt4 key购买 nike

有些品牌名称实际上并不喜欢将其首字母大写,更改可以使其看起来完全不同,尤其是“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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com