from Hacker News

CamelCase vs. underscores revisited (2013)

by aloukissas on 1/25/23, 10:38 PM with 257 comments

  • by layer8 on 1/26/23, 3:48 AM

    I'm slightly conflicted about CamelCase. On the one hand it can be somewhat harder to read, on the other hand it does have a number of benefits:

    - Typing "ABC" Ctrl+Space gives me code completion for AsynchronousByteChannel (and the like), whereas "abc" Ctrl+Space gives me code completion only for what strictly starts with "abc". I.e. uppercase means "words starting with that letter". Of course you could have something equivalent for non-CamelCase naming conventions, but for CamelCase it is a very natural fit.

    - It provides the two variants lowerCamelCase and UpperCamelCase (often used for variable names vs. type names), whereas lower-kebab-case & Upper-Kebab-Case or lower_snake_case & Upper_Snake_Case seem more awkward in comparison (more keystrokes for the "upper" variant).

    - When lowerCamelCase is used for function names, the case distinction often maps nicely to verb + noun (`createFooBar()`, `validateBazQuz()`).

    - You still have underscore available to occasionally "index" a name (`transmogrifyFooBar()`, `transmogrifyFooBar_unchecked()`) unambiguously.

    - The fact that CamelCase does not match normal English spelling has the advantage that it can't clash with it. CamelCase can stand both for hyphenated-compound-words as well as for open compound words (or just a phrase), whereas other naming conventions may look like the one although the words really stand for the other.

    - Minor advantage: You can fit slightly more words into a line.

    Apart from that, I'm pro-kebab-case.

  • by int_19h on 1/26/23, 12:24 AM

    While I find snake_case more readable when looking at the identifier alone, it has the downside that _ is mostly whitespace with the glyph at the bottom of the character cell, and so it visually groups more with punctuation like "." than with letters. Which wouldn't be so bad if "." wasn't so common in property / method call chains, so you end up with stuff like:

       foo.bar_baz.blah()
    
    being difficult to parse when quickly skimming through code. OTOH kebab-case solves this nicely because "-" is not in the same place as "." in the character cell, and being in the middle, groups more naturally with letters:

       foo.bar-baz.blah()
    
    Then again, maybe the established formatting conventions for member access are just suboptimal? Suppose that we put a space before every "."; then:

       foo .bar_baz .blah()
  • by inDigiNeous on 1/25/23, 11:11 PM

    Good to see some scientific studies on the easier_readability_of_snake_case, versusComparedToCamelCaseBouncingUpAndDown.

    I tried to convince my co-workers toTransitionFromCamelCase to the world_of_easy_reading_snake_case, but alas, the codebase alreadyUsingCamelCase won.

    Maybe it's the idea that "shorter == better", or whatever, but if I could choose, I would use snake_case_everywhere man.

  • by hypertele-Xii on 1/26/23, 12:35 PM

    An underscore is a word-separating space that selects as part of the sentence.

    If you use spaces in your normal writing, there's no reason you shouldn't use spaces in programming - except that a space prevents you from easily selecting the whole phrase, a very common operation programmers face.

    So they replaced the space with a character that is effectively identical, but works better.

    Some people figured they could just remove it. WellTellMeThis,DoesItMakeMyTextMoreReadableToYou? If you argue that camelcase is better, then shouldn't we adopt it into normal writing too? Why not?

  • by tlb on 1/26/23, 9:57 AM

    I hacked my editor to make _ look like a small hyphen, and - look like an extra-large em-dash. Then I can work with snake_case underneath, with the pleasing aesthetic of kebab-case. And (for the robot control stuff I work on), negation is an extremely significant operator so I like having it stand out.

    Screenshots at https://visibot.com/post/kebab-case

    This is for a custom language & IDE, but someday I'll get around to making VSCode do the same.

  • by jjice on 1/25/23, 11:35 PM

    Whatever the standard is for the language. Unless we're in a language without a standard (cough cough PHP cough cough). I work in a real snakecase mixed with camelcase PHP codebase at seemingly random intervals. Worst part is that this codebase started only a few years ago.

    Snake is my preferred. The Python/Rust use of snake case and Pascal case for their respective purposes is my favorite.

  • by asiachick on 1/26/23, 3:41 AM

    I'm more of a camelCase person than a snake_case, probably because I've been writing lots of JavaScript for the last 10 years. But, .... You could argue that camelCase is culturally insensitive given that plenty of languages (Japanese, Chinese, Korean, Hindi, Sanskrit, etc) have no concept of case.

    Many modern computer languages (JavaScript, Rust, Swift, ...) allow non-ascii identifiers so if you pick camelCase, then someone writing Japanese, Chinese, Korean has no way to obey. That doesn't mean snake_case would be all that better in those languages though.

        var 画面_幅 = ...;
        var 窓_縦 = ...;
    
    The point is, both camelCase and snake_case are a thing based around western languages.
  • by wodenokoto on 1/26/23, 4:22 AM

    A non-programming colleague asked me why I use so many underscores “wouldn’t it be easier to type a dash if you can’t use a space?”

    Why yes. Kebab case would be so much better.

    Or dots, like R uses.

    Neither of those are compatible with most languages, but they are a better options for current keyboards.

  • by CoolGuySteve on 1/25/23, 11:01 PM

    Everywhere I work seems to settle on CamelCase but reading/writing it is annoying because: sSkKcClIwWxXzZvVoO0

    Goldman Sachs' Slang language allowed spaces in tokens which was actually much better but I guess your grammar has to support that from the ground up.

  • by pavlov on 1/26/23, 10:18 AM

    Somehow I can't believe that it's 2023 and "word breaks within identifiers" remains such a fundamental issue in programming. Why are we stuck treating programs as structureless character sequences? We don't need to enter them on punch cards or Teletypes anymore.

    It's like if file systems were forever stuck on FAT and everybody was quietly resigned to the idea that you can simply never, ever have more than 8+3 characters in a file name. "That's just how computers work. Anyway, I'll share you the latest budget spreadsheet on Google Docs, it's called BDG2023A.GDC"

  • by userbinator on 1/26/23, 3:17 AM

    Why not both and neither? IMHO this is one of those things where people seem to congregate and advocate for one side religiously, but I don't see much value in that. Why can't the style simply imply how much scope and importance an identifier has? I've never liked naming dogma, but this is what I find to be natural:

        trivialidentifier - local variables inside a function, usually < 3 words/abbrs
        slightly_important_identifier - function names with limited scope
        ImportantIdentifier - widely-used functions, class/struct names
        More_Important_Identifier - classes/structs that are quite important
        VERY_IMPORTANT_IDENTIFIER - global constants and (rarely) classes/structs
  • by pleb_nz on 1/26/23, 7:38 AM

    A little confused. People seem to be mixing PascalCase and camelCase in the article and conversation.

    Assuming as they're similar they're being used interchangeably in this context?

  • by yakubin on 1/25/23, 10:59 PM

    Do you have a moment to talk about our lord and saviour kebab-case?
  • by r2b2 on 1/26/23, 7:08 AM

        TypeName, ClassName
    
        functionName, methodName
    
        variable-name, symbol-name   # if possible
    
        variable_name, symbol_name
  • by ldh0011 on 1/26/23, 1:55 PM

    I'm just mildly annoyed that most languages disallow kebab-case I assume almost entirely so '-' can be used for infix subtraction (and maybe decrement) without having to surround it with spaces... not a good tradeoff imo.
  • by dools on 1/26/23, 11:19 AM

    It’s obvious that the only value in using different cases is so you can differentiate between different types of thing:

    1) functionsLikeThis

    2) variables_like_this

    3) ClassesLikeThis

  • by danbruc on 1/26/23, 8:24 AM

    If found a paper years ago that was also looking into the readability of different style choices - how easy is it to miss a leading underscore in _field because it is almost a space or to confuse foo_bar and foo bar and other similar stuff mostly related to casing and spacing. I have been trying to find this paper again from time to time for years now without success, is by chance someone aware of it? One thing I remember in more detail is that it was looking at the bounding box shape and how similar they are, it had figures with different styles and bounding boxes drawn around characters and words.
  • by frereubu on 1/25/23, 11:17 PM

    The title needs (2013) at the end, particularly as the results might be different now.
  • by Zigurd on 1/26/23, 12:15 AM

    This feels like an issue studied through a keyhole: You can do a study that makes underscores seem advantageous. And in a vacuum, a global with an uppercase name otherwise identical to a local with a lowercase name looks like a debacle waiting to happen. But, with a modern IDE, some combination of color, bold, and italics enables coders to easily distinguish among these seemingly inadvisable symbolic names. And if you want to find all uses of a name, that's easy.
  • by pyrolistical on 1/25/23, 11:56 PM

    I wish a language was designed to allow identifier with space in them without quotes. That is the most readable. But I don’t know the consequences to the rest of the grammar
  • by claytongulick on 1/26/23, 4:20 AM

    My style comes from a weird combined history of all the languages I've worked with over the years.

    I do lowercase snake for_variable_names, camel case forFunctions() and title case ClassNames.

    In markup, CSS class names are kebob-case but IDs follow snake_case rules (this is so that they can be referenced easily and are valid identifiers in js).

    I like this approach because I can tell at a glance by the naming convention what kind of identifier I'm dealing with.

    I dislike the common js style of everythingIsCamelCase, I find it more difficult to read - and in a language where functions are a primary type, I think it's good practice to differentiate stylistically a variable from a function within the closure or prototype chain.

  • by Mikhail_Edoshin on 1/26/23, 6:32 AM

    I used to like snake case and variants, but now I like something more elaborate: camel case with semantic underscores:

        Lx_DoThis -- function
        Lx_DoThis_Gen -- generic impl.
        Lx_DoThis_Gcc -- GCC-specific
        LxMyType -- type (class)
        LxMyType_DoThat -- method
    
    That is if I use underscore to separate words, I get names that appear to be composed of an arbitrary number of parts. I want that apparent composition to be meaningful.

    In a more sophisticated language where types, methods, and variants have native support, this reduces to essentially camel case. Which still looks better to me because a single thing appears as as single word, not a random number of words.

  • by HarHarVeryFunny on 1/26/23, 2:02 PM

    For decades(!) I used CamelCase identifiers, with a convention of using an upper case first letter for constants/types/functions and a lower case first letter for variables.

    e.g. MyType myType;

    I'm not quite sure what made me switch, but nowadays I used lower case and underscores for all identifiers other than constants, and use a "_t" suffix to distinguish types.

    e.g. color_t color; string_list_t list;

    I think it's certainly easier on the eyes and more readable.

    At the end of the day though it's personal preference, unless you're working on an existing code base where you should adopt the naming and formatting conventions of the code base.

  • by cpeterso on 1/26/23, 5:29 AM

    I like the symmetry and readability of “Ada case”: snake_case but with uppercase letters where appropriate, such as acronyms. So instead of XMLHttpRequest or xml_http_request, you would use XML_HTTP_Request.
  • by MetaWhirledPeas on 1/25/23, 10:57 PM

    Interesting results! Underscores do have two built-in caveats that offset the recognition benefit somewhat: more characters to type, and an awkward key to hit (shift + hyphen).
  • by robomartin on 1/26/23, 6:19 AM

    Sometimes I feel these discussions can derail into the realm of being pointless.

    Computers do not care. Seriously. I don’t know about others, I have far more important and urgent things to worry about in the course of completing a project than this_case or thatCase. I prefer this_case, probably out of habit. Yet, it isn’t important. I’ll use anyCase if required. My bank account also could not care less.

  • by cassepipe on 1/26/23, 9:48 AM

    I hate camelCase and Pascal Case.

    You want arguments? Even though they're post rationalizations of what I like best?

    OK, well first I hate to type caps, then I hate CAPS, so more than one in a word is unbearable. ThenIThinkItsHardToRead. Finally I like that we have a visible symbol to mean space that's still visible. Who is going to use it if we programmers don't?

  • by jacobsenscott on 1/25/23, 11:55 PM

    kebab-case is best - no awkward shift but more readable than camelcase.
  • by Pxtl on 1/26/23, 3:47 AM

    Let's just make a language where space isn't used to delimit between tokens and let us have spaces in our names.
  • by benreesman on 1/26/23, 10:33 AM

    I would happily trade using some other lexeme for subtraction and negation if it bought me hyphenated identifiers. Code has a fair amount of subtraction but it’s like 2-3 orders of magnitude off from identifier word breaks I’d wager: I take that deal, camel and snake case are both godawful.
  • by psychoslave on 1/26/23, 4:37 PM

    Everybody seems to ignore the median case: `some·name` is a valid identifier in surprisingly large set of mainstream languages actually. At least it works just fine with C, C++, Javascript, PHP, Python and Ruby.

    However it doesn’t in any Shell I tried (bash, fish, zsh) nor C#, Go or Java.

  • by innocentoldguy on 1/25/23, 11:04 PM

    I don't see as well as I used to and snake_case is much easier for me to read when scanning through code than camelCase.

    This may be why I tend to gravitate towards languages like Elixir and Ruby, who prefer snake_case, and away from languages like Go, which use camelCase.

  • by bitwize on 1/26/23, 1:21 AM

    (define kebab-case-for-the-win #t)
  • by inopinatus on 1/25/23, 11:43 PM

    Always liked the Ruby convention of PascalCase for constants and under_score for lexicals.
  • by cb321 on 1/26/23, 11:14 AM

    This relates to a hotly contended topic in style-insensitive Nim https://github.com/nim-lang/RFCs/issues/456
  • by teddyh on 1/26/23, 10:58 AM

    Tht GNU project prescribes snake_case:

    https://www.gnu.org/prep/standards/html_node/Names.html

  • by emodendroket on 1/26/23, 7:00 AM

    Like brace style or a million other nitpicks, I don't care. I'll use whatever the linter says, so set one up. (though I will say I also hate code styles that don't adhere to the general language convention)
  • by dec0dedab0de on 1/25/23, 11:36 PM

    I prefer snakecase, but I just use whatever the standard is for the language I'm using, or the project if it already exists. Staying consistent is most important for these kinds of things.
  • by osigurdson on 1/26/23, 1:55 PM

    I just do what is common in the language. Python is interesting as snake case is supposed to be used for the most part but a lot of code seems to use camel case.
  • by every on 1/26/23, 6:02 AM

    CamelCase seems to be preferred by screen readers for the visually impaired. Just recently encountered this on Mastodon with their extensive usage of hashtags...
  • by kovac on 1/26/23, 7:32 AM

    Isn't camelCase CamelCase and CamelCase PascalCase.

    On a more serious note, I feel like a reasonable middle ground is to agree to use one-word identifiers only.

  • by rpaddock on 1/26/23, 7:16 PM

    "A well placed underscore can make the difference between a s_exchange and a sex_change." - Intel 8048 User Manual cria 1978
  • by jws on 1/26/23, 12:11 AM

    This has remained unsettled for 60 years. Perhaps we need a new choice?

    How about Unicode "Thin Space" 0x2009 for a legal identifier character? (HN isn't letting me put the Thin Space in there.)

    How about Unicode "Middle Dot" 0x00B7 for a legal identifier·character?

    Best if you aren't in a fixed with editor for those.

    I used middle dot in an experimental language which was Unicode heavy so already didn't like fixed width fonts. It parses trivially and reads well.

    I haven't tried the thin space in earnest, but the example code I typed up looked reasonable.

  • by MichaelMoser123 on 1/26/23, 3:29 AM

    i think you need to do what the built-in functions/standard library of language X is doing; if it is camel case then do camel case in your own code - having your own code differ from the convention of the built-in functions/standard library is very confusing.
  • by metadat on 1/25/23, 11:41 PM

    snake_case strains my fingers way more with the excessive reaching to tap shift-underscore.

    Thank goodness for PyCharm auto-complete. Typing every variable name fully manually in snake_case 24/7 everyday was begging for me to develop RSI.

  • by 0x073 on 1/25/23, 11:18 PM

    I prefer camelCase for class properties and under_score for local variables.
  • by skerit on 1/26/23, 8:43 AM

    snake_case for variables and properties, camelCase for methods and classes. That's the way I do all my projects, no matter what the "language standard" is.
  • by teddyh on 1/26/23, 12:10 AM

    Best of both worlds: Emacs with M-x glasses-mode
  • by transfire on 1/26/23, 8:29 AM

    Oh, if only middot had been in the ASCII.
  • by thrown1212 on 1/26/23, 1:21 PM

    If you find yourself using reallyLongKeywordChainsForIdentifiers then you're probably coding in Java and now you have two problems.
  • by kybernetyk on 1/26/23, 5:32 AM

    under_scores look more "l33t" while CamelCase is more readable.