snake_case vs kebab-case
Understanding the key differences between underscores and hyphens
Quick Answer:
snake_case: Words separated by underscores. Example: user_email_address
kebab-case: Words separated by hyphens. Example: user-email-address
What is snake_case?
snake_case is a naming convention where words are written in lowercase and separated by underscores (_). It's called "snake" because the underscores create a long, low appearance resembling a snake.
Examples of snake_case:
first_nameuser_email_addresscalculate_total_priceis_user_logged_in
What is kebab-case?
kebab-case (also called dash-case or hyphen-case) is a naming convention where words are written in lowercase and separated by hyphens (-). It's called "kebab" because the hyphens resemble skewers through meat on a kebab.
Examples of kebab-case:
first-nameuser-email-addresscalculate-total-priceis-user-logged-in
Key Differences
| Aspect | snake_case | kebab-case |
|---|---|---|
| Separator | Underscore (_) | Hyphen (-) |
| Common Use | Python variables, database columns | URLs, CSS classes, file names |
| Example | user_profile_data | user-profile-data |
| Programming | Valid identifier | Not valid in most languages |
When to Use snake_case
Use snake_case for:
- Python code:
def get_user_data(): - Ruby code:
user_email = "test@example.com" - Database columns:
SELECT user_id, email_address FROM users - Environment variables:
DATABASE_URL - Constants in Python:
MAX_UPLOAD_SIZE = 1024
When to Use kebab-case
Use kebab-case for:
- URLs:
example.com/user-profile - CSS class names:
class="user-profile-card" - HTML attributes:
data-user-id="123" - File names:
user-profile-component.tsx - Git branch names:
feature/user-authentication
Why kebab-case for URLs?
kebab-case is preferred for URLs because:
- SEO-friendly: Search engines recognize hyphens as word separators
- Readability: Easier to read in browser address bars
- No special encoding needed: Underscores must be URL-encoded (%5F)
- Google recommendation: Google explicitly recommends hyphens over underscores
Why snake_case in Programming?
snake_case works in programming because:
- Valid identifier: Underscores are allowed in variable names
- No operator confusion: Hyphens are minus operators in most languages
- Language convention: Python, Ruby, and others use it as standard
- Database-friendly: Works well in SQL without quotes
Language-Specific Conventions
Python
# snake_case for variables and functions
user_email = "john@example.com"
def get_user_data():
return user_dataCSS
/* kebab-case for class names */
.user-profile-card {
background-color: white;
}
.email-input-field {
border: 1px solid gray;
}URLs
<!-- kebab-case for URLs -->
https://example.com/user-profile
https://example.com/blog/snake-case-vs-kebab-case
https://example.com/product-details/blue-widgetCommon Mistakes to Avoid
- ❌ Using snake_case in URLs:
example.com/user_profile - ❌ Using kebab-case in Python:
user-email = "test@example.com"(syntax error) - ❌ Mixing conventions:
user_Email-Address - ❌ Using spaces:
user profile data(not a naming convention)
Best Practices
- Be consistent: Choose one and stick with it in the same context
- Follow conventions: Use snake_case for Python, kebab-case for URLs
- Consider SEO: Always use kebab-case for URLs
- Use lowercase: Both conventions use all lowercase letters
Quick Conversion
Need to convert between snake_case and kebab-case? Try our free converter:
Conclusion
Both snake_case and kebab-case are valuable naming conventions. Use snake_case for programming (especially Python) and database columns. Use kebab-case for URLs, CSS classes, and file names. The choice depends on the context and language conventions.