Naming convention for multi-word identifiers with initialisms
w3ctag.github.io/design-principles/#casing-rules
From: es-discuss [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Alexander Jones Sent: Tuesday, July 11, 2017 08:07 To: es-discuss at mozilla.org Subject: Naming convention for multi-word identifiers with initialisms
Occasionally this comes up when deciding on a spelling for something. There are numerous examples in ECMAScript and other Web standards that seem to defy the most uniform convention:
JSON
vs.Json
toJSON
vs.toJson
XMLHttpRequest
vs.XmlHttpRequest
DOMElement
vs.DomElement
While it looks initially strange to specifically drop the capital letters on an initialisms like XML, the rule is simple in that it has an obvious machine decoding - a capital letter starts a new word. Thus, translations of identifiers to other case conventions are automatic (so long as numbers never appear at the start of a word). This helps a lot when e.g. generating bindings for IDLs to different languages, or in general interfacing different systems that really, really want to use their own naming conventions.
- mixed case:
myTlaIdentifierHere
- pascal case:
MyTlaIdentifierHere
- underscore case:
my_tla_identifier_here
- uppercase:
MY_TLA_IDENTIFIER_HERE
- kebab case:
my-tla-identifier-here
- spaces case:
my tla identifier here
My question is whether there is an existing community recommendation anywhere for naming ECMAScript identifiers. Clearly, when Microsoft devised the name XMLHttpRequest
, someone was having a difficult time figuring out how to spell adjacent initialisms in PascalCase.
If there is no recommendation, perhaps there should be? Is there any scope for non-normative sections of information like this in the ECMAScript spec?
Thanks
Alex
Thanks for the pointer Dominic - it does make me weep a bit
(HTMLHRElement
u wot) but I'm glad someone wrote it down! (And it seems
XMLHttpRequest
is still in violation...)
The next step is probably something more akin to PEP-8 than a partial codification of existing norms.
Just for funsies I looked this up - In the interests of honesty I should
point out that apparently it was Mozilla who named this XMLHttpRequest
thing.
softwareengineering.stackexchange.com/questions/157375/why
Microsoft said "XMLHttpRequest" in August 1999 [*1], a month before feature request for Mozilla [*2].
[*1] web.archive.org/web/19990828184122/http://msdn.microsoft.com/xml/reference/scriptref/XMLHttpRequest_Object.asp [*2] bugzilla.mozilla.org/show_bug.cgi?id=15119
Regarding naming, ES itself is pretty consistent:
- Types are PascalCase. Ex: WeakMap
- Non-symbol constants are UPPER_SNAKE_CASED. Ex: Number.MAX_SAFE_INTEGER
- Initalisms within names are always lower case if first and not part of a type name, upper case otherwise. Ex: JSON, Date.prototype.toJSON
- Everything else is camelCase. Ex: Symbol.hasInstance, Object.keys
It's the DOM and HTML APIs that are inconsistent, typically for historical reasons.
Occasionally this comes up when deciding on a spelling for something. There are numerous examples in ECMAScript and other Web standards that seem to defy the most uniform convention:
JSON
vs.Json
toJSON
vs.toJson
XMLHttpRequest
vs.XmlHttpRequest
DOMElement
vs.DomElement
While it looks initially strange to specifically drop the capital letters on an initialisms like XML, the rule is simple in that it has an obvious machine decoding - a capital letter starts a new word. Thus, translations of identifiers to other case conventions are automatic (so long as numbers never appear at the start of a word). This helps a lot when e.g. generating bindings for IDLs to different languages, or in general interfacing different systems that really, really want to use their own naming conventions.
myTlaIdentifierHere
MyTlaIdentifierHere
my_tla_identifier_here
MY_TLA_IDENTIFIER_HERE
my-tla-identifier-here
my tla identifier here
My question is whether there is an existing community recommendation anywhere for naming ECMAScript identifiers. Clearly, when Microsoft devised the name
XMLHttpRequest
, someone was having a difficult time figuring out how to spell adjacent initialisms in PascalCase.If there is no recommendation, perhaps there should be? Is there any scope for non-normative sections of information like this in the ECMAScript spec?
Thanks
Alex