用于在多行上链接调用的 Ruby 约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12906048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:52:34  来源:igfitidea点击:

Ruby convention for chaining calls over multiple lines

ruby-on-railsrubycoding-style

提问by Dmytrii Nagirniak

What are the conventions for this?

对此有何约定?

I use the folowing style, but not sure it is the preferred one since if I miss a dot at the end I can run into a lot of issue without realising that.

我使用以下样式,但不确定它是否是首选样式,因为如果最后我错过了一个点,我可能会在没有意识到的情况下遇到很多问题。

query = reservations_scope.for_company(current_company).joins{property.development}.
  group{property.development.id}.
  group{property.development.name}.
  group{property.number}.
  group{created_at}.
  group{price}.
  group{reservation_path}.
  group{company_id}.
  group{user_id}.
  group{fee_paid_date}.
  group{contract_exchanged_date}.
  group{deposit_paid_date}.
  group{cancelled_date}.
  select_with_reserving_agent_name_for(current_company, [
                                       "developments.id as dev_id",
                                       "developments.name as dev_name",
                                       "properties.number as prop_number",
                                       "reservations.created_at",
                                       "reservations.price",
                                       "reservations.fee_paid_date",
                                       "reservations.contract_exchanged_date",
                                       "reservations.deposit_paid_date",
                                       "reservations.cancelled_date"
  ]).reorder("developments.name")
query.to_a # ....

So what are the conventions for chainingmethods over multiple linesand which one should I prefer?

那么在多行上链接方法的约定是什么,我应该更喜欢哪一个?

NOTE: I couldn't find a good example from the Ruby coding style guide.

注意:我无法从Ruby 编码风格指南中找到一个好的例子。

回答by Bozhidar Batsov

There is actually a section on that in the Ruby style guide:

实际上在Ruby 风格指南中有一个部分:

Adopt a consistent multi-line method chaining style. There are two popular styles in the Ruby community, both of which are considered good - leading .(Option A) and trailing .(Option B).

  • (Option A)When continuing a chained method invocation on another line keep the .on the second line.

    # bad - need to consult first line to understand second line
    one.two.three.
      four
    
    # good - it's immediately clear what's going on the second line
    one.two.three
      .four
    
  • (Option B)When continuing a chained method invocation on another line, include the .on the first line to indicate that the expression continues.

    # bad - need to read ahead to the second line to know that the chain continues
    one.two.three
      .four
    
    # good - it's immediately clear that the expression continues beyond the first line
    one.two.three.
      four
    

A discussion on the merits of both alternative styles can be found here.

采用一致的多行方法链接样式。Ruby 社区中有两种流行的风格,这两种风格都被认为是好的——前导.(选项 A)和尾随.(选项 B)。

  • (选项 A)当在另一行上继续一个链式方法调用时,将 保持.在第二行。

    # bad - need to consult first line to understand second line
    one.two.three.
      four
    
    # good - it's immediately clear what's going on the second line
    one.two.three
      .four
    
  • (选项 B)当在另一行上继续一个链式方法调用时,.在第一行包含 以指示表达式继续。

    # bad - need to read ahead to the second line to know that the chain continues
    one.two.three
      .four
    
    # good - it's immediately clear that the expression continues beyond the first line
    one.two.three.
      four
    

可以在此处找到有关这两种替代风格优点的讨论 。

回答by detunized

In Ruby 1.9+ it's possible to write like this:

在 Ruby 1.9+ 中可以这样写:

query = reservations_scope
  .for_company(current_company)
  .joins{property.development}
  .group{property.development.id}
  .group{property.development.name}
  .group{property.number}
  .group{created_at}
  .group{price}
  .group{reservation_path}
  .group{company_id}
  .group{user_id}

Much more readable, I think.

我认为更具可读性。

回答by Rory O'Kane

Here is a complete list of pros and consof four options. Two of the options have not been mentioned in any other answer.

这是四个选项的优缺点完整列表。在任何其他答案中都没有提到其中两个选项。

Pros and cons can be broken into unique ones and shared ones. Shared pros are the inverses of a unique con of another option. Similarly, shared cons are the inverses of a unique pro of another option. There are also some points that are pros for two options and cons for the other two.

优缺点可以分为独特的和共享的。共享优点是另一种选择的独特缺点的反面。同样,共享的缺点是另一种选择的独特优点的反面。也有一些观点对两个选项有利,而对其他两个选项有利。

To avoid repeating explanations, I describe each option's sharedpros and cons with just a summary of that point. Full details about a shared pro or con are available in the description of their inverse con or pro in another option's unique section. For the points that are pros of two options and cons of the other two, I arbitrarily chose to put the full explanations in the set that starts with “.at line beginning”.

为避免重复解释,我仅通过对这一点的总结来描述每个选项的共同优点和缺点。有关共享的优点或缺点的完整详细信息可在另一个选项的独特部分中的反向缺点或优点的描述中找到。对于两个选项的优点和其他两个选项的缺点,我随意选择将完整的解释放在以“ .at line begin”开头的集合中。

For a shorter list that leaves the shared pros and cons implicit instead of repeating them, see this old versionof this answer.

对于一个较短的列表,它使共享的优点和缺点隐含而不是重复它们,请参阅此答案的旧版本



.at line end

.在行尾

items.get.lazy.
  take(10).
  force

Pros

优点

Shared仅与另一种选择共享
  • Continuing lines can be commented out freely, and comments can be added between lines
  • Pastable into IRB/Pry
  • Supported in Ruby 1.8
  • 连续行可以自由注释,行间可以添加注释
  • 可粘贴到 IRB/Pry
  • 支持 Ruby 1.8
Shared with two other options:与其他两个选项共享:
  • When you read the initial line, it is clear that the expression continues
  • Plenty of horizontal space for continuing lines
  • Doesn't require manual alignment of characters into columns
  • Looks fine when viewed in a proportional font
  • Has a minimum of punctuation, reducing typing and visual noise
  • 当您阅读初始行时,很明显表达式继续
  • 充足的水平空间用于连续线
  • 不需要将字符手动对齐到列中
  • 以比例字体查看时看起来不错
  • 标点符号最少,减少打字和视觉噪音

Cons

缺点

独特的:
  • Continuing lines look strange on their own. You must read the preceding line to understand that they are a continuation.
    • Indentation is not a reliable indicator that a line continues from the previous line – it could merely mean the start of a block.
  • 连续的线条本身看起来很奇怪。您必须阅读前一行以了解它们是一个延续。
    • 缩进不是一行从前一行继续的可靠指示——它可能仅仅意味着一个块的开始。
Shared仅与另一种选择共享
  • When editing the code, it is harder to comment out or reorder the last line
  • 编辑代码时,更难注释掉或重新排序最后一行


.at line beginning

.在行首

items.get.lazy
  .take(10)
  .force

Pros

优点

Shared仅与另一种选择共享
  • When editing the code, it is easier to comment out or change the order of the last line – no need to delete and add .or \.
  • 编辑代码时,更容易注释掉或更改最后一行的顺序——无需删除和添加.\
Shared with two other options:与其他两个选项共享:
  • Continuing lines can be understood when seen on their own
  • Plenty of horizontal space for continuing lines
  • Doesn't require manual alignment of characters into columns
  • Looks fine when viewed in a proportional font
  • Has a minimum of punctuation, reducing typing and visual noise
  • 连续的线条单独看就可以理解
  • 充足的水平空间用于连续线
  • 不需要将字符手动对齐到列中
  • 以比例字体查看时看起来不错
  • 标点符号最少,减少打字和视觉噪音

Cons

缺点

独特的:
  • When you read the initial line, it's not immediately clear that the expression continues
    • If you use this in your codebase, then when you read a line, you must always check the line after to make sure that it doesn't affect the initial line's meaning.
  • 当您阅读初始行时,并不能立即清楚表达式是否继续
    • 如果您在代码库中使用它,那么当您阅读一行时,您必须始终检查该行以确保它不会影响初始行的含义。
Shared仅与另一种选择共享
  • The code silently breaks if you #comment out a continuing line, or add a comment between lines
  • You can't paste this code into IRB/Pry without it being misinterpreted
  • Not supported in Ruby 1.8 and below
  • 如果您#注释掉连续行,或在行之间添加注释,代码将无声地中断
  • 您不能将此代码粘贴到 IRB/Pry 中而不会被误解
  • Ruby 1.8 及更低版本不支持


.at line beginning, indented to the previous .

.在行首,缩进到前一行 .

items.get.lazy
         .take(10)
         .force

Pros

优点

Shared仅与另一种选择共享
  • When editing the code, it is easier to comment out or change the order of the last line – no need to delete and add .or \.
  • 编辑代码时,更容易注释掉或更改最后一行的顺序——无需删除和添加.\
Shared with two other options:与其他两个选项共享:
  • When you read the initial line, it is clear that the expression continues
  • Continuing lines can be understood when seen on their own
  • Has a minimum of punctuation, reducing typing and visual noise
  • 当您阅读初始行时,很明显表达式继续
  • 连续的线条单独看就可以理解
  • 标点符号最少,减少打字和视觉噪音

Cons

缺点

独特的:
  • Each line's code must fit into less horizontal space
  • Requires manual alignment of the .s into columns
    • It is easier if you have an editor plugin for aligning text, but still more work than using default indentation rules.
    • Even if your editing setup includes a plugin for alignment, your coworkers' setups may not.
  • The code will look misaligned when viewed in a proportional font
  • 每行的代码必须适合较少的水平空间
  • 需要将.s手动对齐到列中
    • 如果你有一个用于对齐文本的编辑器插件会更容易,但仍然比使用默认缩进规则做更多的工作。
    • 即使您的编辑设置包含用于对齐的插件,您同事的设置也可能没有。
  • 以比例字体查看时,代码看起来会错位
Shared仅与另一种选择共享
  • The code silently breaks if you #comment out a continuing line, or add a comment between lines
  • You can't paste this code into IRB/Pry without it being misinterpreted
  • Not supported in Ruby 1.8 and below
  • 如果您#注释掉连续行,或在行之间添加注释,代码将无声地中断
  • 您不能将此代码粘贴到 IRB/Pry 中而不会被误解
  • Ruby 1.8 及更低版本不支持
Shared with two other options:与其他两个选项共享:
  • When editing the code, it is harder to comment out or reorder the last line
  • 编辑代码时,更难注释掉或重新排序最后一行


\at line end, .at next line's beginning

\在行尾,.在下一行的开头

items.get.lazy \
  .take(10) \
  .force

Pros

优点

Shared仅与另一种选择共享
  • Continuing lines can be commented out freely, and comments can be added between lines
  • Pastable into IRB/Pry
  • Supported in Ruby 1.8
  • 连续行可以自由注释,行间可以添加注释
  • 可粘贴到 IRB/Pry
  • 支持 Ruby 1.8
Shared with two other options:与其他两个选项共享:
  • When you read the initial line, it is clear that the expression continues
  • Continuing lines can be understood when seen on their own
  • Plenty of horizontal space for continuing lines
  • Doesn't require manual alignment of characters into columns
  • Looks fine when viewed in a proportional font
  • 当您阅读初始行时,很明显表达式继续
  • 连续的线条单独看就可以理解
  • 充足的水平空间用于连续线
  • 不需要将字符手动对齐到列中
  • 以比例字体查看时看起来不错

Cons

缺点

独特的:
  • Requires more typing
  • Creates more visual noise
  • 需要更多的打字
  • 产生更多视觉噪音
Shared仅与另一种选择共享
  • When editing the code, it is harder to comment out or reorder the last line
  • 编辑代码时,更难注释掉或重新排序最后一行

回答by rosenfeld

The reason why I choose the dot in the end of the line is that it will allow you to paste code in an IRB session. Also, you can't comment lines in the middle of the multi-line code if you use the dots in the beginning of the lines. Here's a good discussion to read: https://github.com/bbatsov/ruby-style-guide/pull/176

我选择行尾的点的原因是它允许您在 IRB 会话中粘贴代码。此外,如果在行的开头使用点,则不能在多行代码的中间注释行。这是一个很好的讨论:https: //github.com/bbatsov/ruby-style-guide/pull/176