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
Underscores in Numeric Literals Considered Bad #535
Comments
Perl (of course), Eiffel, Nim, Rust, Julia, Java, D, Ada, Verilog, VHDL, etc. I didn't read the rest of your rant. |
I think @cremno put it quite well. Apart from you, no one has expressed objections to this practice for white a while now, so it's safe to assume Ruby devs actually like it. |
two things: (1) I apologize for not being clearer in the beginning; I thought it would be obvious. This is NOT a request to remove the feature from the language, this is a request to remove an arbitrary coding style that isn't even close to well-accepted across multiple languages. Of course I know it's in other languages (added to Java in mid-7, Perl forever, but it's not in Python, Bash, C, or C++, Fortran, Javascript, or many others), but here's the point: NONE OF THEM ENFORCE THE CHOICE. For a similar example here: Gcc doesn't spit out a warning when you don't use the K&R brackets format. (2) "because the devs haven't complained" is a poor excuse for a rejection, particularly because: Here's the problem: developers are notorious for ignoring things like compiler warnings and style warnings produced by things like (in Ruby's case) rubocop. Operations people, on the other hand, are far more concerned about things which produce errors. Since ruby's user base now contains (at a minimum) a large minority of non-developers, trying to enforce a standard on a community that causes problems for a minority (while giving no real advantage to the majority) is unwise. In effect, why include the style requirement if no one actually can show it's meaningfully useful, and I can show where it has a detrimental effect? This kind of style is perfectly fine to enforce in a company's style guide. To enforce it across the community (and, because of rubocop's ubiquity, that's exactly what's happening) is simply a bad idea. DevOps in particular doesn't see the underscore syntax in virtually any code that floats around. Try looking at the Chef and Puppet cookbook and manifest collections: vast quantities of that ruby code don't use the underscore syntax. Once again, the problem isn't the language support; it's that using a style guide to enforce a particular style that is not common (and confusing) to a significant percentage of the user base simply is a terrible idea. |
as an experienced ruby dev, I like being able to read |
@trims2u This is a community style guide. Nobody is forcing you to use it. You're more than welcome to develop your own style guide, based on this one, or whatever else you might want. Or disable the RuboCop setting that generates warnings for this style. And if you have more trouble reading |
Yeah.. And did you even try what happens, if you enter a list of values with normal numbers like YOU WILL FREAK OUT 👿 👿 👿 👿 👿 Really. Try it. |
@h0jeZvgoxFepBQ2C That's got nothing to do with the underscore syntax. If you prefix a number with a So in your example, |
Who in their right mind is writing methods or variables named UPDATE: I wasn't clear enough. I am in full support of underscores to improve readability of numeric values. All I wanted to point out was that if we are worried people are going to mistake said numbers for variable or method names then we have other problems. It is not even possible to set variable or methods in such a format so it should not be confusing for the reader. |
I'm doing this, since its much easier to read how much it is via Also if you want to save currency values as integer cents, f.e. 1,5 € => |
@h0jeZvgoxFepBQ2C I think my comment wasn't clear enough and was misinterpreted. I am in full support of underscores to improve readability of numeric values. All I wanted to point out was that if we are worried people are going to mistake said numbers for variable or method names then we have other problems. It was more of a joke than anything 😉 I don't believe it is possible to set a variable or method name as such anyway. @ShockwaveNN I think you also might have got confused by my comment 😄 |
In my language, the thousand separator is either a space (a thin non-breaking space In many countries, the use of commas or periods is discouraged because it creates problems with interpretation: is 1.000 one thousand or one point zero? Wikipedia lists the following commonly used separators:
Many standards organizations and style guides endorse, recommend, or even mandate the use of spaces, e.g. SI/ISO 31-0 standard, the International Bureau of Weights and Measures and the International Union of Pure and Applied Chemistry (IUPAC), the American Medical Association's widely followed AMA Manual of Style, and the Metrication Board. Now, if only we could come up with an ASCII character that kind-of simulates a space … |
I used to have variables that are like:
I rarely use that style anymore, preferring just _ to dump variables, or using somewhat To the topic of _ in numbers: I like them. I can instantly group larger numbers that way. |
I also use underscores to prefix an unused variable - it's recommended by the standard rubocop config 😄 In my comment I said "methods or variables named 1_000_000_000", which I intended to mean methods or variables with underscores where the only other character in the name is a digit. I'm wasn't talking about numbers values written with underscores. This seems to be have been misread / misinterpreted. I like the idea of using underscores to make number values more readible. To be honest, I'd forgotten it was a thing (until now!) and don't have to store a large static number often enough to find it a problem. Probably the number I have to write statically most often (but still wouldn't say often - more like once per project) is 1000. |
https://github.com/bbatsov/ruby-style-guide#underscores-in-numerics
This is a terrible idea. Not only does it NOT improve readability, it conflicts with the conventions for using underscore for other things in Ruby.
Humans are conditioned to use either commas (',') or periods ('.') for thousands groupings in normal everyday language - one of the two is virtually universal across all human languages. Neither are available to be used in this context in Ruby, as they both have other uses which take precedence.
However, substituting underscore is NOT an acceptable replacement for period/comma. Not only is this particular "style unique to Ruby (and thus, causes all sorts of problems for people who program in multiple languages), but Ruby already has several uses for underscore which conflict with the idea of an easily parseable/readable numerical literal.
Underscore is a valid character for a variable or function name, in several different ways (including several mentioned in this very guide). Allowing their use in a numeric literal actually CONFUSES the issue as to whether it's a literal or function or variable, particularly since we don't quote numeric literals.
The bottom line is that rather than improve readbility, using underscores in numeric literals confuses the reader, as it's not immediately clear whether this is some number, some function, or other special treatment in Ruby. This is particularly true for people who don't spend every single minute writing ruby code. It's also not consistent: should I be using underscores in non-base-10 numeric literals too? If so, how? In base16, where do I put the underscore? After every 4 digits? Every 2?
Remove this recommendation; it actually is counterproductive.
Frankly, as virtually no other programming languages attempt to solve this problem any other way EXCEPT to follow the normal human language of comma or period as the thousandths separator, either just DON'T bother (programmers have made it this far quite well without needing one), or fix Ruby to allow for comma/period in numeric literals.
Underscore is severely non-standard usage, and simply doesn't solve the problem it purports to. A quick Google will reveal the unpopularity of this "style".
The text was updated successfully, but these errors were encountered: