スネークケース(snake_case)とは、単語のつなぎ目を「_(アンダースコア)」で接続し、各単語の最初の文字を小文字で書く書式を言う。これは、変数名やサブルーチン名、ファイル名など、コンピュータ業界で一般的に使用される命名規則である。ある研究によると、コードのリーダーはキャメルケースよりもスネークケースの方が値(value)をより早く認識できることがわかった。もっとも、「被験者は主にアンダースコアスタイルで訓練されていた」ため、実際の所は定かではない[1]。
歴史
単語の区切り文字としてアンダースコアが使用されるようになったのは、1960年代後半にまで遡る。これは特にC言語に関連しており、『The C Programming Language』(1978)にも見られるスタイルで、「パスカルケース」(キャメルケースの一種) と対比されている。ただし、このスタイルには従来、特定の名前は付いていなかった。2001年にリリースされたPythonコーディング規約の決定版「PEP8」では、単に「lower_case_with_underscores」と呼ばれていた[2]。
「snake_case」という用語の初出は、2004年のUsenet内のRubyコミュニティで[3]、Gavin Kistner が以下のようなコメントにおいて使用した。
ところで…この命名スタイルをお前らなんて呼んでる?スネークケースか〜?誰かが直してくれるまで俺はこう呼ぶことにするよ。
しかし、元IntelエンジニアのJack Dahlgrenによると、2002 年にIntel社内で (そしておそらくはMicrosoftエンジニアとの会話の中で)この用語を使用していたとのこと[4][出典無効]。ひょっとすると、この用語は複数のコミュニティにおいて独自に生み出されたのかもしれない。
2015年現在[update]、複数の単語からなる識別子を区切り文字で区切るような命名規則の名前は標準化されていないが、リスプケース(lisp-case)、ケバブケース(kebab-case)、スクリーミングスネークケース(SCREAMING_SNAKE_CASE)など、一部の用語に関しては使用される機会は増えている[5][6][7]。
用例
以前よりスネークケースを使用しているプログラミング言語のリスト
- ABAP[8]
- Ada - なお頭文字は大文字[9]
- C++ - Boost C++ライブラリ[10]
- C - 標準ライブラリの一部の型名には使用されるが、関数名には使用されない。
- Eiffel - クラスと特性(フィーチャー)名[11]
- Elixir - アトム、変数、関数名[12]
- Erlang - 関数名[13]
- GDScript - 変数名と関数名[14]
- Java - static final定数と列挙型に SCREAMING_SNAKE_CASE を使用する[15]
- Kotlin - 定数に SCREAMING_SNAKE_CASE を使用 [16]
- Magik
- OCaml - 値、型、モジュール名[17]
- Perl - レキシカル変数とサブルーチン[18]
- Oracle SQL and PL/SQL - [19] for all unquoted identifiers (tables, columns, indexes, constraints, PL/SQL variables, constants, procedures/functions, triggers,...), although not official by Oracle itself, still recommended by the majority of known "influencers" and used throughout the official Oracle documentation
- All unquoted snake_case identifiers are actually internally represented as SCREAMING_SNAKE_CASE identifiers.
- Prolog - for both atoms (predicate names, function names, and constants) and variables[20]
- Python - for variable names, function names, method names, and module or package (i.e. file) names[2]
- PHP - uses SCREAMING_SNAKE_CASE for class constants
- R - for variable names, function names, and argument names, especially in the tidyverse style[21]
- Ruby - for variable and method names[22]
- Rust - for variable names, function names, method names, module names, and macros[23]
- Tcl
- Terraform - for resources and variables[24]
命名規則一覧
名称 | 英語表記 | 説明 | 表記例 |
---|---|---|---|
スネークケース | snake case | 単語間をアンダースコア(_ )で繋ぐ形式。
|
example_variable
|
スクリーミングスネークケース | screaming snake case | 単語間をアンダースコア(_ )で繋ぎ、全て大文字にする形式。「アッパースネークケース」(upper snake case) や「コンスタントケース」(constant case) とも呼ばれる[25]。
|
EXAMPLE_VARIABLE
|
キャメルケース | camel case | 各単語の頭文字を大文字にし、単語を連結する形式(最初の単語のみ頭文字が小文字)。.NETの文脈で使用。 | exampleVariable
|
ローワーキャメルケース | lower camel case | キャメルケースと同じ形式だが、フレームワークや言語に依存しない表現。 | exampleVariable
|
パスカルケース | Pascal case | 各単語の頭文字を大文字にし、単語を連結する形式(キャメルケースと似ているが、最初の単語の頭文字も大文字)。.NETの文脈で使用。 | ExampleVariable
|
アッパーキャメルケース | upper camel case | パスカルケースと同じ形式だが、フレームワークや言語に依存しない表現。 | ExampleVariable
|
ケバブケース | kebab case | 単語間をハイフン(- )で繋ぎ、各単語の頭文字を小文字にする形式。
「チェインケース/チェーンケース」(chain case) とも呼ばれる[25]。 |
example-variable
|
トレインケース | train case | 単語間をハイフン(- )で繋ぎ、各単語の頭文字を大文字にする形式。
|
Example-Variable
|
ドットケース | dot case | 単語間をドット(. )で繋ぐ形式。
|
example.variable
|
ローワーケース[独自研究?] | lower case | 全て小文字で単語を連結する形式。 | examplevariable
|
アッパーケース[独自研究?] | upper case | 全て大文字で単語を連結する形式。 | EXAMPLEVARIABLE
|
出典
- ^ Sharif, Bonita; Maletic, Jonathan I. (2010). “An Eye Tracking Study on camelCase and under_score Identifier Styles”. 2010 IEEE 18th International Conference on Program Comprehension. pp. 196–205. doi:10.1109/ICPC.2010.41. ISBN 978-1-4244-7604-6
- ^ a b van Rossum, Guido; Warsaw, Barry; Coghlan, Nick (2001年7月5日). “PEP 0008 -- Style Guide for Python Code”. 2023年8月9日閲覧。
- ^ Gavin Kistner (23 February 2004). "Appropriate use of camelCase". Newsgroup: comp.lang.ruby. Usenet: HBn_b.379957$xy6.2073499@attbi_s02. 2015年8月13日閲覧。
- ^ “What is the history of the term "snake case" (snake_case)?”. Quora (2013年5月10日). 2023年8月9日閲覧。
- ^ “StackOverflow – What's the name for snake_case with dashes?”. 2023年8月9日閲覧。
- ^ “Programmers – If this is camelCase what-is-this?”. 2016年11月5日時点のオリジナルよりアーカイブ。2016年11月5日閲覧。
- ^ “Camel_SNAKE-kebab”. GitHub (2020年4月23日). 2023年8月9日閲覧。
- ^ “Naming Conventions in ABAP Objects”. help.sap.com. 2020年7月28日閲覧。
- ^ “Ada Programming Guidelines”. 2021年12月19日時点のオリジナルよりアーカイブ。2021年12月19日閲覧。
- ^ “Boost Library Requirements and Guidelines”. 2015年8月13日閲覧。
- ^ “Eiffel Class and Feature Names” (2019年12月28日). 2023年8月9日閲覧。
- ^ “Elixir Style Guide”. GitHub (2020年5月). 2023年8月9日閲覧。
- ^ “Programming Rules”. 2017年8月11日閲覧。
- ^ “GDScript Style Guide”. 2023年8月9日閲覧。
- ^ “Code Conventions for the Java Programming Language – Naming Conventions”. Oracle. 2021年8月3日閲覧。
- ^ “Coding Conventions”. 2023年2月3日閲覧。
- ^ “Xen wiki”. 2017年3月15日閲覧。
- ^ Damian Conway (2005). Perl Best Practices. O'Reilly Media Inc.. p. 44. ISBN 978-0596001735
- ^ “Quick Guide to Some Sources for Naming Conventions for Oracle Database Development”. stevenfeuersteinonplsql.blogspot.com. 2020年12月30日閲覧。
- ^ Michael A. Covington; Roberto Bagnara; Richard A. O'Keefe; Jan Wielemaker; Simon Price (2009). "Coding Guidelines for Prolog (v.3)". p. 14. arXiv:0911.2899 [cs.PL]。
- ^ Wickham, Hadley. The tidyverse style guide
- ^ “Ruby · Naming Convention”. namingconvention.org. 2023年8月9日閲覧。
- ^ “Naming – Rust API Guidelines”. 2023年8月9日閲覧。
- ^ “Terraform Naming Conventions” (2022年2月). 2023年8月9日閲覧。
- ^ a b “スネークケースとは - IT用語辞典”. IT用語辞典 e-Words. 2024年7月7日閲覧。