当只使用一个变量时,使用多个赋值是不好的做法吗?

时间:2021-01-05 23:01:39

In the following example of multiple assignment, the variable will_not_be_used will not be referenced anywhere else. However, I personally find this approach to be simple and easy to understand.

在下面的多重赋值示例中,在其他任何地方都不会引用will_not_be_used变量。然而,我个人认为这种方法简单易懂。

Is this considered a bad practice?

这被认为是不好的做法吗?

useful,will_not_be_used = my_hash.detect { |key,value| value == "foo" }

2 个解决方案

#1


8  

It's idiomatic to use an underscore (_) for throwaway values like that:

使用下划线(_)表示这样的一次性价值是惯用的:

useful, _ = my_hash.detect { |key,value| value == "foo" }

Update: As pointed out by Jörg W Mittag in the comments below, Ruby itself understands _ to be a throwaway variable: when run with warnings enabled, Ruby (at least MRI, possibly others) will warn about unused local variables unless they start with an underscore.

更新:如下面评论中Jorg W Mittag所指出的,Ruby本身理解_是一个可丢弃的变量:当启用了警告时,Ruby(至少是MRI,可能还有其他)将警告未使用的局部变量,除非它们以下划线开头。

#2


1  

Throw-Away Values

There's nothing inherently wrong with using multiple assignment to discard throw-away values, but it may not be as intention-revealing as other constructions. Depending on your data or its intended use, one of the following may be more verbose but semantically clearer:

使用多重赋值来丢弃一次性的值并没有本质上的错误,但它可能没有其他结构那样具有意图性。根据您的数据或它的预期用途,以下其中一个可能更详细,但语义更清楚:

my_hash = {bar: "foo", baz: "quux"}
# => {:bar=>"foo", :baz=>"quux"}

my_hash.rassoc('foo').first
# => :bar

my_hash.select { |k,v| v == "foo" }.keys.first
# => :bar

my_hash.map { |key, value| key if value == "foo" }.first
# => :bar

The use of Enumerable#detect may be driving the use of the discarded assignment, but there are certainly other use cases worth considering. For your specific example, though, it probably makes no difference.

使用可枚举的#检测可能会驱动被丢弃的任务的使用,但是肯定还有其他值得考虑的用例。但是,对于您的特定示例,这可能没有什么区别。

#1


8  

It's idiomatic to use an underscore (_) for throwaway values like that:

使用下划线(_)表示这样的一次性价值是惯用的:

useful, _ = my_hash.detect { |key,value| value == "foo" }

Update: As pointed out by Jörg W Mittag in the comments below, Ruby itself understands _ to be a throwaway variable: when run with warnings enabled, Ruby (at least MRI, possibly others) will warn about unused local variables unless they start with an underscore.

更新:如下面评论中Jorg W Mittag所指出的,Ruby本身理解_是一个可丢弃的变量:当启用了警告时,Ruby(至少是MRI,可能还有其他)将警告未使用的局部变量,除非它们以下划线开头。

#2


1  

Throw-Away Values

There's nothing inherently wrong with using multiple assignment to discard throw-away values, but it may not be as intention-revealing as other constructions. Depending on your data or its intended use, one of the following may be more verbose but semantically clearer:

使用多重赋值来丢弃一次性的值并没有本质上的错误,但它可能没有其他结构那样具有意图性。根据您的数据或它的预期用途,以下其中一个可能更详细,但语义更清楚:

my_hash = {bar: "foo", baz: "quux"}
# => {:bar=>"foo", :baz=>"quux"}

my_hash.rassoc('foo').first
# => :bar

my_hash.select { |k,v| v == "foo" }.keys.first
# => :bar

my_hash.map { |key, value| key if value == "foo" }.first
# => :bar

The use of Enumerable#detect may be driving the use of the discarded assignment, but there are certainly other use cases worth considering. For your specific example, though, it probably makes no difference.

使用可枚举的#检测可能会驱动被丢弃的任务的使用,但是肯定还有其他值得考虑的用例。但是,对于您的特定示例,这可能没有什么区别。