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

Default for Style/FormatStringToken #8827

Open
marcandre opened this issue Oct 1, 2020 · 12 comments · May be fixed by #10723
Open

Default for Style/FormatStringToken #8827

marcandre opened this issue Oct 1, 2020 · 12 comments · May be fixed by #10723
Labels
question style guide Requires update(s) to the Ruby Style Guide
Milestone

Comments

@marcandre
Copy link
Contributor

Why is our default for Style/FormatStringToken annotated?

I've always personally used the "template" style and I wonder why we have to worry about these suffixes and alternate format?

@marcandre
Copy link
Contributor Author

I believe "template" style is also Rails' standard, and our default is creates false positives: #7452

@koic
Copy link
Member

koic commented Oct 2, 2020

If this rule is most important to specify to named format string tokens (keyword arguments), then I have one idea. E.g.:

# bad
format('%s', 'Hello')

# good
format('%<greeting>s', greeting: 'Hello')
format('%{greeting}', greeting: 'Hello')

I think it can accept both EnforcedStyle: annotated and EnforcedStyle: template styles by default.

So I think this cop can probably be updated to accept multiple EnforcedStyle configuration using AllowMultipleStyles: true to better default configuration.

# .rubocop.yml
Style/FormatStringToken:
  EnforcedStyle:
    - annotated
    - template

cf: #6649

@marcandre
Copy link
Contributor Author

marcandre commented Oct 7, 2020

@koic This seems fine with me, although I would still prefer a single style (template).

For exaple, I'm reviewing #8844 and there is a change like:

-       MSG = '$%{backref} is out of range (%{count} Regexp capture groups detected).'
+       MSG = '$%<backref>s is out of range (%<count>s Regexp capture groups detected).'

and I much prefer the first version. Note that backref is an integer, but %<backref>i or %<backref>s have the same result for integers anyways...

@marcandre
Copy link
Contributor Author

cc/@bbatsov

@marcandre
Copy link
Contributor Author

ping @bbatsov

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 15, 2020

I've always personally used the "template" style and I wonder why we have to worry about these suffixes and alternate format?

Simply because I felt this resulted in the most maitainable code, as it features more info. I knew relatively few people even knew they could name the print placeholders with names and their type and I thought this was a good practice worth promoting. I don't recall anything complaining about the default, even if it was uncommon, so I can only hope this got traction from RuboCop. :-)

I think it can accept both EnforcedStyle: annotated and EnforcedStyle: template styles by default.

That's an option, but I'm not fond of mixing styles for no reason. I do like one recent suggestion to add some limit under which it's ok not to name things, as the benefit is most pronnounced with several placeholders anyways.

@marcandre
Copy link
Contributor Author

There are two orthogonal questions here: naming things (I'm all for it, at least for more than one placeholder) and typing / format. It's really the second I'm questioning.

Note that type "s" features no additional info, as the argument can be any type and to_s is called on it.

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 15, 2020

Well, it helps you figure out the types of the other arguments for format, but it's obviously subjective weather that adds value or not. I agree that the names are definitely the more important part here.

@marcandre
Copy link
Contributor Author

So in conclusion, should I understand you'd rather keep the default as is?

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 16, 2020

I don't have a strong preference, but as the users are not complaining I'd rather not introduce any headaches for them, by changing the default. That's my only consideration. If you want to check whether a lot of people are changing the default (e.g. by doing some scan of publicly available RuboCop config files) and it turns out that's the case - then it'd be fine by me.

@marcandre
Copy link
Contributor Author

I understand the concern.

I think format strings are not that much used so I doubt I would see much config changes even if I knew where to look or public config files.

If we added autocorrection from annotated to template style though, impact on users would be minimal (change the config, or agree with new default and autocorrect).

Concern for users should be weighed with the fact that we currently create issues for Rails routes, among others #7452

@bbatsov bbatsov added this to the 2.0 milestone Nov 5, 2020
@bbatsov bbatsov added question style guide Requires update(s) to the Ruby Style Guide labels Nov 5, 2020
@pirj
Copy link
Member

pirj commented Jun 18, 2022

Checked on real-world-rspec:

  1. annotated

57497 files inspected, 5785 offenses detected

  1. template

57497 files inspected, 1279 offenses detected

  1. unannotated

57497 files inspected, 6424 offenses detected

  1. annotated + template (with AllowMultipleStyles: true and a crutch added to ConfigurableEnforcedStyle):

57497 files inspected, 320 offenses detected

The difference speaks for itself - template is by far the least offended, and the most accepted style.
Personally, I wouldn't allow multiple styles. And in general, AllowMultipleStyles don't seem to be used apart from Layout/HashAlignment.

I encourage everyone in doubt to check against a larger sample, e.g. real-world-ruby-apps.

Unless anyone objects, I intend to change the default enforced style for this cop.

Please keep in mind that:

Out of the box it will enforce many of the guidelines outlined in the community Ruby Style Guide.

A style guide that reflects real-world usage gets used, while a style guide that holds to an ideal that has been rejected by the people it is supposed to help risks not getting used at all - no matter how good it is.

@pirj pirj linked a pull request Jun 18, 2022 that will close this issue
8 tasks
pirj added a commit to pirj/rubocop that referenced this issue Jun 19, 2022
pirj added a commit to pirj/rubocop that referenced this issue Dec 29, 2022
pirj added a commit to rubocop/ruby-style-guide that referenced this issue Dec 30, 2022
According to [research](rubocop/rubocop#8827 (comment)):

annotated (`%<name>s`):
    57497 files inspected, 5785 offenses detected

template (`%{name}`):
    57497 files inspected, 1279 offenses detected

unannotated (`%s`)
    57497 files inspected, 6424 offenses detected

the `%{name}` is the most commonly used style.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question style guide Requires update(s) to the Ruby Style Guide
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants