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 · 13 comments
Closed

Underscores in Numeric Literals Considered Bad #535

trims2u opened this issue Jan 27, 2016 · 13 comments

Comments

@trims2u
Copy link

trims2u 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
Copy link

cremno 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
Copy link
Collaborator

bbatsov 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 as completed Jan 27, 2016
@trims2u
Copy link
Author

trims2u 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
Copy link

bion 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
Copy link

booch 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
Copy link

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
Copy link

tom-lord 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
Copy link

cameronbourgeois 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
Copy link

h0jeZvgoxFepBQ2C 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
Copy link

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

@JoergWMittag
Copy link

JoergWMittag commented Apr 26, 2020

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.

In my language, the thousand separator is either a space (a thin non-breaking space , to be exact) or sometimes a prime ().

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:

  • six-per-em-space
  • thin space
  • punctuation space
  • space
  • prime
  • apostrophe
  • underscore
  • comma
  • period

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 …

@rubyFeedback
Copy link

rubyFeedback commented Oct 14, 2023

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

I used to have variables that are like:

_temp = []

I rarely use that style anymore, preferring just _ to dump variables, or using somewhat
longer names that tell me what they do - but I can definitely see people using _. There
may also be variable names such as _42 for whatever the reason. Evidently a variable
name such as 1_000_000_000 would not be valid and I think it is super-super-super
rare for anyone to ever want to use something like _1_000_000_000. But _ definitely
is used for variables, and I can see some numbers used for some variables too, such
as street numbers when you need to keep track of some place/location.

To the topic of _ in numbers: I like them. I can instantly group larger numbers that way.
In fact, I found this discussion here when I wrote a small ruby class converting a
binary number to decimal, and then the display was 100000000. I first calculated the
correct number on paper (for an exam), and then wanted to see whether I made any
mistake. As I could not separate the 0 easily, without _, I also added to the ruby class a
way to show it via _ which made it soooo much easier to know whether I gave the right
answer.

@cameronbourgeois
Copy link

cameronbourgeois commented Oct 15, 2023

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

I used to have variables that are like:

_temp = []

I rarely use that style anymore, preferring just _ to dump variables, or using somewhat longer names that tell me what they do - but I can definitely see people using _. There may also be variable names such as _42 for whatever the reason. Evidently a variable name such as 1_000_000_000 would not be valid and I think it is super-super-super rare for anyone to ever want to use something like _1_000_000_000. But _ definitely is used for variables, and I can see some numbers used for some variables too, such as street numbers when you need to keep track of some place/location.

To the topic of _ in numbers: I like them. I can instantly group larger numbers that way. In fact, I found this discussion here when I wrote a small ruby class converting a binary number to decimal, and then the display was 100000000. I first calculated the correct number on paper (for an exam), and then wanted to see whether I made any mistake. As I could not separate the 0 easily, without _, I also added to the ruby class a way to show it via _ which made it soooo much easier to know whether I gave the right answer.

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.

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

No branches or pull requests

10 participants