GameParticles
Text

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.

StyleExampleAlso called
camelCaseuserAccountIdlower camel case, dromedary case
PascalCaseUserAccountIdupper camel case, StudlyCaps
snake_caseuser_account_idlower snake case
CONSTANT_CASEUSER_ACCOUNT_IDscreaming snake case, upper snake case
kebab-caseuser-account-iddash 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.

LanguageVariables and functionsClasses and typesConstants
JavaScript, TypeScriptcamelCasePascalCaseCONSTANT_CASE
JavacamelCasePascalCaseCONSTANT_CASE
Kotlin, SwiftcamelCasePascalCaseVaries by style guide
C#camelCase for locals and parameters, PascalCase for methods and propertiesPascalCasePascalCase
Pythonsnake_casePascalCaseCONSTANT_CASE
Rubysnake_casePascalCaseCONSTANT_CASE
Rustsnake_casePascalCaseCONSTANT_CASE
GocamelCase, or PascalCase when exportedPascalCase when exportedSame as variables
PHP (PSR standards)camelCase for methodsPascalCaseCONSTANT_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 userId and userid can end up as the same column. Underscores survive that; capitals do not.
  • Environment variables. By long-standing convention these are CONSTANT_CASE, such as DATABASE_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:

  1. Follow the language's style guide.
  2. If you are adding to an existing codebase, match what is already there.
  3. 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 mixes userId, user_id and UserID forces 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_CASE environment 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.

Tools in this guide

Frequently asked questions

What is camel case?

Camel case joins words with no separator and capitalizes the first letter of every word except the first, as in userAccountId. The capitals in the middle resemble a camel's humps. It is the standard style for variables and functions in JavaScript, Java and Swift.

What is snake case?

Snake case writes every word in lowercase and joins them with underscores, as in user_account_id. It is the standard for variables and functions in Python, Ruby and Rust, and is the most common style for database table and column names.

What is the difference between Pascal case and camel case?

Only the first letter. Pascal case capitalizes every word including the first, as in UserAccount, while camel case leaves the first word lowercase, as in userAccount. Pascal case is used for class and type names in most languages.

What is kebab case used for?

Kebab case joins lowercase words with hyphens, as in user-account-id. It is used for URL slugs, CSS class names, HTML attributes, file names and command-line flags. Most programming languages cannot use it for identifiers because the hyphen is the minus operator.

More text guides

JSON Formatter
Developer · Format, validate and minify JSON with syntax highlighting.
↵
Base64 Encode / Decode
Developer · Encode text to Base64 or decode Base64 back to plain text.
↵
URL Encoder / Decoder
Developer · Encode or decode URL components and query strings.
↵
Hash Generator
Developer · Generate MD5, SHA-1, SHA-256 and SHA-512 hashes.
↵
UUID Generator
Developer · Generate UUIDs v4 (random) in bulk with one click.
↵
Regex Tester
Developer · Test regular expressions against text with live match highlighting.
↵
Word Counter
Text · Count words, characters, sentences, paragraphs and reading time.
↵
Case Converter
Text · Convert text between UPPER, lower, Title, camelCase, snake_case and more.
↵
List Sorter
Text · Sort, reverse, deduplicate and shuffle lists of text.
↵
Markdown Preview
Text · Write Markdown and see a live rendered preview side by side.
↵
Percentage Calculator
Numbers · Calculate percentages, increases, decreases and what % one number is of another.
↵
Color Converter
Colors · Convert colors between HEX, RGB, HSL and HSV instantly.
↵
Snake
Arcade · Eat food, grow longer, don't hit the walls. A timeless classic.
↵
2048
Puzzle · Slide tiles and combine them to reach the legendary 2048 tile.
↵
Word Grid
Puzzle · Guess the hidden five letter word in six tries using color clues.
↵
Sudoku
Puzzle · Fill the 9x9 grid with logic. Four difficulties from easy to expert.
↵
Minesweeper
Strategy · Use number clues to clear the minefield. Classic logic, three levels.
↵
Breakout
Arcade · Smash every brick with paddle and ball. Power-ups, combos, endless levels.
↵
Typing Practice
Casual · Build your WPM and accuracy with timed drills from 30 seconds to 5 minutes. Find your weak keys.
↵
TypeRunner
Arcade · Multiplayer typing race. Race friends by link, beat computer runners, or chase your own ghost.
↵
Typing Blocks
Arcade · Type each word before its wooden block lands. Miss, and the pile climbs toward the line.
↵
Bubble Shooter
Arcade · Aim, bank shots off the walls, and pop groups of three or more bubbles.
↵
Road Rush
Arcade · Race seven rivals through traffic and punch your way to the front.
↵