loading...

snake_case, camelCase, or dash-case?

Ryan Westlund on February 19, 2020

So we've got snake_case and camelCase when it comes to code, and when it comes to filenames there's a third option we don't talk about as much beca... [Read Full]
markdown guide
 

Eye-tracking study finds that snake_case is more readable than camelCase.

I like how Ruby uses snake case for typical variables and methods and UpperCamelCase for objects and UPPER_SNAKE_CASE for constants. The convention of using the different cases to signify different things makes for a lot of good legible code IMO.

 

I agree with all of this, but also feel kebob case is just easy to read as snake case. I tend to prefer it for file names.

 
Sloan, the sloth mascot Comment marked as low quality/non-constructive by the community View code of conduct

also feel

It's a good thing feelings don't trump observations found in scientific studies. Also, not everyone is the same, it turns out.

 
 

I'm just waiting for a language to come around that codifies these conventions as errors so the style arguments die.

 

I don’t have a dog in the fight, except this.

I always try to program in the conventions of the language I am using.

Dashes for CSS classes. CamelCase for JS, except title case for classes. Python is underscores. Ruby is just a mixed bag. Dashes for html attributes.

To me that makes readable code. And I think the conventions are better for the community. Historically when these things change, it’s something that gets adopted and accepted. In recent years for JavaScript this seems to be AirBnB or Prettier.

 

Don't forget SCREAMING_SNAKE_CASE.

Okay, it's terrible for anything but naming constants. I just love the name.

screaming snake

 

Forgot to mention: one thing that really bugs me about JS/JSON is that the JS convention is lowerCamelCase, but the JSON convention is snake_case. Having these two be different really throws me off a lot, given how interrelated they are.

 

Can I ask where you’ve seen snake case as the standard for JSON? I very rarely see it, and I’ve definitely never seen an official recommendation for it.

 

Google APIs (place search/geocode stuff), the API of the web service I work on at my job (Awning Tracker), and... huh. Now that you mention it, I can't think of any other examples. I guess I haven't seen as many different JSON APIs as I'd started to think (I shouldn't count my own website lol). I just found out that apparently the standard is supposed to be camel. TIL.

Interesting. I wonder why Google chose to do that. Maybe it makes it more consistent since they do a lot of their APIs for multiple languages, and they picked a snake language as their favorite.

 

Clojure / Lisp also seems to use dash-case (i.e. kebab-case).

I think Python's snake_case is an outlier, despite being slightly more readable than camelCase. So, I'll stick with camelCase. camelCase is more standard.

kebab-case is very similar in readability and parsing to snake_case, I think, but it is much less supported in programming.

However, I tend to use kebab-case in TypeScript filenames (in Python filenames, I use snake_case).

UPPER_SNAKE_NAME is also very popular for constants.

 

I think Python's snake_case is an outlier

I mean, it works for rust-lang. I like kebab-case as well, coming from a .NET/PowerShell background.

 

In my case, I use camelCase all the time.
My API dev is using snake_case, so most of the time I just harass her for using it (we still friends). And when it comes to do some code fixes in her APIs, I refactor it to camelCase.

I can't think there are pros or cons to this specific subject. These two options are the only possible ways to code in, its just a matter of preferences.

 

Lol, that sounds like a big source of time waste :P you two should really agree on one.

 

Yeah we’re still working on it 😂

 
Sloan, the sloth mascot Comment marked as low quality/non-constructive by the community View code of conduct

I refactor it to camelCase

So during a refactor you introduce API breaking changes everywhere? You sound awful to work with.

 

Thanks!! I know right? Thanks God you’re not working with me ha? Mind your business son.

You posted your story in a public forum. Responses to your passive-aggressive co-worker relationship should be expected.

You replied to a public forum post, You expected to hear something you don't like. This is the life.

 

IMO, it depends.

  1. If I want to make a file name, I will try to follow the URL naming convention, which will be kebab-case in common.
  2. If I want to make a function name in JavaScript, I will try to follow the JavaScript naming convention, which will be camelCase in common.
  3. If I want to make a function name in PHP, I will try to follow the PHP static function naming convention, which will be snake_case in common.
  4. If I want to make an ID or class name in CSS, I will try to follow the CSS naming convention, which will be kebab-case in common. It also match to the HTML naming convention.
 

It varies strongly from language to language.

Java has a de facto styleguide that says variables should be camelCased and classes should be CamelCaps (and other "rules" and conventions - POJO conventions comes to mind).

Python has PEP8 that also has their saying: classes should be CamelCaps and variables, snake_case (both, preferably - exceptions apply).

PEP8 is a interesting reading.

@yujiri8 mentioned JSON has a standard and, AFAIK, there isn't an official one. The JSON.org docs says that properties names should contain only 'a'..'z', 'A'..'Z' and '_'. There's also no indication on ECMA of a standard.

Kebab case is, IMHO, confusing as it would be mistaken for a minus sign (and probably incompatible in some languages).

My take here is: follow the language convention. It's easier on the eyes of people that has more time/experience on the language and less work for the linter (if you use one - PS: you should).

 

In some Python libraries, like PyQt, uses CamelCase (PascalCase).

 

I'm late to this party, but I've never cared as long as it's consistent. I like languages with either built-in code formatters or at least widely accepted styles, so that I know what I'm in for when I open someone else's code. One less thing to think about.

 

Personally I like snake_case, but sadly I think personal preference is the least important POV:

1º what the existing codebase use
2º what the PL "suggest" (fight with automated formaters is wasted effort)
3º finally what I like :)

feel free to hate the former programmers if 1º is different than 2º.

 

One thing that's also a consideration here is that with kebab case, in your text editor you lose the option to select the whole variable name as well as chunks of it, like you can with snake case and camel case. For this reason I use snake case in my CSS classnames and IDs.

 

Personally I like CamelCase. In the language I work in, however, dash-case* was quite common to use. The main disadvantage is that a dash-cased word cannot be selected by double clicking it. If you do it, only one side of the dash is selected.

  • never knew the real name was kebab-case
 

dash-lower-case for me :) no stupid case switching ever

 

Problem with dash-case is that double clicking will not select the entire name.

 

Ooh, that's something that never even occurred to me. I don't really use double-clicking to select text, but it's an interesting point. A lot o environments can customize what counts as a word divider (eg. xfce4-terminal), but I guess that might mess up other things.

 

That doesn't work in most languages, though, does it? What do you do in languages that don't allow - in names?

 

Did you know that dash-case is also called kebab-case?

 
 

I’m a fan of kebob case, especially with file names.

 

How about nut case? It's when you mix as many styles as possible and let chaos flow through the codebase

 

I use all of them, depending the language I am using.
i.e
in C++ and Rust I use snake case
in C#/js variable names I use camelCase
in Angular filenames I use dash-case

everything has its place

 

I vote for SaRCasM_cASe, it's the best of all worlds.

code of conduct - report abuse