Snake case facts for kids
Snake case (also written as snake_case) is a way to write words that are joined together. Instead of using a space between words, you use an underscore symbol (`_`). Also, the first letter of each word is usually changed to a small letter.
For example, instead of writing "hello world" with a space, in snake case you would write "hello_world". Another example is "foo_bar".
This style is very common in computer code. It helps programmers read and understand the code easily. Some studies have even shown that people can read words written in snake case faster than words written in CamelCase.
Contents
What is Snake Case?
Snake case is a naming style where you connect words using an underscore (`_`). Imagine a snake slithering between words – that's why it's called snake case! This method is often used for names of things in computer programs, like variables or functions.
For example:
- `user_name` (instead of "User Name")
- `total_score` (instead of "Total Score")
- `first_name` (instead of "First Name")
This makes the names clear and easy to read, especially when they are long.
How is Snake Case Different?
There are other ways to write joined words in computer code. One common way is CamelCase.
Snake Case vs. Camel Case
- Snake case uses underscores and usually all small letters (e.g., `my_variable`).
- Camel case joins words without spaces or underscores. The first letter of each new word (except sometimes the very first word) is a capital letter (e.g., `myVariable` or `MyVariable`). It's called camel case because the capital letters look like the humps of a camel!
Both styles help make names easy to read in programming, but different programming languages often prefer one over the other.
Train Case
If you use underscores like in snake case, but the first letter of each word is a capital letter, it's called Train-Case. For example, `My_Variable` or `Train_Case`. This style is less common than snake case or camel case.
Where is Snake Case Used?
Many programming languages use snake case as their main style for naming things. This helps keep the code looking neat and consistent.
Here are some programming languages that often use snake case:
When you learn to code in these languages, you will often see and use snake case yourself!
Images for kids
-
This picture shows a piece of computer code from the Linux kernel. You can see how snake case is used for names like "alx_pci_read_config_byte" and "alx_pci_write_config_byte".