我对骆驼案有疑问。假设你有这样的首字母缩写:Unesco =联合国教育、科学及文化组织。

你应该写:联合国、国家、科学和文化组织

但是如果你需要写首字母缩写呢?喜欢的东西:

getUnescoProperties();

这样写对吗?getUnescoProperties()或getUnescoProperties();


当前回答

UNESCO是一个特殊的例子,因为它通常(在英语中)被读成一个单词,而不是一个首字母缩略词——像UEFA, RADA, BAFTA,不像BBC, HTML, SSL

其他回答

要转换为驼峰case,还有谷歌的(近)确定性驼峰case算法:

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. Note that the casing of the original words is almost entirely disregarded.

在以下示例中,“XMLHTTP request”正确地转换为XmlHttpRequest, XmlHttpRequest则不正确。

JavaScript Airbnb风格指南谈到了这一点。基本上:

// bad
const HttpRequests = [ req ];

// good
const httpRequests = [ req ];

// also good
const HTTPRequests = [ req ];

因为我通常在课堂上阅读大写字母的前导,所以我倾向于避免这样做。说到底,这都是个人偏好。

除了@valex所说的,我想用这个问题的答案来概括一些事情。

我认为一般的答案是:这取决于你使用的编程语言。

升C

微软已经编写了一些指导方针,在这种情况下,HtmlButton似乎是命名类的正确方式。

Javascript

Javascript有一些首字母缩写的全局变量,并且它们都是大写的(但有趣的是,并不总是一致),下面是一些例子:

encodeURIComponent XMLHttpRequest toJSON toISOString

还有另一种驼峰格式的约定,试图通过使用大写(HTML)或小写(HTML)来提高首字母缩略词的可读性,但避免两者同时使用(HTML)。

在你的例子中,你可以写getUNESCOProperties。你也可以为一个变量写unescoProperties,或者为一个类写unescoProperties(类的约定是以大写开头)。

如果您想将两个首字母缩写放在一起,例如一个名为XML HTTP Request的类,则该规则会变得很棘手。它将以大写开始,但由于XMLHTTPRequest不容易阅读(是XMLHTTPRequest吗?),而且XMLHTTPRequest将打破驼峰格式约定(是XMLhttpRequest吗?),最好的选择是混合大小写:XMLHTTPRequest,这实际上是W3C使用的。然而,不鼓励使用这种类型的命名。对于本例,HTTPRequest将是一个更好的名称。

由于标识/身份的官方英文单词似乎是ID,尽管它不是首字母缩略词,所以您可以在这里应用相同的规则。

这种惯例似乎很流行,但这只是惯例,没有对错之分。只要坚持约定,并确保你的名字是可读的。

在github上有airbnb JavaScript风格指南,有很多星星(目前约57.5k)和关于首字母缩写的指南,其中说:

首字母缩写和首字母缩写都应该大写,或者全部大写 小写的。 为什么?名字是为了可读性,而不是为了取悦计算机算法。

// bad
import SmsContainer from './containers/SmsContainer';

// bad
const HttpRequests = [
  // ...
];

// good
import SMSContainer from './containers/SMSContainer';

// good
const HTTPRequests = [
  // ...
];

// also good
const httpRequests = [
  // ...
];

// best
import TextMessageContainer from './containers/TextMessageContainer';

// best
const requests = [
  // ...
];