我对骆驼案有疑问。假设你有这样的首字母缩写:Unesco =联合国教育、科学及文化组织。
你应该写:联合国、国家、科学和文化组织
但是如果你需要写首字母缩写呢?喜欢的东西:
getUnescoProperties();
这样写对吗?getUnescoProperties()或getUnescoProperties();
我对骆驼案有疑问。假设你有这样的首字母缩写:Unesco =联合国教育、科学及文化组织。
你应该写:联合国、国家、科学和文化组织
但是如果你需要写首字母缩写呢?喜欢的东西:
getUnescoProperties();
这样写对吗?getUnescoProperties()或getUnescoProperties();
当前回答
要转换为驼峰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则不正确。
其他回答
在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 = [
// ...
];
要转换为驼峰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则不正确。
还有另一种驼峰格式的约定,试图通过使用大写(HTML)或小写(HTML)来提高首字母缩略词的可读性,但避免两者同时使用(HTML)。
在你的例子中,你可以写getUNESCOProperties。你也可以为一个变量写unescoProperties,或者为一个类写unescoProperties(类的约定是以大写开头)。
如果您想将两个首字母缩写放在一起,例如一个名为XML HTTP Request的类,则该规则会变得很棘手。它将以大写开始,但由于XMLHTTPRequest不容易阅读(是XMLHTTPRequest吗?),而且XMLHTTPRequest将打破驼峰格式约定(是XMLhttpRequest吗?),最好的选择是混合大小写:XMLHTTPRequest,这实际上是W3C使用的。然而,不鼓励使用这种类型的命名。对于本例,HTTPRequest将是一个更好的名称。
由于标识/身份的官方英文单词似乎是ID,尽管它不是首字母缩略词,所以您可以在这里应用相同的规则。
这种惯例似乎很流行,但这只是惯例,没有对错之分。只要坚持约定,并确保你的名字是可读的。
微软写的关于camelCase的一些指导方针是:
当使用首字母缩略词时,长度超过两个字符的首字母缩略词应使用Pascal大小写或驼峰大小写。例如,使用HtmlButton或HtmlButton。但是,只由两个字符组成的首字母缩写词应该大写,例如System。而不是System.Io。 不要在标识符或参数名中使用缩写。如果必须使用缩写,则使用驼峰大小写表示包含两个以上字符的缩写,即使这与单词的标准缩写相矛盾。
总结:
当你使用的缩写或首字母缩写只有两个字符长时,全部用大写; 当首字母缩略词超过两个字符时,第一个字符用大写。
因此,在您的特定情况下,getUnescoProperties()是正确的。
目前我正在使用以下规则:
首字母缩写的大写字母:XMLHTTPRequest, XMLHTTPRequest, requestIPAddress。 驼色表示缩写:ID[标识符],Exe[可切割的],App[应用程序]。
ID是个例外,抱歉,但这是真的。
当我看到一个大写字母时,我认为这是一个首字母缩写,即每个字母对应一个单独的单词。缩写并不是每个字母都有单独的单词,所以我使用驼峰格。
XMLHTTPRequest是模棱两可的,但这是罕见的情况,它并没有那么模棱两可,所以没关系,规则和逻辑比美观更重要。