TTKTheTextKit
Text Cleanup7 min read

camelCase vs snake case vs kebab-case: Which to Use and How to Convert

camelCase vs snake case vs kebab-case: which style each language and URL expects, the rules from PEP 8 and Google, and how to convert names correctly.

By TheTextKit TeamPublished

camelCase vs snake_case: the name user first name written in five styles from camelCase to CONSTANT_CASE

A card lists the name user first name in five styles. camelCase is userFirstName, PascalCase is UserFirstName, snake_case is user_first_name, kebab-case is user-first-name and CONSTANT_CASE is USER_FIRST_NAME. Each style has a colored label.

camelCase vs snake_case vs kebab-case is a choice between three ways to write a name that has several words in it, in places where a space is not allowed. They look like a matter of taste, but each language, format and web address has a convention, and breaking it makes code harder to read and URLs harder for search engines to understand. This guide sets out which style goes where, with the rules from the official style guides, and shows how a converter should treat the awkward cases such as acronyms. We also found that our own converter got many of them wrong and fixed it.

The same name in five styles

Here is one name, written in each of the five styles the Case Converter supports:

StyleExampleRule
camelCaseuserFirstNameFirst word lowercase, each other word starts with a capital
PascalCaseUserFirstNameEvery word starts with a capital
snake_caseuser_first_nameAll lowercase, words joined with underscores
kebab-caseuser-first-nameAll lowercase, words joined with hyphens
CONSTANT_CASEUSER_FIRST_NAMEAll capitals, words joined with underscores

The names come from the way they look. camelCase has humps in the middle, snake_case lies along the ground, and kebab-case looks like words on a skewer. They all carry the same information, and a good converter can move between any two of them.

camelCase vs snake_case: which style goes where

The right choice is whatever the place you are writing expects. We checked the official sources:

WhereStyleSource
Python functions and variablessnake_casePEP 8
Python classesCapWords (PascalCase)PEP 8
Python constantsALL_CAPS_WITH_UNDERSCORESPEP 8
JavaScript variables and methodslowerCamelCaseGoogle JavaScript Style Guide
JavaScript classesUpperCamelCaseGoogle JavaScript Style Guide
JavaScript constantsCONSTANT_CASEGoogle JavaScript Style Guide
URLskebab-caseGoogle URL structure guidance

PEP 8 adds that mixedCase, which is another name for camelCase, is allowed only where it is already the prevailing style, to keep backward compatibility. The Google JavaScript Style Guide also says that file names are all lowercase with underscores or dashes.

Naming styles by place: Python snake_case, JavaScript camelCase, JavaScript constants CONSTANT_CASE, URLs kebab-case

A table of four places and the naming style each expects. Python variables and functions use snake case. JavaScript variables and methods use camel case. JavaScript constants use constant case. URLs use kebab case.

Each language and format has its own convention.

CSS follows the same pattern. Its properties, such as background-color and font-size, are written in kebab-case, so class names in that style fit naturally next to them.

Hyphens and underscores in URLs

Google's URL structure guidance recommends hyphens instead of underscores to separate words, because hyphens help users and search engines identify the concepts in the URL. It notes that underscores have long been used in programming to join connected concepts, which makes it unclear whether the words are meant to be read together or apart. Its example is summer-clothing against summer_clothing. Our guide to URL slug best practices covers the rest of the rules for slugs.

Google's URL advice: summer-clothing with a hyphen is recommended and summer_clothing with an underscore is not

Two cards compare two URL paths. The path with a hyphen, summer-clothing, is marked as recommended by Google. The path with an underscore, summer_clothing, is marked as not recommended.

In a URL, use a hyphen to separate words.

Is one style easier to read?

People have argued about this for decades, and there is some evidence. A 2013 study in Empirical Software Engineering by Binkley and colleagues tested camelCase and underscore identifiers in five studies with 150 participants. It found that beginner programmers did better with camelCase, while experienced programmers were not affected by the style. The practical lesson is that, in the camelCase vs snake_case debate, consistency matters more than the choice. Mixing styles in one codebase costs more than either style does.

How a converter should split a name

A converter first has to find the words. For user first name that is easy. The hard cases are names that are already joined:

InputcamelCasesnake_case
XMLHttpRequestxmlHttpRequestxml_http_request
getHTTPResponsegetHttpResponseget_http_response
YouTube importeryouTubeImporteryou_tube_importer
es6 modulees6Modulees6_module

The Google JavaScript Style Guide gives a four-step method. First convert to ASCII and remove apostrophes. Then split the text into words at spaces and punctuation. Next lowercase everything and capitalize the first letter of the words that need it. Last, join the words. It says an acronym is treated as an ordinary word, so XML HTTP request becomes xmlHttpRequest and not XMLHTTPRequest. Our converter gives the same results for the guide's two examples, xmlHttpRequest and youTubeImporter.

XMLHttpRequest converted: camelCase xmlHttpRequest, snake_case xml_http_request, kebab-case xml-http-request

A table of three conversions of the name XMLHttpRequest. camelCase gives xmlHttpRequest. snake case gives xml underscore http underscore request. kebab case gives xml hyphen http hyphen request.

The acronym is treated as a word, so the boundaries are found.

What simple converters get wrong

Our previous converter removed every character that was not a plain English letter or digit, and it never looked for word boundaries inside a name. We ran it against the new one on typical inputs:

InputStyleOld converterNew converter
café au laitcamelCasecafAuLaitcaféAuLait
HELLO WORLDcamelCasehELLOWORLDhelloWorld
don't stopcamelCasedonTStopdontStop
helloWorldsnake_casehelloworldhello_world
foo-bar.bazsnake_casefoobarbazfoo_bar_baz
Café Crèmesnake_casecaf_crmecafé_crème
camelCaseNamekebab-casecamelcasenamecamel-case-name

Accented letters were deleted, a name that was already camelCase lost its word boundaries, and an apostrophe turned don't into donTStop. To measure the damage, we compared both converters with the widely used lodash library on 20,000 random names built from words with separators, capitals, acronyms and accents. The old camelCase converter differed from lodash on 15,339 of them, the snake_case one on 16,748 and the kebab-case one on 16,671. The new converter matched lodash on every one, in all five styles, which is 100,000 conversions.

Old case converter differed from lodash on 15,339, 16,748 and 16,671 of 20,000 names; the new one matched on all

A table of three styles tested on 20,000 random names. The old camel case converter differed from lodash on 15,339 names, the snake case one on 16,748 and the kebab case one on 16,671. The new converter differed on none.

The new converter matched lodash on every name.

One difference from lodash is on purpose. Lodash puts a digit in a word of its own, so es6 module becomes es_6_module. Our converter keeps digits attached to the word before them and gives es6_module, which is how most people write such names. We left digits out of the comparison for that reason.

When to use each style

  • camelCase for variables, parameters and methods in JavaScript and Java, where the style guides ask for it.
  • PascalCase for class names and types, which most languages write with a capital on every word.
  • snake_case for Python variables and functions and for database columns, where a lowercase name needs no quoting.
  • kebab-case for CSS classes, file names and URL slugs. It cannot be used as a name in most programming languages, because the hyphen reads as a minus sign.
  • CONSTANT_CASE for constants and environment variables, so they stand out from ordinary names.

If you work in more than one language, expect to convert between styles often. The same field may be user_first_name in the database, userFirstName in the JavaScript that reads it and user-first-name in a CSS class.

Tips for converting names

  • Convert a list one line at a time. The Case Converter treats each line as one name, so a column of field names stays a column.
  • Remove accents when you need plain ASCII. Identifiers and file names are often safer without accents. The converter's option turns é into e and ß into ss.
  • Write acronyms as words. userId and parseHttpResponse convert both ways without loss. parseHTTPResponse also works with our converter, but a different tool may read it differently.
  • Rename in bulk with care. After converting names, a find and replace across your files can update them. Test the pattern in the Regex Tester first, and add word boundaries so id does not match inside video.

Frequently asked questions

What is the difference between camelCase and snake_case?

camelCase joins words with no separator and capitalizes each word after the first, as in userFirstName. snake_case writes every word in lowercase and joins them with underscores, as in user_first_name. The first is usual in JavaScript and Java, the second in Python and SQL.

Which case does Python use?

PEP 8 says function and variable names should be lowercase with words separated by underscores. Class names use CapWords, and constants are written in capital letters with underscores. mixedCase is allowed only where it is already the prevailing style.

Should URLs use hyphens or underscores?

Hyphens. Google recommends hyphens instead of underscores to separate words in URLs, because they help users and search engines identify the words. Use kebab-case for slugs, for example summer-clothing and not summer_clothing.

How do I write an acronym in camelCase?

Treat it as a normal word. Google's JavaScript style guide turns XML HTTP request into xmlHttpRequest, not XMLHTTPRequest. That keeps the word boundaries visible and makes conversion to other styles reliable.

Is camelCase or snake_case easier to read?

It depends on the reader. A 2013 study by Binkley and colleagues found that beginners did better with camelCase, while experienced programmers were not affected by the style. Choose the convention of your language or project and apply it everywhere.

Convert your names now

Whichever side of camelCase vs snake_case you choose, paste a name or a list into the Case Converter, pick a style and copy the result. It splits camelCase and acronyms, keeps accented letters unless you ask it not to, and works on each line separately.

Try these free tools

100% in your browser

Keep reading

All articles →