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
Comments
What about just using commas? |
ambiguity.
|
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...
... I don't think this is particularly necessary. Closing as a |
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 ( |
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. |
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. |
Okay ... re-opening the ticket -- have at it. |
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. |
I wouldn't use this feature, but it wouldn't bother me to work with someone else's source who had used it. |
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. |
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. |
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 ( |
I guess it doesn't hurt as long as we can have a reasonable implementation. |
The question isn't really "does it hurt?" The question is: "would you use it?" |
Well yeah, I might use it for fun. It'd allow more smileys in code. =p
|
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. |
Since the support for this is still lukewarm at best, closing as a |
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). |
railsjedi raises a good point. Sure, you can write
but it feels a bit clumsy. (There's also 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 |
Update: I made a pull request at issue 913; Jeremy declined. |
@jashkenas had your opinion changed since the last year? We now have |
Nope -- my opinion about underscores in numbers remains the same. |
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. |
This feature is veeery lacking.. 😢 |
Shame - I love this feature in perl |
A consideration that I didn't see mentioned so far: it's not just for zero filling. So whereas |
|
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 |
Related: babel/babylon#538 |
Most JS platforms should support numeric separators in the future, so they can be used in CoffeeScript by momentarily escaping to JS:
If @babel/plugin-proposal-numeric-separator is used, I think the above CoffeeScript will work on all platforms today. |
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:
Which is equivalent to:
Any takers?
The text was updated successfully, but these errors were encountered: