6 ms·
What is a compelling use case for omitting values in hashes or method calls? I would think that it just increases cognitive load for the code maintainer, and ma
by buzzdenver 5y ago
What is a compelling use case for omitting values in hashes or method calls? I would think that it just increases cognitive load for the code maintainer, and makes the parser more complex.
- dyeje 5y agoIt removes the boilerplate of repeating the variable name when it's the same as the hash key. It's an extremely popular syntax in the JavaScript world bleeding over into Ruby. Personally, I enjoy it because it feels more fluid.
- vvillena 5y agoRust has had this bit of syntax sugar for a while now, it is used for structs. I think it's nice syntax, because it's located in a place where there's no doubt about the meaning. Both Rust structs and Ruby hashes are defined in-language as key-value sets, so whenever there's just one element instead of a pair, it's clear what the meaning is.
- anentropic 5y agoOCaml has it too! (for records)
- hashhar 5y agoI have noticed that Ruby tends to move towards more magic which is true in this case. I don't see any benefits at all from this.
- gls2ro 5y agoI dont think a programming language can be _magic_ when talking about syntax. Syntax is something that one knows or dont know but no magic is there. It is like saying limit in math is magic. I could agree that we can evaluate if it is magic a library or a standard library. More to the point: In the Ruby apps that I saw so far and articles I read I get a different feeling: more and more the community is promoting clarity, explicitness, simplicity. I somehow feel that from Ruby 2 forward metaprogramming is seen as last resort. Almost no guideline promotes magic in Ruby code by default. More syntax is actually removing the need to do “magic tricks” with the language. It makes explicit in some cases some things that people were doing with DSLs. I feel that magic was more present in Ruby 1.9 for example than now even if syntax was more limited. I was solving a lot of things with metaprogramming, monkey patching, calling private attributes and more. Now I will not approve my own code from back then if I will see it in an MR.
- gls2ro 5y agoWith keyword arguments there are a lot of cases when we do things like def call(batch_id:, language: ) end batch_id = find_batch_id_from_conditions(conditions) language = load_language_settings(user.id) call(batch_id: batch_id, language: language) For me, I would gladly replace the last method with call(batch_id:, language:)
- fourseventy 5y agoI don't like it either. I much prefer simple syntax with limited exceptions and variation. I use ruby every day but I prefer a syntax closer to go.