Why is it getElementById in JavaScript but get_element_by_id in Python? Why is a CSS class .main-container but a React component MainContainer? Different programming languages and contexts have different conventions for naming things. Understanding these conventions makes your code more readable and professional.
The Main Case Styles
camelCase
First word lowercase, subsequent words capitalized. No separators.
camelCase
getUserName
isValidEmail
calculateTotalPrice
Used in:
- JavaScript/TypeScript (variables, functions)
- Java (variables, methods)
- Swift, Kotlin, C# (variables, local names)
- JSON property names (commonly)
PascalCase (UpperCamelCase)
Every word capitalized. No separators.
PascalCase
UserAccount
HttpClient
DatabaseConnection
Used in:
- Classes in most languages
- C# (methods, properties)
- React components
- TypeScript interfaces and types
- .NET namespaces
snake_case
All lowercase with underscores between words.
snake_case
user_name
get_user_by_id
max_retry_count
Used in:
- Python (variables, functions, modules)
- Ruby (variables, methods)
- Rust (variables, functions)
- Database column names
- PostgreSQL
SCREAMING_SNAKE_CASE
All uppercase with underscores.
SCREAMING_SNAKE_CASE
MAX_CONNECTIONS
API_BASE_URL
DEFAULT_TIMEOUT
Used in:
- Constants in most languages
- Environment variables
- Preprocessor macros (C/C++)
kebab-case
All lowercase with hyphens between words.
kebab-case
main-container
user-profile
api-endpoint
Used in:
- CSS class names
- HTML attributes
- URL slugs
- npm package names
- Lisp dialects
Train-Case
Every word capitalized with hyphens.
Train-Case
Content-Type
X-Custom-Header
Used in:
- HTTP headers
- MIME types
flatcase
All lowercase, no separators.
flatcase
username
getall
parseint
Used in:
- Rarely recommended
- Some Unix commands
- Package names (sometimes)
Language-Specific Conventions
JavaScript/TypeScript
// Variables and functions: camelCase
const userName = 'John';
function getUserById(id) { }
// Classes and React components: PascalCase
class UserService { }
function UserProfile() { }
// Constants: SCREAMING_SNAKE_CASE
const MAX_RETRIES = 3;
const API_URL = 'https://api.example.com';
// Private by convention: _prefix or #prefix
const _internalValue = 'hidden';
class Example {
#privateField = true;
}
Python
# Variables and functions: snake_case
user_name = 'John'
def get_user_by_id(user_id):
pass
# Classes: PascalCase
class UserService:
pass
# Constants: SCREAMING_SNAKE_CASE
MAX_RETRIES = 3
API_URL = 'https://api.example.com'
# Private by convention: _prefix
_internal_value = 'hidden'
def _private_function():
pass
# Name mangling: __prefix
class Example:
__very_private = True
Java/C#
// Variables and methods: camelCase
String userName = "John";
public User getUserById(int id) { }
// Classes, interfaces, enums: PascalCase
public class UserService { }
public interface IUserRepository { } // C# uses I prefix
// Constants: SCREAMING_SNAKE_CASE
public static final int MAX_RETRIES = 3;
private const string API_URL = "https://api.example.com"; // C#
// Packages: lowercase.dotted (Java)
// Namespaces: PascalCase (C#)
package com.example.users;
namespace Company.Project.Users;
CSS
/* Classes: kebab-case */
.main-container { }
.user-profile-card { }
.btn-primary { }
/* IDs: kebab-case */
#main-nav { }
#user-dropdown { }
/* BEM convention */
.block { }
.block__element { }
.block--modifier { }
.user-card { }
.user-card__header { }
.user-card--featured { }
SQL/Databases
-- Tables: snake_case (singular or plural varies)
CREATE TABLE user_accounts (
-- Columns: snake_case
user_id SERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email_address VARCHAR(100),
created_at TIMESTAMP
);
-- Some prefer PascalCase for tables
CREATE TABLE UserAccounts (
UserID INT PRIMARY KEY,
FirstName VARCHAR(50)
);
URLs and File Names
# URL slugs: kebab-case
/user-profile
/api/get-all-users
/articles/my-first-post
# File names vary by convention
user-service.js // kebab-case (common in JS)
user_service.py // snake_case (Python modules)
UserService.cs // PascalCase (C# files match class names)
UserService.java // PascalCase (Java files match class names)
Conversion Functions
JavaScript
// To camelCase
function toCamelCase(str) {
return str
.replace(/[-_\s]+(.)?/g, (_, c) => c ? c.toUpperCase() : '')
.replace(/^./, c => c.toLowerCase());
}
// To PascalCase
function toPascalCase(str) {
return str
.replace(/[-_\s]+(.)?/g, (_, c) => c ? c.toUpperCase() : '')
.replace(/^./, c => c.toUpperCase());
}
// To snake_case
function toSnakeCase(str) {
return str
.replace(/([a-z])([A-Z])/g, '$1_$2')
.replace(/[-\s]+/g, '_')
.toLowerCase();
}
// To kebab-case
function toKebabCase(str) {
return str
.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/[_\s]+/g, '-')
.toLowerCase();
}
// Usage
toCamelCase('user_name'); // 'userName'
toPascalCase('user_name'); // 'UserName'
toSnakeCase('userName'); // 'user_name'
toKebabCase('userName'); // 'user-name'
Python
import re
def to_camel_case(s):
components = re.split('[-_\\s]+', s)
return components[0].lower() + ''.join(x.title() for x in components[1:])
def to_pascal_case(s):
components = re.split('[-_\\s]+', s)
return ''.join(x.title() for x in components)
def to_snake_case(s):
s = re.sub('([a-z])([A-Z])', r'\1_\2', s)
return re.sub('[-\\s]+', '_', s).lower()
def to_kebab_case(s):
s = re.sub('([a-z])([A-Z])', r'\1-\2', s)
return re.sub('[_\\s]+', '-', s).lower()
# Usage
to_camel_case('user_name') # 'userName'
to_snake_case('userName') # 'user_name'
Quick Reference Table
| Convention | Example | Common Use |
|---|---|---|
| camelCase | getUserName |
JS/Java variables, methods |
| PascalCase | UserAccount |
Classes, React components |
| snake_case | get_user_name |
Python, Ruby, databases |
| SCREAMING_SNAKE_CASE | MAX_VALUE |
Constants, env vars |
| kebab-case | user-profile |
CSS, URLs, npm packages |
| Train-Case | Content-Type |
HTTP headers |
Best Practices
1. Be Consistent
Pick a convention and stick with it throughout your project.
2. Follow Language Conventions
Don't use snake_case in JavaScript or camelCase in Python—it will look out of place.
3. Use Meaningful Names
The case matters less than clarity:
// Bad
const x = getUserData();
const temp = processIt();
// Good
const currentUser = getUserData();
const processedResults = processUserInput();
4. Match Your Framework
React expects PascalCase components. Django expects snake_case. Go with the flow.
5. Configure Your Linter
Tools like ESLint, Pylint, and RuboCop can enforce naming conventions automatically.
Common Pitfalls
Acronyms
How to handle acronyms varies:
// JavaScript: Keep short acronyms uppercase
const userID = 1; // or userId
const parseHTML = () => {}; // or parseHtml
// Some prefer lowercase after first letter for readability
const userId = 1;
const parseHtml = () => {};
Mixed Conventions in APIs
When consuming external APIs, you often need to convert:
// API returns snake_case
const apiResponse = { user_name: 'John', email_address: 'john@example.com' };
// Convert to camelCase for JS
const user = {
userName: apiResponse.user_name,
emailAddress: apiResponse.email_address
};
Summary
| Language | Variables | Functions | Classes | Constants |
|---|---|---|---|---|
| JavaScript | camelCase | camelCase | PascalCase | SCREAMING_SNAKE |
| Python | snake_case | snake_case | PascalCase | SCREAMING_SNAKE |
| Java | camelCase | camelCase | PascalCase | SCREAMING_SNAKE |
| C# | camelCase | PascalCase | PascalCase | PascalCase/SCREAMING |
| CSS | kebab-case | - | - | - |
| SQL | snake_case | snake_case | - | - |
The right case depends on context. Learn the conventions for your language and be consistent!
Need to convert between cases? Try our Case Converter!