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

Percent literal brackets #526

Closed
christianrolle opened this issue Jan 5, 2016 · 10 comments
Closed

Percent literal brackets #526

christianrolle opened this issue Jan 5, 2016 · 10 comments

Comments

@christianrolle
Copy link

In general I agree with the percent literal styles.
It clearly makes sense to avoid other delimiters than brackets for escaping reasons.
Additionally I would like to question using squared brackets for Array literals is a bad practice, like:

%w[Ruby Javscript Python]

compared to:

%w(Ruby Javscript Python)

I am very interested in the reason for the decision.
From my point of view it expresses the Array character way more. Please let us discuss it.
My thoughts about percentage notations

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 5, 2016

I agree this is a reasonable usage of square brackets.

@christianrolle
Copy link
Author

I am a little surprised. I expected some kind of refusal.
Agreeing this is a reasonable usage of square brackets contradicts the current published style guide.
What does your comment mean?
Does it mean that the style guide could be changed?

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 6, 2016

I am a little surprised. I expected some kind of refusal.
Agreeing this is a reasonable usage of square brackets contradicts the current published style guide.

Like most projects, the guide is never truly finished. :-) As I've said many times - I don't view myself as its author, but just its editor and I'm always open to reasonable suggestions.

Does it mean that the style guide could be changed?

Yes.

@christianrolle
Copy link
Author

O.K. Then I'll send a pull request and let's see, what the community will think about it.

@onebree
Copy link
Contributor

onebree commented Mar 9, 2016

Some comments about percent-literal brackets:

  1. Using hard brackets for percent-literal arrays natually convey the fact you are creating an array.
  2. If the rule states to only use parenthesis, then why does the "good" section list also curly braces?

@ibartley
Copy link

I would like add a vote in favor of the use of square brackets when referencing or inferring a reference to an array. In my humble opinion parenthesis imply params. If %w is a function and the elements of an array are parameters then parens make sense, otherwise brackets seem to convey the message with natural clarity.

@Drenmi
Copy link
Contributor

Drenmi commented Oct 14, 2016

I am not a frequent user of this notation myself, but I can imagine the distinction between a bare array literal and these serves a purpose. If I'm looking at the trailing end of a percent literal like this:

... foo bar]

I must first notice the missing comma, and then follow the line to the beginning to see that it's actually a percent literal, and not a bare array literal with a syntax error.

Now, this is completely speculative, and I am slightly in favour of the change myself, but thought I'd try to argue for the other side for a balanced discussion. 😀

@savef
Copy link

savef commented Oct 14, 2016

I do use the percent literal arrays quite often and I disagree with this change. As @bbatsov does, I think square-bracket percent literals for arrays are perfectly reasonable, but I think rounded brackets are just as reasonable and that's where my personal preference lies. I don't think there's enough of a distinction between a normal array and the square bracket percent-literal array.

It makes sense to me that we have an array shorthand in-line with JSON in [one, two], and that an array of strings shorthand would require some special syntactic sugar that sets it apart from other arrays, and different brackets ensure that.

Both positions are personal preference, and as far as I've seen over the years the rounded brackets is the more popular option. Here are the stats on usage of 3 styles in the Rails codebase:

~/work/rails $ grep -rE "%w\(" | wc -l # %w(a b c)
683
~/work/rails $ grep -rE "%w\[" | wc -l # %w[a b c]
43
~/work/rails $ grep -rE "%w\{" | wc -l # %w{a b c}
85

By no means do I think of the Rails codebase as a style guide, but it's probably a decent indicator of popularity. So since there's no overwhelming majority one way or the other - and if anything a majority on the side of rounded brackets - I'm not sure why we would change it.

I appreciate that other people have different personal preferences (as I have plenty that differ from this style guide), but that's why we have the ability to configure options like this in RuboCop...

@bronson
Copy link

bronson commented Feb 25, 2017

In light of #563, can this be closed?

@christianrolle
Copy link
Author

Sure :)

Drenmi added a commit to Drenmi/rubocop that referenced this issue Apr 2, 2017
…nfiguration to match Style Guide update

The Style Guide was changed to [prefer square brackets for word- and
symbol array literals](rubocop/ruby-style-guide#526).

This change brings RuboCop's config back in line.
bbatsov pushed a commit to rubocop/rubocop that referenced this issue Apr 2, 2017
…tion to match Style Guide update

The Style Guide was changed to [prefer square brackets for word- and
symbol array literals](rubocop/ruby-style-guide#526).

This change brings RuboCop's config back in line.
koic pushed a commit to koic/rubocop-performance that referenced this issue Oct 12, 2018
…tion to match Style Guide update

The Style Guide was changed to [prefer square brackets for word- and
symbol array literals](rubocop/ruby-style-guide#526).

This change brings RuboCop's config back in line.
koic pushed a commit to rubocop/rubocop-rails that referenced this issue Oct 26, 2018
…tion to match Style Guide update

The Style Guide was changed to [prefer square brackets for word- and
symbol array literals](rubocop/ruby-style-guide#526).

This change brings RuboCop's config back in line.
patrickm53 pushed a commit to patrickm53/performance-develop-rubyonrails that referenced this issue Sep 23, 2022
…tion to match Style Guide update

The Style Guide was changed to [prefer square brackets for word- and
symbol array literals](rubocop/ruby-style-guide#526).

This change brings RuboCop's config back in line.
richardstewart0213 added a commit to richardstewart0213/performance-build-Performance-optimization-analysis- that referenced this issue Nov 4, 2022
…tion to match Style Guide update

The Style Guide was changed to [prefer square brackets for word- and
symbol array literals](rubocop/ruby-style-guide#526).

This change brings RuboCop's config back in line.
Cute0110 added a commit to Cute0110/Rubocop-Performance that referenced this issue Sep 28, 2023
…tion to match Style Guide update

The Style Guide was changed to [prefer square brackets for word- and
symbol array literals](rubocop/ruby-style-guide#526).

This change brings RuboCop's config back in line.
SerhiiMisiura added a commit to SerhiiMisiura/Rubocop-Performance that referenced this issue Oct 5, 2023
…tion to match Style Guide update

The Style Guide was changed to [prefer square brackets for word- and
symbol array literals](rubocop/ruby-style-guide#526).

This change brings RuboCop's config back in line.
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

7 participants