ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: gray.kemmey@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:91878] [Ruby trunk Feature#4475] default variable name for parameter
Date: Mon, 18 Mar 2019 22:48:32 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-77146.20190318224831.24077a1018857d34@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-4475.20110306224849@ruby-lang.org

Issue #4475 has been updated by gkemmey (Gray Kemmey).


Is there a reason to do this other than to save some typing? I'm not sure we've proposed any terribly real-world cases here where this made code better.

I feel like we already have some single-character patterns around blocks with one or two arguments, that aren't that onerous to type and still convey meaning. For example:

```ruby
10.times  { |i| ... } # i for index
10.times { |n| ... } # n for number

[1, 2, 3].map { |n| ... } # n for number
[1, 2, 3].map { |e| ... } # e for element

['one', 'two'].map { |s| ... } # s for string

[1, 2, 3].each_with_index { |e, i| ... }  # e for element, i for index

{ }.each { |k, v| ... }  # k for key, v for value

[[], []]each { |r, c| ... } # two-d arrays: r for row, c for column

File.open('') { |f| ... } # f for file
```

For anything more complex, is it bad too *have* to name your arguments?

In no particular order, some downsides I see:

1. It adds some cognitive load to remember the position of arguments. Like in the `{}.each` example -- is `@1` the key or the value? It's obvious when using `k` and `v` which is which.

2. It blurs the line between instance variables.

3. I don't know that we use simple blocks like this *most* of the time, which means, as a newcomer, you won't see this regularly, and it'll be confusing when you do. Think about the first time you saw something like `[1,2,3].reduce(&:+)`. Was that clear to anyone? (*Though in the `&` case, certainly worth the complexity.*)

4. It's always a little strange when things like this aren't 0-based, right?

5. Personally, find it a little jarring to iterate over an array and then use `@1` to reference each element. "At one" just *feels* positional (like an index).

Anyway, this seems like it adds a fair amount of complexity and sacrifices a lot of readability, just to save ~2 characters. Happy to hear why I'm wrong though...

----------------------------------------
Feature #4475: default variable name for parameter
https://bugs.ruby-lang.org/issues/4475#change-77146

* Author: jordi (jordi polo)
* Status: Closed
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
=begin

There is a very common pattern in Ruby:

 object.method do |variable_name|
  variable_name doing something  
 end

 Many times in fact the name of the object is so self explanatory that we don't care about the name of the variable of the block. It is common to see things like :

 @my_sons.each { |s| s.sell_to_someone }

or

 Account.all.each { |a|  my_account << a.money }


 People tend to choose s or a because we have the class or the object name just there to remind you about the context. 


I would like to know if can be a good idea to have a default name for that parameter. I think it is Groovy that does something like:

  Account.all.each { my_account << it.money } 

Where it is automagically filled and it doesn't need to be declared.  

I think it is as readable or more (for newbies who don't know what is ||) and we save some typing :)


=end




-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2019-03-18 22:48 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-4475.20110306224849@ruby-lang.org>
2012-03-18 10:05 ` [ruby-core:43439] [ruby-trunk - Feature #4475][Assigned] default variable name for parameter nahi
2012-11-20 12:26 ` [ruby-core:49705] [ruby-trunk - Feature #4475] " mame (Yusuke Endoh)
2018-04-19  7:51 ` [ruby-core:86598] [Ruby trunk Feature#4475] " knu
2018-04-19 11:42 ` [ruby-core:86607] " matz
2018-09-13  5:44 ` [ruby-core:88969] " niinikazuki
2018-11-06 14:15 ` [ruby-core:89723] " shevegen
2018-11-06 14:33 ` [ruby-core:89724] " hanmac
2019-01-19  5:58 ` [ruby-core:91171] " merch-redmine
2019-02-07  7:35 ` [ruby-core:91458] " matz
2019-02-07 12:48   ` [ruby-core:91470] " Ale Miralles
2019-02-07 12:55     ` [ruby-core:91471] " Ale Miralles
2019-02-08 16:35 ` [ruby-core:91497] " janfri26
2019-02-08 16:55 ` [ruby-core:91498] " merch-redmine
2019-03-11  4:47 ` [ruby-core:91743] " matz
2019-03-11 14:36 ` [ruby-core:91785] " nobu
2019-03-18 11:42 ` [ruby-core:91865] " shevegen
2019-03-18 15:43 ` [ruby-core:91868] " richard.schneeman+ruby-lang
2019-03-18 17:12 ` [ruby-core:91870] " bozhidar
2019-03-18 22:48 ` gray.kemmey [this message]
2019-03-22  0:19 ` [ruby-core:91919] " harlemsquirrel
2019-03-22  0:31 ` [ruby-core:91920] " duerst
2019-03-22  6:56 ` [ruby-core:91923] " pascal.betz
2019-03-22 13:11 ` [ruby-core:91932] " mail

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-77146.20190318224831.24077a1018857d34@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).