8 ms·
CamelCase vs underscores: Scientific showdown (2011)
- masfoobar 2y agoI tend to respect the programming language I am using - based on the offical recommendation, or what has naturally evolved over time. C, Odin :- my_function("hi") D, Java :- myFunction("hi") C#, Pascal :- MyFunction("hi") Lisp, Scheme := (my-function "hi") Even these are not 100% accurate. For Odin, I would create a struct like MyStruct. In C, it would likely be my_struct_t. etc. In future, I might start following Anti Pascal Case for all languages mYnEWfUNCTION("hi")
- JadeNB 2y agoAnd a follow-up: https://whatheco.de/2013/02/16/camelcase-vs-underscores-revisited/ https://whatheco.de/2013/02/16/camelcase-vs-underscores-revi...
- keybored 2y agohyphen-case is worth the grammar concessions (like making whitespace between binary operators mandatory).
- dailykoder 2y agosPoNgEbOb-CaSe combined with hyphens is the only viable solution
- stock_toaster 2y agokebab-case and SCREAMING-KEBAB-CASE are often found in urls too
- IshKebab 2y agoYeah no thanks. Soo `a-b` and `a - b` are both potentially valid and different? Also allowing hyphens generally leads to issues when interoperating with other languages that don't support hyphens. Probably the best example of this is CSS which does allow hyphens, and Javascript which doesn't. `background-color` in CSS gets translated to `backgroundColor` in JS. It's an annoying paper cut that trips up beginners and makes code less greppable. I generally avoid hyphens wherever possible for that reason.
- ReleaseCandidat 2y ago> Soo `a-b` and `a - b` are both potentially valid and different? You shouldn't be able to start a name with a hyphen, so `a -b` and `a - b` can be both valid. But I mostly just came to acknowledge your fitting username ;)
- keybored 2y ago> Yeah no thanks. Soo `a-b` and `a - b` are both potentially valid and different? Like what the sibling comment said: `-b` should be illegal. And I don’t think this would be a big deal in practice for experienced users (used to these rules). For beginners you could build in an error check: give a dedicated error message if you write `a-b` but you happen to have both variables `a` and `b`. Then the compiler can tell that you probably meant `a - b`. > Also allowing hyphens generally leads to issues when interoperating with other languages that don't support hyphens. Make underscore illegal in the language. It’s not like you need them anymore. In turn you have a bidirectional translator.
- IshKebab 2y ago> `-b` should be illegal Sure... but I didn't use `-b`. Maybe I've misunderstood. > give a dedicated error message if you write `a-b` but you happen to have both variables `a` and `b` I guess, though the idea of having mutually exclusive sets of identifiers sounds like a nightmare. You can have `a` and `a-b`, or `a` and `b`, but not `a`, `b` and `a-b`... Maybe it wouldn't come up much in practice but it's still a pretty big WTF. > In turn you have a bidirectional translator. But the problem is that you have this translation in the first place. It makes the identifier ungreppable. Ideally any place you have an identifier it looks exactly the same through your whole codebase and if you have to translate it then that is no longer true. For example if you want to update all background colours in your project you might search for `background-color`... but miss `backgroundColor`.
- keybored 2y agoThey’re only mutually exclusive as far as getting a good error message is concerned.
- yodon 2y agoUnderscore is a double-pinky keystroke. As someone who has battled RSI, I stopped using snake case and underscore-prefixed member variables because of the added stress all those underscores place on the weakest fingers.
- sirentdiver 2y agoI have a programmable keyboard which I have bound underscore to LeftAlt+N, input with my left thumb + right index fingers. The first key is functionally not a Left Alt key, it is a layer key that shares the same position as where you would find a Left Alt key on keyboards. But you do have a point because not everybody has access to programmable keyboards almost all the time. Maybe snake_case isn't that ergonomic now that I think about it, although I find it easier to read than camelCase. Ultimately, the winner is still kebab-case, which is both aesthetically pleasing and is not a double-pinky keystroke!
- deleted 2y ago[deleted]
- mtlguitarist 2y agoI just realized that I always use my ring finger for underscores. I wonder if that changed how I feel about snake case.
- aramndrt 2y agothat’s interesting to me. For me it’s a left pinky-right ring finger move. We probably have different keyboard sizes, afaik.
- ihumanable 2y agoApparently I type underscore with a chord on my right hand, pinky on right-shift and middle-finger up to underscore. I never noticed before your comment.
- NikkiA 2y agoWhy are you not bending the keyboard's will to your whims ?
- eviks 2y agoIf I read figure 2 correctly (in the follow up study), the difference in find time only appears in 3-word names, while for 2-word names the time is the same
- adolph 2y agoOk, so I’m most definitely going to snakeCase my Hungarian notation from now on. https://en.wikipedia.org/wiki/Hungarian_notation https://en.wikipedia.org/wiki/Hungarian_notation
- endre 2y agonow a study on tabs vs spaces, please
- ubertaco 2y agoI didn't care about tabs vs spaces until I was handed a project to work on accessibility for a particular webapp, and that included firing up a screen reader. That got me to learning about all the ways that markup can (and should) be used to convey _both_ the content _and_ the structure of the information on the screen, for the benefit of vision-impaired readers. That in turn led to the epiphany that the tab character _is_ markup for indentation, and in the world of programming, where indentation is so significant for understanding (especially in whitespace-sensitive languages like Python), I wondered why we were making things harder for vision-impaired users by focusing so much on _visual_ consistency (which can still be achieved by syncing editor "how do I render tabs" settings)
- petepete 2y agoI find snake case so much easier to read, breathing space between words is there for a reason.
- collyw 2y agoAgreed. I started with Java which gave me a bias towards camel case to begin with (as well as braces over whitespace), but have changed to Perl and now Python. Snake case is definitely easier to read, but Python encourages CamelCase for class names, and I see the advantage in mixing for different things.
- montebicyclelo 2y agoIAgreeThatSnakeCaseIsEasierToRead,ThoughIfWeWereToUseCamelCaseMoreWeWouldBeSavingAGoodAmountOfSpace,WhichMightBeUsefulForNarrowBlocksOfText.I'mSuprisedItHasn'tCaughtOnInProse.Actually,LookingAtThis,MaybeIt'sAGoodThingItHasn'tCaughtOnForProse.
- ejolto 2y agoClassical Latin used all caps no spaces, so rather than caught on we moved away from it. https://en.wikipedia.org/wiki/Scriptio_continua https://en.wikipedia.org/wiki/Scriptio_continua
- olejorgenb 2y agoI've read that it such text usually was read out loud. Not sure if doing so actually make it easier to parse, but maybe latin was more phonetically regular than english? whenwordseparationbecamethestandardsystemitwasseenasasimplificationofromanculturebecauseitunderminedthemetricandrhythmicfluencygeneratedthroughscriptiocontinua
- aredox 2y agoFrom the article: "paleographers today identify the extinction of scriptio continua as a critical factor in augmenting the widespread absorption of knowledge in the pre-Modern Era. By saving the reader the taxing process of interpreting pauses and breaks, the inclusion of spaces enables the brain to comprehend written text more rapidly."
- martinclayton 2y agoTitle should mention that the article is from 2011.
- navane 2y agoWhy can't we just have spaces in our variable names? These are both work arounds.
- interstice 2y agoTwo space bars?
- bogdan 2y agotab where tab = 1 space Note: this is not a good idea
- reqo 2y agoIt would make languages like Python quite unreadable as it would be hard to distinguish between variables and keywords!
- eviks 2y agoColors/fonts/styles still exist?
- delichon 2y agoSo that the compiler or interpreter knows the extent of the variable token. Without spaces allowed you would need some other way to tell it where they start and end, like quotes or a special declaration, which would be harder for the brain to parse. If you know a better way please share it.
- rileymat2 2y agoI am not picturing why this is true, if you cannot use key words as parts of variables (alone separated by a space) and lines end with a newline or semicolon (some symbol) The first restriction might make this a problem. I am not saying it is a good idea, but it is not obvious to me.
- 2y ago
- thrance 2y agoWith an azerty keyboard, underscores are a single stroke of the "8" key. This nullifies the "underscores are hard to type" argument. I'd even wager that the performance results are inverted in my country since snake_case does not require any press of the shift key, unlike camelCase. Also, I'm pretty sure I read a study one time that showed snake_case identifiers to be easier to read than camelCase ones. Anyways, I think you should follow the specific guidelines of the language you are currently using.
- jaynetics 2y agoThis does make me wonder how much language preferences are really shaped by lofty philosophical arguments about different paradigms, and how much is in fact unconscious physical resistance: The awkwardness of typing the language on a given country's standard keyboard layout, individual hand physiology, individual visual perception, language verbosity (looking at you, Java)...
- ChrisMarshallNY 2y agoI use both. Depends on the context. In Swift (where I spend most of my time), it's CamelCase, or, quite often, dromedaryCase.
- jrpelkonen 2y agoI’ve never heard of dromedaryCase. It has always been PascalCase vs camelCase in my circles. TIL
- ChrisMarshallNY 2y agoIt's a joke. I made it up. Probably not a good idea to propagate it. ;) I think that there is an official name for lowercase-prefixed CamelCase, but I don't remember it.
- ReleaseCandidat 2y agoThat's camelCase. Uppercase would be PascalCase.
- CRConrad 2y agoAh, makes sense: The hump is in the middle. (Though yes, camels have two; should be dromedaryCase.) But then again, camels have heads at about the same height as the hump, so...
- ChrisMarshallNY 2y agoTIL. Thanks!
- rerdavies 2y agodromedaryCase has one hump. camelCAse has two humps. (I appreciated the joke; and biologists everywhere will thank you).
- j1elo 2y agoI got the solution! Given that spaces in identifiers cannot be used because we need to please the language tokenizers (shouldn't be the other way around with humans vs machines?), let's use TAB as a word separator! Configure TAB width = 1 character, and there you go.
- pavlov 2y agoIn Unicode there's the non-breaking space character. It's even easy to type, at least on Mac: just press Option+Spacebar.
- Glacia 2y agoi'm pretty sure ALGOL allowed spaces in identifiers. Probably some other old programming languages too. For the most part, it's just a tradition at this point.
- ReleaseCandidat 2y agoFortran up to 77 (well, technically still everything in fixed-form, a.k.a. punch-card-style source files) ignores spaces. And AFAIK there still exist both versions of e.g. "goto": "go to" and "goto" are both valid Fortran 90 and later. This, and the fact that variable names are allowed to be implicitly defined, lead to the famous bug: DO 10 I = 1.100 declared the variable `DO10I` with a value of 1.1, instead of the loop from 1 to 100 and declaring the "statement label" 10: DO 10 I = 1,100 SUM = SUM + I 10 CONTINUE
- aragonite 2y agoAlso older versions of FORTRAN, according to Crockford at least :) > It is good to have names containing multiple words, but there is little agreement on how to do that since spaces are not allowed inside of names. There is wun [sic] school that insists on the use of camel case, where the first letter of words are capitalized to indicate the word boundaries. There is another school that insists that _ underbar should be used in place of space to show the word boundaries. There is a third school that just runs all the words together, losing the word boundaries. The schools are unable to agree on the best practice. This argument has been going on for years and years and does not appear to be approaching any kind of consensus. That is because all of the schools are wrong. > The correct answer is to use spaces to separate the words. Programming languages currently do not allow this because compilers in the 1950s had to run in a very small number of kilowords, and spaces in names were considered an unaffordable luxury. FORTRAN actually pulled it off, allowing names to contain spaces, but later languages did not follow that good example ... I am hoping that the next language does the right thing and allows names to contain spaces to improve readability.
- caleblloyd 2y agoThe most peeving to me is camel case conventions that preserves the capitalization of abbreviations. Convention that would require TCPIPQOSScore instead of tcpIpQosScore.
- throwaway482945 2y agoI think you can't really win here with camelCase. Both look bad to me.
- pekim 2y agoI prefer to use a language's convention, whatever that might be. The language's standard library will expose names using the language's convention, and using a different convention for my own code's names would make for strange and confusing looking code.
- pid-1 2y agoWhat if you are creating a new language? Which one would you use?
- ZainRiz 2y agoAvoid casing entirely and use single letter function and variable names Case insensitive, of course
- ReleaseCandidat 2y agoiNVERSEcAMELcASE of course.
- ModernMech 2y agoThat's known as crazy case.
- nijuashi 2y agoor basket case
- thfuran 2y agoCasing is so last century. The future is CnOaNmCiUnRgRENT.
- shrimp_emoji 2y agoUnironically, letter casing was a mistake and it should not exist in any language.
- 2y ago
- derbOac 2y agoI use both but camel case assumes capitalization doesn't have meaning other than as a sort of break indicator, which isn't always the case.
- thenbe 2y agoBehold this monstrosity: devDbUrl Madness. The only thing worse that developers willingly tolerate is prettier's sacrilegious linebreaks [0]. 0: https://prettier.io/playground/#N4Igxg9gdgLgprEAuc0DOMAEBXNcBOamAvJgNoA6UmmwOe+AkgCZKYCMANPQVAIYBbOGwogAggBsAZgEs4mAMJ98QiTJh9RmAL6cqNOrgIs2AJm5H8-ISJABxGf0wAlCGgAWfKFt37aPJlZMAGYLBmthTFEAZXdsAHNMADk+ACNsHz1qf0sTTAAWMN5BSNFnPncBL0wAMXw+Bky-QwY8gFYiqxLbABU3d3kAGQBPbFSEJuyW4yCANk6I22iCeJkIZJkJCCllSYBdAG4qEE4QCAAHGDWoNGRQZXwIAHcABWUEW5Q+CSe+YdvTql6mAANZwGDREqDRxwZA7CR4QHAsEQ858MCOeLIGD4bBwU5wATjZjMODMQZeeLYPjxOA1CAqPgwK5QLFfbAwCAnEDuGACCQAdXc6jgaDRYDgyxu6hkADd1MNkOA0ACQI4GDAXvV4lU4d9ESAAFZoAAe0UxEjgAEVsBB4HqEfiQGjCAQlak0nAJNzzvhHDABTJmDB3Mh8uZnY88AL6uclb7RQRZbDTgBHW3wLUXT4gBoAWigcDJZO5+Dg6ZkZa1NN1SHhBrwAhk2NxTrQFutGdhdf1To0qUDwdDSAjOL4m0xCggAlrIFFbW5Rh6aU+9adsrxjCgpNg0TAfsuYm30Rgw0tDrw2m0QA https://prettier.io/playground/#N4Igxg9gdgLgprEAuc0DOMAEBXNc...
- moogly 2y ago42 columns, eh?
- dvsk 2y agoJFC preach! This shits me to tears, I need to figure out a way to turn those line breaks off.
- ____________g 2y agoThe best solution I've found is to not use Prettier and instead use ESLint Stylistic for formatting: https://eslint.style/guide/why https://eslint.style/guide/why
- cletus 2y agoI'm firmly in the camp that doesn't care what the rule/style is, I just want an unambiguous rule. I'll just use whatever the language conventions are. Snake case in Java, for example, would be a hate crime. So would camel case in C. My general preferences beyond that are: - Opening braces on the same line (I see the article has examples where it's on a new line); - Two space indent, no tabs - No trailing white space. This should be automatically removed so it doesn't generate extraneous changes on commit; - A reasonable line length between 80 and 120 characters, depending on the language. You need to be able to look at 3 files side by side without wrapping. - Don't put the return type on a separate line. This is a really old school (K&R) C style - Don't align function parameters with opening parentheses. Change the function name and you generate a bunch of changed lines for the parameters. - No space before semi-colons eg for (i=1; i<100; i++) not for (i = i ; i < 100 ; i++ )
- speed_spread 2y agoThere are two classes of rules here: naming and formatting. Formatting can be completely automated and thus changed at will, even locally with git hooks. Whitespace can be made truly insignificant with the proper tools. While you could apply the same kind of automated logic to naming, the risk of collision is non zero and moreover would likely break runtime mechanisms like reflection, etc.
- ReleaseCandidat 2y ago> Don't put the return type on a separate line. Generally yes, but stuff like ReturnType<SomeOther<S, TypeConstructor<Nested<S, T, U>, T>>, U, HmmLetsAdd<V, W>> can be on a line of its own.
- cletus 2y agoI generally agree but there are two problems: 1. You're writing C++. You've really lost half the battle laready :) and 2. Writing correct templated code is difficult and should generally be reserved for when you're writing a library; and 3. For complicatred types like this, one should strive to increase readability by using type aliases, assuming your language supports it (eg C++ and Hack do). It's not always possible.
- myfonj 2y agojust•Give•Me•Extra•Kerning•Or•Ligatures I still don't understand how comes that in times of programming fonts with fancy ligatures there is not a single one that would attempt to make camelCase more legible by slightly separating lowerUPPER sequences (presumably by constructing a "anti-ligature" for each unique pair that would have still width of two glyphs). Or try to do anything similar to ease reading theseAnachronisticCharacterSequences.
- ReleaseCandidat 2y agoEmacs has GlassesMode, which displays (but not actually adds) an underline before every uppercase character https://www.emacswiki.org/emacs/GlassesMode https://www.emacswiki.org/emacs/GlassesMode
- NayamAmarshe 2y agoAny kebab-case fans here?
- ReleaseCandidat 2y agoYes.
- ModernMech 2y agoI commented that it's objectively the best case. The problem is most developers aren't willing to sacrifice what it takes to get it.
- LkpPo 2y agoYep.
- ModernMech 2y agoKebab-case is the clear superior case due to requiring fewer keystrokes overall. I think languages should go for kebab case and do something else with infix formulas, as programmers write/read identifiers more than they write/read subtraction in formulas.
- ssousa666 2y agomy favorite thing about camelCase is that class instances are (typically) named and cased as the inverse of their pascal-cased class. val somethingRepository = SomethingRepository() Is much more visually satisfying and balanced to me than: something_repository = SomethingRepository()
- nequo 2y agoI like the latter much more. I can come up with rationalizations, like my eyes having to travel to the beginning of the long identifiers if I want to tell instance and class apart in the first example but not in the second. But honestly it’s most likely because I was socialized on Python. Feeling lucky personally that Rust follows the same convention.
- chrismorgan 2y agoBy sheer coincidence, I was talking about this only a few days ago, and it also jogged my memory so that I reviewed a 2013 rust-dev thread on Java versus .NET conventions for type names, which differ in handling of acronyms: .NET style goes Gc and HttpServer; while Java goes GC, HTTPServer. As for XMLHttpRequest… ugh, that thing’s a menace. My suggestion: > There seems to be a basic assumption of ASCII identifiers. Hey, this ain't the eighties! > Let's have us an XᴍʟHᴛᴛᴘRequest. Absolutely clear with no scope for misunderstanding in either direction: Gᴄ; Rᴄ; Aʀᴄ; SimpleHᴛᴛᴘServer. > Monospace font support is a little poor, but I'm sure they'll fix that up once the desire is demonstrated. > Q and X don't have small-caps variants in Unicode, so acronyms will be banned from having a Q or an X in the middle. (My email wasn’t just trolling; I also added meaningful arguments in both directions to the discussion. But small caps was just too fun a concept to not mention. I also notice that Unicode 11 in 2018 added U+A7AF "ꞯ" LATIN LETTER SMALL CAPITAL Q for some reason (subhead “Letter for Japanese phonemic transcription” and I haven’t looked any deeper), but there’s still no small caps X.) I’m glad to say that Rust stuck with the .NET rather than Java style—it’s easier to reason about, apart from anything else, because of having fairly unambiguous rules, and supports tooling better in a similar way to snake_case, because of unambiguous word separation. I’m also glad that no one ever challenged Rust’s use of snake_case for variables and fields and such.
- dang 2y agoRelated: CamelCase vs. underscores: Scientific showdown - https://news.ycombinator.com/item?id=9138156 https://news.ycombinator.com/item?id=9138156 - March 2015 (57 comments) CamelCase vs underscores: Scientific showdown - https://news.ycombinator.com/item?id=5224531 https://news.ycombinator.com/item?id=5224531 - Feb 2013 (6 comments) Also: CamelCase vs. underscores revisited (2013) - https://news.ycombinator.com/item?id=34525139 https://news.ycombinator.com/item?id=34525139 - Jan 2023 (257 comments)
- tracker1 2y agoUnderscores are harder to type, at least on a US keyboard layout. To me, that's the single largest tag against underscore_separation.
- tredre3 2y agoOn a US keyboard, _ is shift + -. To do CamelCase it's shift + c. Isn't that the same about of "difficulty"?
- Wowfunhappy 2y agoAll I can tell you is I find lots of underscores physically uncomfortable to type, and I have zero trouble with capital letters.
- amadeuspagel 2y agoThis misses one advantage of underscores, which is consistency: foo modified_foo foo modifiedFoo With underscores, "foo" looks the same whether it stands alone or not.
- workethics 2y agoIn that example foo is the important aspect, so it should be the leading element: foo fooModified
- mzan 2y agoIMO, with underscores it is not immediate recognizing the elements of an expression like some_var.some_fun(some_param, another_param) Instead with CamelCase, they are immediately visible: someVar.someFun(someParam, anotherParam) But my preferred syntax is Lisp: (some-fun some-var some-param another-param) (when this-looks-appealing (setf you-like-lisp-syntax true) (vote-poll 'camel-case-formatting)) IMO, "this-looks-appealing" is more readable than "thisLooskAppealing", but "-" is more space friendly respect "_".
- viralpraxis 2y agoI wonder if we could move from sharing original (plaintext) source code to some sort of intermediate representation (mostly like AST). So if you publish the AST anyone can “render” and modify it with his own preferences