snake case
What is snake case?
Snake case is a naming convention where a developer replaces spaces between words with an underscore.
Most object-oriented programming languages don't allow variable, method, class and function names to contain spaces. The snake case -- also commonly seen as snake_case -- naming convention replaces spaces with underscores to create descriptive variable and method names from multiple words without a compile time error.
The snake case name is derived from the associated imagery of underscores that represents snakes that slither on the ground between words.
Snake case examples
Here are three simple examples of code in the snake case naming convention:
- INTEREST_RATE
- increase_count_by_one
- FIND_ALL_USERS
Snake case vs. CamelCase
Snake case is a similar naming convention to kebab case -- also commonly seen as kebab-case. Both conventions can make it easier for a developer to read code because the white space between words reads in the same way as a normal sentence.
One naming convention that doesn't follow this practice is CamelCase, where there is no white space between compound words. Here's an example of snake case, CamelCase and kebab case:
- this_is_snake_case
- ThisIsCamelCase
- this-is-kebab-case
Snake_case vs. kebab-case
While both snake_case and kebab-case allow for white space between words, they differ in how a developer creates the white space.
Snake case uses an underscore to create white space. Kebab case uses a hyphen to create white space.
One potential area of concern with kebab-case is the similarity between a hyphen and a minus sign. Certain software programs can misdiagnose a hyphen as a minus sign and make it difficult for a program to diagnose bugs in an application.
Screaming snake case
In C and C++ inspired languages such as Java, Kotlin, Groovy and Clojure, constants use snake case with all upper-case letters, which is known as screaming snake case. This convention owes its name to social media and internet chat rooms, which describe users as "screaming" when they type in all capital letters. For example:
- THIS_IS_SCREAMING_SNAKE_CASE
- this_is_not_screaming_snake_case
Snake case and machine code
A developer should never use a leading or trailing underscore with snake case. A leading or trailing underscore in a variable name is typically used to denote that a method or class was machine generated.