Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow place-separating underscores for number literals a la Ruby #632

Closed
jameshfisher opened this issue Aug 18, 2010 · 30 comments
Closed

Allow place-separating underscores for number literals a la Ruby #632

jameshfisher opened this issue Aug 18, 2010 · 30 comments

Comments

@jameshfisher
Copy link

This is a simple one. Ruby allows the underscore to be used in numeric literals to increase readability of large numbers. The underscore is simply ignored when the number is parsed.

I propose the same thing in CS. It's simply implementable and has no issues with ambiguity AFAICS. So we will be able to write:

while a < 1_000_000_000_000
  do_this_one_trillion_times()

Which is equivalent to:

while a < 1000000000000
  do_this_one_trillion_times()

Any takers?

@devongovett
Copy link

What about just using commas?

@jameshfisher
Copy link
Author

ambiguity.

function_with_two_arguments(1,000,000, this_is_actually_the_fourth_argument)

@jashkenas
Copy link
Owner

It may not be ambiguous to parse, but I don't think it's particularly readable. It's a neat trick so that you don't have to count zeroes, but in a language that supports native exponential notation...

1e12

... I don't think this is particularly necessary. Closing as a wontfix.

@michaelficarra
Copy link
Collaborator

This is a slightly old issue, but I really think this is a pretty useful little language feature. It was dismissed because of native exponentiation (5000 === 5e3), but underscores can separate digits of numbers other than those that end in a bunch of zeros, for example 12_345_678_910. This shouldn't be hard to implement and can't possibly hurt the language. I'd even be willing to implement it myself next week if someone hasn't already done it by then.

@jashkenas
Copy link
Owner

It hurts the language in that it expands the syntax in a direction away from JavaScript, and in one that I'd expect will be little used. If 9 out of 10 CoffeeScript programmers prefer to leave the underscores out of long numbers, then it's more of an obfuscation than an improvement to readability -- and this is the reason I feel the same way about it in Ruby: very few people actually use it.

But, all that said, if you feel strongly about it, we can reopen the ticket and take votes, and see if folks think they would use it or ignore it.

@michaelficarra
Copy link
Collaborator

I don't exactly think it's one of those must-have language features, but I don't feel it got the attention it deserved when first opened. A few days for a vote would make me feel better about it. That being said, my vote is in favor... for now.

@jashkenas
Copy link
Owner

Okay ... re-opening the ticket -- have at it.

@hen-x
Copy link

hen-x commented Oct 20, 2010

Anyone got a link to Ruby code that is made significantly more readable by numeric underscores? I think that would help to put this ticket in context.

@naturalethic
Copy link

I wouldn't use this feature, but it wouldn't bother me to work with someone else's source who had used it.

@bdimcheff
Copy link

The only really good use case that I've had for using underscored numbers like this in ruby is when I was testing an app that stored currency as an integer number of cents. It's much easier to read 100_00 than 10000 as $100.00, especially when you're scanning through quickly.

@tristanz
Copy link

I'm weakly -1 for this, particularly if it is overloaded to mean thousands and hundredths (cents). I see very little gain and it adds one more difference between coffeescript and javascript that needs to be documented/explained.

@michaelficarra
Copy link
Collaborator

For clarity, underscores would be completely ignored. They can be inserted anywhere in a number to mean anything the developer wants. For example, it can separate bytes in a hex number (0xFFFF_FFFF), thousands (12_345_678), dollars and cents (1_234_56), or anything you want (12_34_56_78, 123_4_567,0_400 is 0x100). They're just used to make numbers more readable and possibly hint at their purpose.

@satyr
Copy link
Collaborator

satyr commented Oct 21, 2010

+4_2e-0_2

I guess it doesn't hurt as long as we can have a reasonable implementation.

@jashkenas
Copy link
Owner

The question isn't really "does it hurt?" The question is: "would you use it?"

@satyr
Copy link
Collaborator

satyr commented Oct 21, 2010

The question is: "would you use it?"

Well yeah, I might use it for fun. It'd allow more smileys in code. =p

oh = 0_0
eh = 3_3

@TrevorBurnham
Copy link
Collaborator

Yeah, I don't see any need for this. I've never seen it used in Ruby, and I don't think I've ever seen a number in JavaScript that's 1) large enough to benefit from the syntax and 2) not something like an API key that's just copy-pasted in anyway.

@jashkenas
Copy link
Owner

Since the support for this is still lukewarm at best, closing as a wontfix -- sorry, Michael.

@jacquescrocker
Copy link

Might have missed the boat on this one, but I use this feature all the time :-)

It's especially nice in Javascript since a lot of time things are dealt with in milliseconds. 30_000 looks so much nicer than 30000 (to represent 30 seconds).

@TrevorBurnham
Copy link
Collaborator

railsjedi raises a good point. Sure, you can write

K = 1000
delay = 30*K

but it feels a bit clumsy. (There's also 30E3, but for some reason scientific notation doesn't seem to have caught on among JavaScripters.)

Consider me converted. This is possibly the most self-documenting feature ever, so why not? Here's a patch, with test cases.

[Note: While I'm fairly confident that my patch covers all the obvious cases—e.g. things that start with _ aren't assumed to be numbers—it differs from Ruby's implementation in one respect: The Ruby compiler will throw a syntax error if there's more than one _ in a row, whereas the patch compiles 1_____0 to 10 without complaint. I don't see this as a big deal, but it's worth mentioning.]

@TrevorBurnham
Copy link
Collaborator

Update: I made a pull request at issue 913; Jeremy declined.

@paulmillr
Copy link

@jashkenas had your opinion changed since the last year? We now have 0b111101110 which is harder to read than 0b111_101_110.

@jashkenas
Copy link
Owner

Nope -- my opinion about underscores in numbers remains the same.

@showell
Copy link

showell commented Mar 13, 2012

I would definitely use this feature. I'm not dyslexic or arithmetically challenged, but I have a hard time visually parsing large numbers, for whatever reason, especially when there are consecutive zeros.

The underscore seems fine to me. I agree with @michaelficarra that exponentiation doesn't solve all use cases, and I also like the idea that the underscore is just completely ignored.

@milushov
Copy link

This feature is veeery lacking.. 😢

@antonpiatek
Copy link

Shame - I love this feature in perl

@henrik
Copy link

henrik commented Sep 1, 2015

A consideration that I didn't see mentioned so far: it's not just for zero filling.

So whereas 1e9 might be a good alternative to 1_000_000_000, I don't think anyone would argue that 1.234567e9 is as readable as 1_234_567_000.

@lydell
Copy link
Collaborator

lydell commented Sep 1, 2015

1e9 + 234e6 + 567e3. Nah, just kidding :)

@tristil
Copy link

tristil commented May 26, 2017

Not expecting you to change this but wanted to throw this out as a counterargument to consider: you said in 2010 that in Ruby very few people actually use this feature. But these days it's part of the Rubocop style guide so I would guess that it gets used quite frequently now: http://www.rubydoc.info/github/bbatsov/rubocop/Rubocop/Cop/Style/NumericLiterals Just a FWIW

@lydell
Copy link
Collaborator

lydell commented May 26, 2017

Related: babel/babylon#538

@Leftium
Copy link

Leftium commented May 17, 2019

Most JS platforms should support numeric separators in the future, so they can be used in CoffeeScript by momentarily escaping to JS:

v = `1_019_436_871.42`

If @babel/plugin-proposal-numeric-separator is used, I think the above CoffeeScript will work on all platforms today.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests