camelCase, snake_case and kebab-case: naming conventions explained
Code can’t contain spaces in names, so programmers join words together in a few standard ways. Each language and tool has its own conventions, and following them makes code easier for other people (and linters) to read.
The main styles
- camelCase: first word lowercase, each later word capitalized:
userName,getAccountBalance. - PascalCase (also called UpperCamelCase): every word capitalized:
UserName,HttpClient. - snake_case: lowercase words joined by underscores:
user_name,get_account_balance. - CONSTANT_CASE (SCREAMING_SNAKE_CASE): uppercase with underscores:
MAX_RETRIES,API_BASE_URL. - kebab-case: lowercase words joined by hyphens:
user-name,main-navigation. - dot.case: words joined by dots:
app.server.port, common in configuration keys.
Which style goes where
JavaScript and TypeScript
camelCase for variables and functions (fetchUser), PascalCase for classes, types and React components (UserCard), and CONSTANT_CASE for true constants.
Python
The PEP 8 style guide asks for snake_case for variables, functions and modules (load_config), PascalCase for classes (ConfigLoader) and CONSTANT_CASE for module-level constants.
Java, C# and Kotlin
Java and Kotlin use camelCase for methods and variables and PascalCase for classes. C# uses PascalCase for methods and public properties as well as classes, and camelCase for local variables and parameters.
Go
Go uses camelCase and PascalCase, and capitalization carries meaning: a name starting with a capital letter is exported (visible outside its package), a lowercase one is not.
Ruby, Rust and PHP
Ruby and Rust use snake_case for methods and variables and PascalCase for classes, structs and types. Modern PHP code usually follows the PSR standards: camelCase methods and PascalCase classes.
CSS, HTML and URLs
kebab-case is the norm: CSS classes and custom properties (.site-header, --brand-color), HTML attributes (data-user-id), and URL slugs (/blog/how-to-write-a-cover-letter). Google recommends hyphens rather than underscores to separate words in URLs.
Databases and JSON
SQL column and table names are usually snake_case (created_at) because many databases are case-insensitive or fold names to one case. JSON APIs vary: JavaScript-centric APIs tend to use camelCase and many others use snake_case. Pick one per API and stick to it.
Acronyms: HTTPServer or HttpServer?
Style guides disagree on acronyms. Many modern guides (including those for C#, Kotlin and much JavaScript code) treat an acronym as a normal word: HttpServer, parseJson. Go and some older Java code keep acronyms uppercase: HTTPServer, parseJSON. Treating acronyms as words makes conversion between styles more predictable.
Converting between styles
Renaming a list of fields from a spreadsheet into code, or turning column headings into JSON keys, is tedious by hand. Paste the words into Wordformat’s camelCase, PascalCase, snake_case or kebab-case converter to rewrite them in one go. Each line is converted separately, so you can convert a whole list at once, and nothing leaves your browser.