我对骆驼案有疑问。假设你有这样的首字母缩写:Unesco =联合国教育、科学及文化组织。
你应该写:联合国、国家、科学和文化组织
但是如果你需要写首字母缩写呢?喜欢的东西:
getUnescoProperties();
这样写对吗?getUnescoProperties()或getUnescoProperties();
我对骆驼案有疑问。假设你有这样的首字母缩写:Unesco =联合国教育、科学及文化组织。
你应该写:联合国、国家、科学和文化组织
但是如果你需要写首字母缩写呢?喜欢的东西:
getUnescoProperties();
这样写对吗?getUnescoProperties()或getUnescoProperties();
当前回答
JavaScript Airbnb风格指南谈到了这一点。基本上:
// bad
const HttpRequests = [ req ];
// good
const httpRequests = [ req ];
// also good
const HTTPRequests = [ req ];
因为我通常在课堂上阅读大写字母的前导,所以我倾向于避免这样做。说到底,这都是个人偏好。
其他回答
JavaScript Airbnb风格指南谈到了这一点。基本上:
// bad
const HttpRequests = [ req ];
// good
const httpRequests = [ req ];
// also good
const HTTPRequests = [ req ];
因为我通常在课堂上阅读大写字母的前导,所以我倾向于避免这样做。说到底,这都是个人偏好。
getUnescoProperties()应该是最好的解决方案…
如果可能的话,遵循纯驼峰式,当你有首字母缩写时,尽可能让它们大写,否则就采用驼峰式。
通常在面向对象编程中,变量应该以小写字母(lowerCamelCase)开始,类应该以大写字母(UpperCamelCase)开始。
如果有疑问,就去纯骆驼牌吧;)
parseXML是好的,parseXML也是camelCase
XMLHTTPRequest应该是XMLHTTPRequest还是XMLHTTPRequest没有办法与后续的大写首字母缩写一起使用,这肯定不是所有测试用例都清楚。
如。 你怎么读这个词httpslrequest, HTTP + SSL,或者HTTPS + SL(这没有任何意思,但…),在这种情况下,遵循驼峰的惯例,去httpslrequest或httpslrequest,也许它不再好看,但它肯定更清楚。
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则不正确。
免责声明:英语不是我的母语。但是我已经考虑这个问题很长一段时间了,特别是当使用节点(驼峰风格)来处理数据库时,因为表字段的名称应该是蛇化的,这是我的想法:
对于程序员来说,有两种“首字母缩写”:
自然语言,联合国教科文组织 在计算机编程语言中,例如tmc和textMessageContainer,通常以局部变量的形式出现。
在编程世界中,自然语言中的所有首字母缩略词都应该被视为单词,原因是:
when we programming, we should name a variable either in acronym style or non-acronym-style. So, if we name a function getUNESCOProperties, it means UNESCO is an acronym ( otherwise it shouldn't be all uppercase letters ), but evidently, get and properties are not acronyms. so, we should name this function either gunescop or getUnitedNationsEducationalScientificAndCulturalOrganizationProperties, both are unacceptable. natural language is evolving continuously, and today's acronyms will become words tommorow, but programs should be independent of this trend and stand forever.
顺便说一下,在投票最多的答案中,IO是计算机语言中的首字母缩写(代表InputOutput),但我不喜欢这个名字,因为我认为(在计算机语言中)首字母缩写只应该用于命名局部变量,而不是顶级类/函数,所以应该使用InputOutput而不是IO