Hacker News new | past | comments | ask | show | jobs | submit login

Hyphens in variable names. It's a bold strategy for a scripting language, Cotton.



I think a lot of lisps use hyphens in variable names


There's plenty of precedent. It's just rare for a reason. I'm not sure allowing slightly easier access to CSS syntax justifies it.


I guess it depends a lot on your coding style. When I started allowing hyphens in variable names, I already had about 10,000 LOC of Earl Grey, including the whole compiler for it (seriously, it was a pretty late addition). In all that code, I only had to change one or two lines if I allowed hyphens, so I decided to just go ahead and do it.

And not even because of CSS, just because I think "do-thing" looks better than both "doThing" and "do_thing". Of course, I would think differently if I tended to write "a-b" instead of "a - b", but at the moment I quite enjoy this feature.


I'd love to hear why people prefer underscores over hyphens. I think I grew out of syntax fanboy-ism, but C and python __private__, _special_variable are everything but readable to me. They break the visual line too much. Even historically it was a weird symbol, originally a line break that made it into non-space separator in PL/1, then almost everywhere. Before that it was pure formatting, a typewriter glyph to be overtyped/composed on words to underline them.


> I'd love to hear why people prefer underscores over hyphens.

Because I like subtraction.


It's funny in the end. Parts of languages syntax reinforce other parts. In lisps you cannot write a-b as an arithmetic expression. Sequences of characters are symbols, and that's it. But also, lispers don't really care about infix notation[1] they have abstract polyadic operations: - + / * all take a list of zero or more arguments. (- x y z ...)

[1] And IMO people shouldn't, basic arithmetic isn't that frequent or complex enough to need such a dedicated treatment. But that's another lisp thing, you care more about having DSL opportunities for anything rather than a fixed set.


Just wondering though, how often do people write subtraction as "stuff-thing" instead of "stuff - thing"? I find myself typing the latter almost systemically, so hyphens never get in my way, but of course it's all too easy to be blind to the habits of others.


If you have `stuff - thing` it’s no big deal, but once you start having `(a0-b0)/(a1-b1) + (x0-y0)/(x1-y1)` or whatever, then being able to save all the spaces starts to be kind of nice, especially if your math expressions get to be 60 characters long. There are a few times where I’ve definitely ended up with more readable code by using the presence or absence of a space as a way to group expressions. Also, I generally prefer to write things like `array[n+1]` or `array[n-1]` without the extra spaces.

This code example doesn’t actually have any examples of unspaced subtraction in it, but there are a bunch of other binary operators with space removed. In my opinion adding spaces around all the operators in this file would make the code less readable, especially if trying to follow along from the formal published spec describing the algorithm: https://github.com/jrus/chromatist/blob/master/src/ciecam.co...


I think using spacing for this can be a little misleading since it can obfuscate priority. To give an extreme example, 1e100 * 1e300/1e300 is infinity, because the multiplication is done before the division, but the formatting suggests it's the other way around. It's not too bad in that case but if you were to accidentally group an addition instead the mistake would be harder to spot. I would rather use parentheses all the time to be safe.

Still, you make a good point with the space savings. I guess I just find hyphens nice enough that I don't mind the tradeoff :)


I gave these kind of semantic spaces meaning when I wrote another implementation of AsciiMath[1] because I found it so intuitive. So `1-2 / 3-4 = (1-2) / (3-4)` but `1 - 2/3 - 4 = 1 - (2/3) - 4` like you'd expect.

[1]: http://runarberg.github.io/ascii2mathml/


In long and complicated mathematical expressions I find myself selectively using whitespace to identify logically matching pieces of code, similar to LaTeX' \left( and \right). If I couldn't write (x-y) that wouldn't be possible in the same way.


It's more a question of readability than writeability

Something like:

    result = balance-sheet - ledger-total + discount-total
Is harder to read than

    result = balanceSheet - ledgerTotal + discountTotal


But see, being used to hyphens in variable names, I must tell you I can't figure out which one I find easier to read. On one hand, the presence of hyphens and subtraction is a tad confusing, but on the other, I find "balance-sheet" to be generally nicer to read than "balanceSheet", so it feels like a tie to me. Then, on the other hand, you have things like "xs.for-each(x -> x + 1)" vs "xs.forEach(x -> x + 1)", where I really like the hyphen.

Anyhow, I feel that this is the kind of feature you just stop noticing after a while. I think we often tend to assume that what we are used to is more readable than what we aren't used to, but the brain will adapt to nearly anything. Doesn't mean we ought to go crazy with changes, but hey, I like hyphens.


> I find "balance-sheet" to be generally nicer to read than "balanceSheet"

Me too, and I've wondered if it's because it's activating the same part of my brain that gets aggravated when people decide to use capitals on seemingly random words within the sentence.


To me it's almost cognitively dissonant to see a capitalized letter in the middle of a word. In written language grammars, we have only capitals at the beginnings of new sentences (new ideas), so my brain sees camelcase terms and wants to separate the words into separate terms.

Also, it feels morally wrong that the first word typically doesn't get the capitalization (and often languages ask/require programmers to follow some arbitrary first letter capitalization rules...). Yes, that's right, it's about ethics in programming language design.


in a complex expression, I tend to use white space, and absence of it, to make it clearer, as well as parentheses. So, sometimes x2-x1 happens.

A search on github etc might give some idea of how common it really is - though their search wasn't precise enough last time I tried. Google code was (regex) but I think they closed down? Or you could just clone a few of your favorite projects and grep it.


But you don't like spaces in statements?


Because fanboy-ism could be parsed as fanboy - ism = fanboy minus ism.


You mean like this :

let some-var = a-number-another-number

instead of

let some-var = a-number - another-number


On the other hand, pinky fingers everywhere are collectively breathing sighs of relief from not having to push down the shift key nearly as often.


Don't pinky fingers press the hyphen key?


My ring finger does. Not sure which is more common.


Well, in standard touch typing, the pinky handles the hyphen.


It does? I never learned it that way. Using the ring finger is much more ergonomic for me than using the pinky finger.


Out of curiosity, what's that reason?


Because a hyphen is also the subtraction operator. It's extremely weird if a+b is valid syntax but a-b is not.

This language may require whitespace around all operators, though. It's not clear to me.


Ah that makes total sense, thanks!


Yes, because Lisp uses prefix notation and hence there is no ambiguity: a-b cannot be interpreted as subtraction in lisp but in EG ...?


Lisps do not have operators. This language does.


Imagine being able to type style.border-width in JS instead of style.borderWidth



  style['border-width']


inner = border - width




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: