Sergey Nivens - Fotolia

Camel case vs. snake case: What's the difference?

The use of white space is what separates camel case and snake case. Camel case has none, while snake case uses underscores. Let's review some examples to make this clear.

Camel case and snake case stand at opposite ends of the variable naming convention spectrum.

When multiple words are used to form a variable, camel case joins those words together, without any white space, and delineates the start of each new word with a capital letter.

In contrast, snake case uses an underscore between words to create separation. Variable names cannot contain spaces, which reduces readability when multiple words must be used to describe a variable's purpose. The underscore in a snake-cased variable tries to minimize this shortcoming.

Snake case examples

The following variable names follow the snake case naming convention:

  • this_is_snake_case
  • build_docker_image
  • run_javascript_function
  • call_python_method
  • ruby_loves_snake_case

Some languages, like Java, use snake case with all uppercase letters to indicate a variable is a constant. For example, INTEREST_RATE would be a Java constant. When all of the snake-cased letters are uppercase, this is known as screaming snake case.

Camel case examples

The following are examples of variables that use the camel case naming convention:

  • FileNotFoundException
  • toString
  • SpringBoot
  • TomcatServer
  • getJson

When the first letter of a camel-cased variable is uppercase, it is also known as Pascal case or upper camel case. When it is not, it is often referred to as lower camel case.

Snake case vs. camel case usage

While individual languages specify their own naming conventions, there is little consistency across languages regarding camel case versus snake case usage.

Languages such as Java and Kotlin, which have a C and C++ heritage, use lower camel case for variables and methods, and upper camel case for reference types such as enums, classes and interfaces. Screaming snake case is used for variables.

Scripting languages, as demonstrated in the Python style guide, recommend snake case in the instances where C-based languages use camel case.

Camel case vs. snake case in JavaScript

When browser-based programming with HTML and JavaScript first emerged, snake case was a common approach to variable and method naming. But as object-oriented derivations of JavaScript, such as TypeScript, AngularJS and Node.JS, gained popularity, camel case has become the standard.

Variable naming conventions, such as snake case or camel case, are standardized differently for each programming language. Furthermore, independent software development teams often break these rules when corner cases are encountered.

When you learn a new software development language, or join a new development team, always be sure to appraise yourself of the variable naming conventions used to ensure that all your co-developers are speaking the same language.

Dig Deeper on Core Java APIs and programming techniques