Jump to content

Snake case

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 216.123.208.30 (talk) at 21:03, 4 August 2016 (Examples of languages that use snake case as convention). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Snake case (or snake_case) is the practice of writing compound words or phrases in which the elements are separated with one underscore character (_) and no spaces, with each element's initial letter usually lowercased within the compound and the first letter either upper or lower case—as in "foo_bar" and "Hello_world". It is commonly used in computer code for variable names, and function names, and sometimes computer filenames.[1] At least one study found that readers can recognize snake case values more quickly than CamelCase.[2]

History

The use of underscores as word separators in identifiers in programming languages is old, dating to the late 1960s. It is particularly associated with C, being found in The C Programming Language (1978), and contrasted with Pascal case, an older term for CamelCase. However, the convention traditionally had no specific name: the Python style guide refers to it simply as "lower_case_with_underscores".[3] The name "snake_case" comes from the Ruby community, where it was coined in 2004 by Gavin Kistner, writing:[4]

(BTW...what *do* you call that naming style? snake_case? That's what I'll call it until someone corrects me.)

The name is evidently by contrast with CamelCase (as the subject of the message notes), continuing the animal theme with a long creature, low to the ground.

As of 2015 there are no standard names for other delimiter-separated naming conventions for multiple-word identifiers, though there are various terms with limited usage, such as lisp-case, kebab-case, SCREAMING_SNAKE_CASE, etc.[5][6][7]

Examples of languages that use snake case as convention

  • OCaml, for variable and function names
  • C++, for the standard library[8] and Boost[9]
  • Erlang, for function names
  • Perl
  • PHP, for functions, but not class methods. Older conventions were often using snake case for every type of identifiers, except classes and namespaces.
  • Python, for variable names, function names, and method names[3]
  • Ruby
  • Rust, for variable and function names[10]
  • Elixir, for variable and function names[11]

See also

References

  1. ^ e.g. in Python and Ruby; see Naming convention (programming)
  2. ^ "An Eye Tracking Study on camelCase and under_score Identifier Styles".
  3. ^ a b "PEP 0008 -- Style Guide for Python Code". 2001-07-05. {{cite web}}: Unknown parameter |authors= ignored (help)
  4. ^ Gavin Kistner (2004-02-23). "Appropriate use of camelCase". Newsgroupcomp.lang.ruby. Usenet: HBn_b.379957$xy6.2073499@attbi_s02. Retrieved August 13, 2015.
  5. ^ "StackOverflow - What's the name for snake_case with dashes?".
  6. ^ "Programmers - If this is camelCase what-is-this?".
  7. ^ "Camel_SNAKE-kebab".
  8. ^ "Library Design Guidelines". Retrieved August 13, 2015.
  9. ^ "Boost Library Requirements and Guidelines". Retrieved August 13, 2015.
  10. ^ "Rust Naming Conventions".
  11. ^ "Elixir Style Guide".