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

Underscores in Numeric Literals Considered Bad #535

Closed
trims2u opened this issue Jan 27, 2016 · 10 comments

Comments

@trims2u
Copy link

commented Jan 27, 2016

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".

@cremno

This comment has been minimized.

Copy link

commented Jan 27, 2016

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)

Perl (of course), Eiffel, Nim, Rust, Julia, Java, D, Ada, Verilog, VHDL, etc.

I didn't read the rest of your rant.

@bbatsov

This comment has been minimized.

Copy link
Collaborator

commented Jan 27, 2016

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.

@bbatsov bbatsov closed this Jan 27, 2016
@trims2u

This comment has been minimized.

Copy link
Author

commented Jan 27, 2016

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:
(a) Ruby's userbase is now heavily non-developers, particularly DevOps folks
(b) developers tend to spend all their time in a single languages, and thus, become blind to it's quirks and warts, which can be glaringly obvious to people who are either casual users, or new folks.
(c) How many bugs get filed against this style guide that are related to content? Not a lot, that I can see.
(d) for the inverse to this argument, try removing the style, and see if anyone complains. That should validate whether or not it was useful, correct? I think you'll find that no one will complain about it's removal, for the same reason no one complains about it being in there - the user base is inherently lazy and mostly can't be bothered to file bugs about cosmetic problems.

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.

@bion

This comment has been minimized.

Copy link

commented Feb 23, 2019

as an experienced ruby dev, I like being able to read 1_000_000_000 instead of 1000000000 and have come across it numerous times in code written by others.

@booch

This comment has been minimized.

Copy link

commented Apr 23, 2019

@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 1_000_000_000 than 1000000000, then you're definitely in the minority.

@h0jeZvgoxFepBQ2C

This comment has been minimized.

Copy link

commented Sep 17, 2019

Yeah.. And did you even try what happens, if you enter a list of values with normal numbers like 1_50 and then do this with lower numbers like 0_50?

YOU WILL FREAK OUT 👿 👿 👿 👿 👿

Really. Try it.

@tom-lord

This comment has been minimized.

Copy link

commented Sep 26, 2019

@h0jeZvgoxFepBQ2C That's got nothing to do with the underscore syntax.

If you prefix a number with a 0, then it becomes an octal value. It's also possible to define hexadecimal and binary numbers, in a similar manner.

So in your example, 0_50 == 050 == 5 * 8 == 40.

@cameronbourgeois

This comment has been minimized.

Copy link

commented Sep 27, 2019

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.

Who in their right mind is writing methods or variables named 1_000_000_000 ?

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.

@h0jeZvgoxFepBQ2C

This comment has been minimized.

Copy link

commented Sep 27, 2019

I'm doing this, since its much easier to read how much it is via 1_000_000_000than 1000000000.

Also if you want to save currency values as integer cents, f.e. 1,5 € => 1_50

@cameronbourgeois

This comment has been minimized.

Copy link

commented Sep 30, 2019

I'm doing this, since its much easier to read how much it is via 1_000_000_000than 1000000000.

Also if you want to save currency values as integer cents, f.e. 1,5 € => 1_50

@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 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
8 participants
You can’t perform that action at this time.