スネークケース(snake_case)とは、単語のつなぎ目を「_(アンダースコア)」で接続し、各単語の最初の文字を小文字で書く書式を言う。これは、変数名やサブルーチン名、ファイル名など、コンピュータ業界で一般的に使用される命名規則である。ある研究によると、コードのリーダーはキャメルケースよりもスネークケースの方が値(value)をより早く認識できることがわかった。もっとも、「被験者は主にアンダースコアスタイルで訓練されていた」ため、実際の所は定かではない[1]

Linuxのカーネルモジュールのコードの一部より。識別子にスネークケースを用いている

歴史 編集

単語の区切り文字としてアンダースコアが使用されるようになったのは、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年現在、複数の単語からなる識別子区切り文字で区切るような命名規則の名前は標準化されていないが、リスプケース(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]

関連項目 編集

参照 編集

  1. ^ 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. http://www.cs.kent.edu/~jmaletic/papers/ICPC2010-CamelCaseUnderScoreClouds.pdf 
  2. ^ a b van Rossum, Guido; Warsaw, Barry; Coghlan, Nick (2001年7月5日). “PEP 0008 -- Style Guide for Python Code”. 2023年8月9日閲覧。
  3. ^ Gavin Kistner (23 February 2004). "Appropriate use of camelCase". Newsgroupcomp.lang.ruby. Usenet: HBn_b.379957$xy6.2073499@attbi_s02. 2015年8月13日閲覧
  4. ^ What is the history of the term "snake case" (snake_case)?”. Quora (2013年5月10日). 2023年8月9日閲覧。
  5. ^ StackOverflow – What's the name for snake_case with dashes?”. 2023年8月9日閲覧。
  6. ^ Programmers – If this is camelCase what-is-this?”. 2016年11月5日時点のオリジナルよりアーカイブ。2016年11月5日閲覧。
  7. ^ Camel_SNAKE-kebab”. GitHub (2020年4月23日). 2023年8月9日閲覧。
  8. ^ Naming Conventions in ABAP Objects”. help.sap.com. 2020年7月28日閲覧。
  9. ^ Ada Programming Guidelines”. 2021年12月19日時点のオリジナルよりアーカイブ。2021年12月19日閲覧。
  10. ^ Boost Library Requirements and Guidelines”. 2015年8月13日閲覧。
  11. ^ Eiffel Class and Feature Names” (2019年12月28日). 2023年8月9日閲覧。
  12. ^ Elixir Style Guide”. GitHub (2020年5月). 2023年8月9日閲覧。
  13. ^ Programming Rules”. 2017年8月11日閲覧。
  14. ^ GDScript Style Guide”. 2023年8月9日閲覧。
  15. ^ Code Conventions for the Java Programming Language – Naming Conventions”. Oracle. 2021年8月3日閲覧。
  16. ^ Coding Conventions”. 2023年2月3日閲覧。
  17. ^ Xen wiki”. 2017年3月15日閲覧。
  18. ^ Damian Conway (2005). Perl Best Practices. O'Reilly Media Inc.. p. 44. ISBN 978-0596001735. https://archive.org/details/perlbestpractice00conw 
  19. ^ Quick Guide to Some Sources for Naming Conventions for Oracle Database Development”. stevenfeuersteinonplsql.blogspot.com. 2020年12月30日閲覧。
  20. ^ 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]。
  21. ^ Wickham, Hadley. The tidyverse style guide. http://style.tidyverse.org/ 
  22. ^ Ruby · Naming Convention”. namingconvention.org. 2023年8月9日閲覧。
  23. ^ Naming – Rust API Guidelines”. 2023年8月9日閲覧。
  24. ^ Terraform Naming Conventions” (2022年2月). 2023年8月9日閲覧。

外部リンク 編集