Naming is hard, really. There is a famous browser API XMLHttpRequest
, it’s famous in part for its mixed naming strategy. Here, "XML" is fully capitalized, while "Http" follows a capitalized first letter, leading to an inconsistent and somewhat perplexing naming style.
…until you bumped into it yourself. See how you will name these:
xml http request
, new customer id
, supports IPv6 on iOS
, YouTube importer
Handling Acronyms in Naming Conventions
When incorporating acronyms into identifiers, developers generally adopt one of two approaches:
-
Preserve Original Capitalization: Maintain the acronym's original uppercase letters.
-
Treat Acronyms as Words: Apply standard naming conventions, capitalizing only the first letter in PascalCase or the first letter of subsequent words in camelCase.
For instance, consider the term "XML HTTP request":
-
Preserving Original Capitalization:
-
PascalCase:
XMLHTTPRequest
-
camelCase:
xMLHTTPRequest
orxmlHTTPRequest
(both appear awkward and conflict with camelCase norms)
-
-
Treating Acronyms as Words:
-
PascalCase:
XmlHttpRequest
-
camelCase:
xmlHttpRequest
-
The second approach aligns better with naming conventions, ensuring consistency and readability.
Challenges with Special Cases
Certain names, like "iOS," intentionally start with a lowercase letter. In such cases, adhering strictly to original capitalization can clash with naming conventions. For example, in PascalCase, "iOS Adapter" would become IOSAdapter
, which alters the intended capitalization.
Similarly, names containing numbers, such as "IPv6," present challenges. Deciding whether to treat "IPv6" as a single word or split it into "IpV6" affects the final identifier.
Google's Guidelines
Fortunately, Google offers a systematic approach to handle these scenarios:
-
Convert to ASCII and Remove Apostrophes: For example, "Müller's algorithm" becomes "Muellers algorithm."
-
Split into Words: Divide based on spaces and remaining punctuation.
-
Apply Common Conventions: If a word has a conventional camelCase form, use it (e.g., "AdWords" becomes "adWords").
-
Combine According to Naming Rules: Assemble the words following the desired naming convention.
Notably, Google's guidelines don't specify exact treatments for acronyms, but the process of splitting into words typically treats acronyms as individual words, leading to identifiers like XmlHttpRequest
or xmlHttpRequest
.
Advantages of Treating Acronyms as Words
This method simplifies transitions between different naming conventions (e.g., camelCase to snake_case) and ensures consistency across various codebases. For instance, converting XmlHttpRequest
to snake_case results in xml_http_request
, which is straightforward. In contrast, preserving original capitalization can complicate such conversions.
Conclusion
When dealing with acronyms in naming conventions:
-
Split the term into individual words or components.
-
Apply the standard naming convention rules to each component.
This approach promotes clarity and consistency, though it may require adjusting to seeing acronyms like "ID" rendered as "Id" or "URL" as "Url."
Adhere to the convention, the naming from first section would be:
-
"XML HTTP request" →
XmlHttpRequest
-
"new customer ID" →
NewCustomerId
-
"supports IPv6 on iOS" →
SupportsIpv6OnIos
-
"YouTube importer" →
YouTubeImporter
By adopting this strategy, developers can maintain a consistent and readable codebase, even when faced with complex naming scenarios.