What is snake_case?
snake_case is a naming convention where words are separated by underscores (_) and all letters are lowercase. It gets its name from the visual appearance of the underscores connecting words like a snake on the ground.
Examples:
first_name
get_user_data
total_price_with_tax
is_valid_emailWhen to Use snake_case
- Python: The standard for variables, functions, and file names
- Ruby: Variables and method names
- Rust: Variables and function names
- Database: Table names and column names (SQL)
- File names: Especially on Unix/Linux systems
- Environment variables: Configuration settings
Examples in Code
Python Variables:
user_name = "John Doe"
total_items = 42
is_logged_in = TrueFunction Names:
def calculate_total_price():
pass
def get_user_by_id(user_id):
pass
def validate_email_address(email):
passDatabase Schema:
CREATE TABLE user_accounts (
user_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email_address VARCHAR(100)
);Comparison with Other Conventions
Convention
snake_case
camelCase
PascalCase
kebab-case
CONSTANT_CASE
Example
user_nameuserNameUserNameuser-nameUSER_NAMEBest Practices
- ✓ Use descriptive names:
user_account_balanceinstead ofuab - ✓ Keep consistent with language conventions
- ✓ Perfect for database column names
- ✓ Excellent for file names (cross-platform compatible)
- ✓ Use UPPER_SNAKE_CASE for constants
- ✗ Don't mix with camelCase in the same context
- ✗ Avoid starting or ending with underscores (unless specific convention)
- ✗ Don't use double underscores (__ reserved in Python)
Language-Specific Guidelines
Python (PEP 8)
snake_case is the standard for function names, variable names, and module names. Use UPPER_SNAKE_CASE for constants.
def my_function():
MAX_CONNECTIONS = 100Ruby
Ruby uses snake_case for methods, variables, and file names. Class names use PascalCase.
def calculate_total
# method body
endSQL Databases
snake_case is widely adopted for table names and column names across most SQL databases.
SELECT first_name, last_name FROM user_accounts;Why Use snake_case?
- • Readability: Underscores clearly separate words
- • Convention: Standard in Python, Ruby, and databases
- • Accessibility: Easier to read for some people than camelCase
- • Case-insensitive: Works in case-insensitive systems
- • File names: Safe for all operating systems
- • No ambiguity: Clear word boundaries unlike camelCase