LoginSignup
16
5

More than 3 years have passed since last update.

キャメルケースで省略語(頭字語)を使うときの命名規約

Last updated at Posted at 2018-07-16

【移行のお知らせ】プライバシー上の懸念により、Qiitaアカウントの削除を検討しています。
各投稿は個人blog( https://gateau.hatenablog.com/archive/category/Qiita )に移行しました。
今後も参照される方は、ブックマークなど移行をお願い致します。


KotlinでAndroidを書いているときに、命名に迷って調べたメモです。
キャメルケースにおいてHTTP, XML, SMS といった大文字の省略語を扱う場合どう書くのが良いのか、参考のために各コーディングガイドラインを読んでみました。

AOSP Java Code Style for Contributors

Android Open Source Projectにcontributeする際の規約です。

Treat acronyms and abbreviations as words in naming variables, methods, and classes to make names more readable:

Good Bad
XmlHttpRequest XMLHTTPRequest
getCustomerId getCustomerID
class Html class HTML
String url String URL
long id long ID

クラス名はupper camel case, メソッド名や変数名はlower camel caseで普通の単語と同様に扱っています。
JDKとAndroidのcodeでここのルールは矛盾しているけども、致し方なしとしています。

Google Java Style Guide

こちらはかなり詳細に定義してありました。"Ipv6OnIos"は全然見慣れなくてすごいですね。

Prose form Correct Incorrect
XML HTTP request XmlHttpRequest XMLHTTPRequest
new customer ID newCustomerId newCustomerID
inner stopwatch innerStopwatch innerStopWatch
supports IPv6 on iOS? supportsIpv6OnIos supportsIPv6OnIOS
YouTube importer YouTubeImporter

Kotlin.org Coding Conventions

When using an acronym as part of a declaration name, capitalize it if it consists of two letters (IOStream); capitalize only the first letter if it is longer (XmlFormatter, HttpInputStream).

2文字までなら大文字で、それより長い場合は先頭だけ大文字です。

Android以外の場合

Go Code Review Comments

Words in names that are initialisms or acronyms (e.g. "URL" or "NATO") have a consistent case. For example, "URL" should appear as "URL" or "url" (as in "urlPony", or "URLPony"), never as "Url". As an example: ServeHTTP not ServeHttp. For identifiers with multiple initialized "words", use for example "xmlHTTPRequest" or "XMLHTTPRequest".

This rule also applies to "ID" when it is short for "identifier", so write "appID" instead of "appId".

大文字か小文字で統一します。今回サーバサイドがGoでAPIはこのルールに従っていました。

Swift Style Guide

Variables and functions should be in lowerCamelCase, including statics and constants. An exception is acronyms, which should be UPPERCASE.

Rationale: Adopt Apple's naming rules for uniformity. As for acronyms, the readability makes keeping them upper-case worth it.

公式が見当たらずエウレカさんのガイドラインです。upper caseとのこと。

結論

今回使いたかったのは"SMS"と3文字だったのもあり、JavaやKotlinの例を参考に"Sms..."といった書き方にしました。
この件以外にもコーディング規約はあった方が良いので、下記辺りを参考にしてみるつもりです。

言語ごとに結構ルールが違って、調べてみたら面白かったです。

16
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
5