Sergey Nivens - Fotolia
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:
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:
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.
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 Software programming languages
Related Q&A from Cameron McKenzie
Learn the difference between compiled languages like Go and interpreted ones like Java. One language is platform agnostic. The other can run a bit ... Continue Reading
It's easy to confuse camel case and Pascal case. We'll show you the difference between the two common variable naming conventions, along with some ... Continue Reading
These two approaches both aim to improve Java performance and can be done together. But there is a fundamental difference between instrumentation and... Continue Reading