某些品牌名称实际上并不喜欢使用首字母大写,更改名称可能会使外观看起来完全不同,尤其是对于“ iOS”而言.还有更多类似“ mCore”,“ macOS”等等.关于班级名称,我应该将首字母大写还是保留原样?
例如“ iOSCompatProxy”和“ IOSCompatProxy”
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:
Convert the phrase to plain ASCII and remove any apostrophes. For example,“Müller’s algorithm” might become “Muellers algorithm”.
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.
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
Finally,join all the words into a single identifier.
他们的方案产生IosCompatProxy作为类名,并产生iosCompatProxy作为变量名.他们故意不理会原始大写字母,而是采用基于规则的骆驼案形式.
也许这不是最漂亮的表格,但是如果您正在寻找可以遵循的规则,并且在您工作的地方没有规则,那么Google的风格指南将是您的最佳选择,因为它专门提到“ iOS”.我倾向于高度重视他们的约定,因为他们使用它们维护着大量Java(截至2018 [1]为止有3亿行).