-
Notifications
You must be signed in to change notification settings - Fork 16
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
Enable Hash shorthand syntax - accept both new and old syntax #195
Enable Hash shorthand syntax - accept both new and old syntax #195
Conversation
This enabled both type of hash syntax: ```ruby {foo: , bar: bar} {foo: , bar: baz} {foo:, bar:} # shorthand syntax {foo: foo, bar: baz} # old/currnet syntax ``` Source: https://docs.rubocop.org/rubocop/cops_style.html#stylehashsyntax
Arguments for Hash Shorthand SyntaxHere are some arguments for using the shorthand syntax for hashes and with it what is called method punning, and I will try to express this in some steps argument. Hash shorthand syntaxHere are some arguments for hash shorthand syntax When working with hash, it sometimes happens to write boilerplate code: client_id = get_client_id(app)
response_type = get_response_from(request)
redirect_url = # ...
params = {
client_id: client_id,
response_type: response_type,
redirect_url: redirect_url
} This makes the code longer, as sometimes specifying keys and values like this will make the line longer. Thus we break it on multiple lines, adding to longer diffs when changing. Citing Matz from "Treating Code as an Essay":
Thus it is easier to read the following: client_id = get_client_id(app)
response_type = get_response_from(request)
redirect_url = # ...
params = { client_id:, response_type:, redirect_url: } What before spanned five lines is now contained in one single line. One can make the case that to read the second piece of code you have to know how the hash shorthand syntax works; thus it needs extra information. While this is a valid argument, it is also a very general argument: the same can be said about using Method call punningAccepting hash shorthand syntax opens the path to another nice feature of Ruby 3.1 Method punning means to call a method that has keyword arguments in the same way as the hash shorthand syntax: def response(client_id:, response_type:, redirect_url:)
end
client_id = get_client_id(app)
response_type = get_response_from(request)
redirect_url = # ...
# 👇 method can be called omitting the keyword args values
response(client_id:, response_type:, redirect_url:) Arguments for method call punning
Examples: class SocialMediaService
def initialize(data)
@data = data
@client = Twitter::Client.new
end
def run
height, width = get_resolution(@data)
media_key = get_media_key(@data)
tweet = @data.fetch(:body)
author = get_author_id
@client.tweet(
tweet: tweet,
media_key: media_key,
height: height,
width: width,
author: author
)
end
end
class SocialMediaService
def initialize(data)
@data = data
@client = Twitter::Client.new
end
def run
height, width = get_resolution(@data)
media_key = get_media_key(@data)
tweet = @data.fetch(:body)
author = get_author_id
@client.tweet(tweet:, media_key:, height:, width:, author:)
end
end Rubocop rulesRubocop defines a couple of options for the Shorthand Syntax. I think probably there are three that can be of interest:
I read the conversation on 192, and it is not clear to me from there what the conclusion was about if we want or not to use the new syntax. What I got from there was that we don't want to enable the I propose to use |
@cookpad/global-web-devs what do you think about this? |
I didn't know you can now do this. Nice feature. 👍 from me. |
Thanks @lucianghinda I wanted to raise a general discussion about this feature a few months ago. My view is that we should embrace all features of the language. I don't think we should be auto-correcting old instances though, as we have a lot of them, and this will add unnecessary noise to PRs. |
Thank you @sikachu for the feedback. I am glad that you are on board with this |
Thank you @Bodacious for the reply. I read your previous PR about enabling this behaviour that inspired me to open this new PR and now read the Slack conversation. I saw there that the discussion focused in the end on changing the old code. This is why my proposal is to adopt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in #192 (review) as well. I'm 101% for this 👍
Thanks @lucianghinda 👍 I was confused by the explanation of
I think that's meant to say "like 'either', but will avoid mixing styles in a single hash". It's not like "always" at all, unless I'm missing something, because |
This enables both type of hash syntax:
Source: https://docs.rubocop.org/rubocop/cops_style.html#stylehashsyntax
See my arguments for this 👇