Camel Case vs Snake Case: Naming Conventions by Language
Camel case vs snake case, plus Pascal case and kebab case: what each naming style looks like, which languages use which, and how to convert between them.
Updated
Camel case joins words by capitalizing each one after the first (userAccountId), while snake case keeps everything lowercase and joins words with underscores (user_account_id). Neither is better in general: each language has a convention, and the right choice is whatever that language's style guide says. JavaScript and Java use camel case for variables; Python, Ruby and Rust use snake case.
The styles at a glance
Identifiers cannot contain spaces, so every naming style is a way of marking where one word ends and the next begins.
| Style | Example | Also called |
|---|---|---|
| camelCase | userAccountId | lower camel case, dromedary case |
| PascalCase | UserAccountId | upper camel case, StudlyCaps |
| snake_case | user_account_id | lower snake case |
| CONSTANT_CASE | USER_ACCOUNT_ID | screaming snake case, upper snake case |
| kebab-case | user-account-id | dash case, hyphen case, spinal case |
The names are informal. Camel case refers to the humps the capitals make, snake case to the underscores lying flat on the line, and kebab case to words on a skewer. Pascal case is named after the Pascal programming language, where the style was common.
Conventions by language
These are the conventions set by each language's official or dominant style guide.
| Language | Variables and functions | Classes and types | Constants |
|---|---|---|---|
| JavaScript, TypeScript | camelCase | PascalCase | CONSTANT_CASE |
| Java | camelCase | PascalCase | CONSTANT_CASE |
| Kotlin, Swift | camelCase | PascalCase | Varies by style guide |
| C# | camelCase for locals and parameters, PascalCase for methods and properties | PascalCase | PascalCase |
| Python | snake_case | PascalCase | CONSTANT_CASE |
| Ruby | snake_case | PascalCase | CONSTANT_CASE |
| Rust | snake_case | PascalCase | CONSTANT_CASE |
| Go | camelCase, or PascalCase when exported | PascalCase when exported | Same as variables |
| PHP (PSR standards) | camelCase for methods | PascalCase | CONSTANT_CASE |
A few details worth knowing:
- Python's rules come from PEP 8, which calls Pascal case "CapWords".
- Rust's compiler warns when a name does not follow the convention, so the style is effectively enforced.
- In Go, the case of the first letter is not just style. A name that starts with a capital letter is exported from its package; a lowercase one is private. Go's own guidance is to use mixed caps rather than underscores.
- C# uses Pascal case more widely than other languages: methods, properties and public members all start with a capital.
Outside programming languages
Naming styles also apply to the things code touches.
- CSS. Properties and most class names use kebab case:
background-color,.nav-item. - HTML. Custom elements must contain a hyphen, and
data-attributes are written in kebab case. - URLs. Kebab case is the norm for slugs, as in
/learn/camel-case-vs-snake-case. Hyphens are treated as word separators by search engines, and underscores can disappear under link underlining. - SQL. Snake case is the most common choice for tables and columns. Many databases fold unquoted names to a single case, so
userIdanduseridcan end up as the same column. Underscores survive that; capitals do not. - Environment variables. By long-standing convention these are
CONSTANT_CASE, such asDATABASE_URL. - JSON. There is no single rule. Camel case is common in APIs written for JavaScript clients, and snake case is common in APIs built with Python or Ruby. Pick one and keep it consistent.
- File names. Kebab case or snake case avoids problems with spaces and with file systems that ignore letter case.
Why kebab case is rare in code
In most languages user-name is parsed as user minus name, so a hyphen cannot appear inside an identifier. That is why kebab case lives in CSS, URLs, file names and command-line flags like --dry-run, and not in variable names. Lisp-family languages such as Clojure are the main exception: they use prefix notation for arithmetic, so hyphenated names are legal and standard.
Camel case or snake case: which reads better?
The argument for snake case is that underscores look like spaces, so long names are easier to scan: maximum_retry_count_exceeded against maximumRetryCountExceeded. The argument for camel case is that it is shorter and needs no extra key. Small studies have compared recognition speed for the two styles with mixed results, and familiarity seems to matter most.
In practice the question is settled by context, not preference:
- Follow the language's style guide.
- If you are adding to an existing codebase, match what is already there.
- Where two conventions meet, such as a JavaScript front end reading snake case JSON from a Python API, convert at the boundary so each side stays consistent internally.
Consistency beats any individual style. A codebase that mixesuserId,user_idandUserIDforces every reader to remember which spelling is used where.
Acronyms and numbers
Camel case and Pascal case get awkward with acronyms. Is it parseHTMLString or parseHtmlString, userID or userId? Style guides disagree. Google's Java guide treats acronyms as ordinary words (XmlHttpRequest), Microsoft's .NET guidelines capitalize two-letter acronyms and treat longer ones as words (IOStream, HtmlElement), and Go keeps initialisms in a single case (userID, HTTPServer). The lowercase-after-the-first-letter approach has one practical advantage: it converts cleanly to snake case, because HtmlParser becomes html_parser and not h_t_m_l_parser.
Snake case and kebab case do not have this problem, since every word is lowercase.
Numbers are another edge. No mainstream language allows an identifier to start with a digit, so 2ndAttempt has to become secondAttempt or attempt2.
Converting between styles
Renaming a list of fields by hand is slow and easy to get wrong. The Case Converter takes a phrase and shows it in camelCase, PascalCase, snake_case, kebab-case and CONSTANT_CASE at the same time, each with a copy button. Type user account id and you get userAccountId, UserAccountId, user_account_id, user-account-id and USER_ACCOUNT_ID.
For the cleanest results, enter the words separated by spaces. The snake, kebab and constant outputs use the spaces to place their separators, so pasting an existing userAccountId will not split it back into three words. The camelCase and PascalCase outputs do accept snake case or kebab case input, because they treat any non-letter character as a word boundary.
Typical uses:
- Turning column headings from a spreadsheet into snake case column names
- Turning an article title into a kebab case URL slug
- Turning a setting's description into a
CONSTANT_CASEenvironment variable name
If you are matching identifier patterns in a search or a linter rule, the regex cheat sheet for beginners covers the character classes involved, and you can test a pattern in the Regex Tester. For capitalization of headings and prose instead of identifiers, see title case vs sentence case.
Try it
Type a phrase into the Case Converter to see it in camelCase, PascalCase, snake_case, kebab-case and CONSTANT_CASE side by side, then copy the one your language expects.