TC
ProgrammingJanuary 14, 2025·4 min read

What is Snake Case? Definition, Examples & Where It's Used

What is Snake Case?

snake_case is a naming convention where words are written in all lowercase and separated by underscores (_). It gets its name from the flat, ground-level appearance of the underscores — like a snake lying on the ground.

Examples: first_name, get_user_by_id, background_color, max_retry_count


Origin of snake_case

Snake case has been used in Unix-based systems and scripting languages for decades. Python's PEP 8 style guide, published in 2001, formalized its use for Python code, which significantly boosted its popularity.


Where snake_case is Used

Programming Languages

  • Python — PEP 8 mandates snake_case for variables, functions, module names, and method names
  • Ruby — community standard for methods, variables, and file names
  • PHP — commonly used for function names (though not strictly enforced)
  • Rust — convention for variable and function names

Databases

  • SQL — column names, table names, and stored procedure names (e.g., user_id, created_at)
  • PostgreSQL — strongly prefers snake_case for identifiers

File Systems

  • Linux/macOS file names — underscores over spaces for shell compatibility
  • Python module files — e.g., my_module.py, data_processor.py

Variations of snake_case

VariantExampleUsage

|---|---|---|

snake_case`user_name`Variables, functions
SCREAMING_SNAKE_CASE`MAX_VALUE`Constants, env vars
Snake_Title_Case`User_Name`Rare; some legacy systems

snake_case vs Other Conventions

ConventionExampleBest For

|---|---|---|

snake_case`user_name`Python, SQL
camelCase`userName`JavaScript, Java
kebab-case`user-name`CSS, URLs
PascalCase`UserName`Class names

Best Practices

  • Use lowercase onlymy_variable, not My_Variable
  • Keep names descriptiveget_user_by_id is better than g_u_b_i
  • Avoid excessive underscoresget__user___name is hard to read
  • Use SCREAMING_SNAKE_CASE for constants and environment variables