简体   繁体   English

分配分支条件太高

[英]Assignment Branch Condition is too high

My code is:我的代码是:

def send_deliverer_push_notification(order_fulfillment, parse_events)
shopper_id               = order_fulfillment.shopper_id
order                = Order.find_by(id: order_fulfillment_id)
user_id              = order.user_id
role_name            = Shopper.find_by(id: shopper_id).roles.pluck(:name).first
batch_id             = Batch.find_by(shopper_id: shopper_id).id
message              = "Order # #{order_fulfillment.order_id} is now ready for pick-up"

ParseHelpers.publish_batching_status(user_id, *parse_events, message) do
    {
    shopper_id:   shopper_id,
    role:     role_name,
    task:     order_fulfillment.shopper_status,
    batch_id: batch_id,
    fulfillment_number: "#{order.order_number}-#{order_fulfillment.store_id}"
    }
    end
  end

and I get Assignment Branch Condition size for send_deliverer_push_notification is too high. [16.16/15]我得到Assignment Branch Condition size for send_deliverer_push_notification is too high. [16.16/15] Assignment Branch Condition size for send_deliverer_push_notification is too high. [16.16/15] . Assignment Branch Condition size for send_deliverer_push_notification is too high. [16.16/15]

How can I fix it?我该如何解决?

From your example it is clear you don't need many of the intermediary variables and therefore assignments.从您的示例中可以清楚地看出,您不需要很多中间变量,因此也不需要赋值。 All the parameters building code that is actually necessary should be moved to a separate method:实际需要的所有参数构建代码应移至单独的方法:

def send_deliverer_push_notification(order_fulfillment, parse_events)
  order = Order.find_by(id: order_fulfillment_id)
  message = "Order # #{order_fulfillment.order_id} is now ready for pick-up"

  ParseHelpers.publish_batching_status(order.user_id, *parse_events, message) do
    batching_status_params(order, order_fulfillment)
  end
end

def batching_status_params(order, order_fulfillment)
  {
    shopper_id: order_fulfillment.shopper_id,
    role: Shopper.find_by(id: order_fulfillment.shopper_id).roles.pluck(:name).first,
    task: order_fulfillment.shopper_status,
    batch_id: Batch.find_by(shopper_id: order_fulfillment.shopper_id).id,
    fulfillment_number: "#{order.order_number}-#{order_fulfillment.store_id}"
  }
end

But it's better to worry about code conciseness and readability, not a score.但最好担心代码的简洁性和可读性,而不是分数。 The score can be a good way to spot places that require attention.分数可能是发现需要注意的地方的好方法。 It is in no way an objective measure of the code quality.它绝不是代码质量的客观衡量标准。

Looking at this example, it is clearly too verbose and is not very good on the separation of concerns aspect.看这个例子,它显然过于冗长,在关注点分离方面不是很好。 This line:这一行:

role: Shopper.find_by(id: order_fulfillment.shopper_id).roles.pluck(:name).first

Should probably be better expressed as a Shopper method like this:应该更好地表达为像这样的Shopper方法:

class Shopper < ...
  ...

  def self.role_of(shopper_id)
    find_by(id: shopper_id).roles.pluck(:name).first
  end
end

Then you could replace that line with a much more readable然后你可以用更具可读性的替换该行

role: Shopper.role_of(order_fulfillment.shopper_id)

Something similar could be done with this line:可以用这条线做类似的事情:

Batch.find_by(shopper_id: order_fulfillment.shopper_id).id

The logic could be placed to an OrderFullfillment 's method.该逻辑可以放在OrderFullfillment的方法中。

Following this approach you could reach a point where your code would consist of smaller building blocks, easily combinable as needed.遵循这种方法,您可以达到一个点,即您的代码将由更小的构建块组成,并且可以根据需要轻松组合。 There are other benefits to this approach such as better testability and potential readability.这种方法还有其他好处,例如更好的可测试性和潜在的可读性。 Be careful not to go to the opposite extent and make every line a separate method.注意不要将 go 设置为相反的程度,并使每一行都成为单独的方法。

you have to move away你必须搬走

shopper_id               = order_fulfillment.shopper_id
order                = Order.find_by(id: order_fulfillment_id)
user_id              = order.user_id
role_name            = Shopper.find_by(id: shopper_id).roles.pluck(:name).first
batch_id             = Batch.find_by(shopper_id: shopper_id).id
message              = "Order # #{order_fulfillment.order_id} is now ready for pick-up"

to to separate method or even couple methods and set it as attributes分离方法甚至耦合方法并将其设置为属性

same for ParseHelpers.publish_batching_status ParseHelpers.publish_batching_status相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么是'分配分支条件大小太高'以及如何解决它? - What is meant by 'Assignment Branch Condition Size too high' and how to fix it? rails查询对象中的“调用的分配分支条件大小太高” - "Assignment Branch Condition size for call is too high" in rails query object Rubocop 错误:function 的分配分支条件大小过高。 我怎样才能减少这个? - Rubocop error: Assignment Branch Condition size for a function is too high. How can I decrease this? Rubocop:方法的分配分支条件大小太高。 我怎样才能减少方法? - Rubocop: Assignment Branch Condition size for method is too high. How can I reduce the method? Metrics/AbcSize:fill_arrays 的分配分支条件大小太高。 [&lt;9, 21, 0&gt; 22.85/17] - Metrics/AbcSize: Assignment Branch Condition size for fill_arrays is too high. [<9, 21, 0> 22.85/17] update_status 的分配分支条件大小太高。 [<1, 18, 8> 19.72/17] - Assignment Branch Condition size for update_status is too high. [<1, 18, 8> 19.72/17] 重构代码以减少分配分支条件大小 - Refactor the code to reduce Assignment Branch Condition size 辅助分支条件太高 robocop 进攻 - assigenment branching condition is too high robocop offense 索引的圈复杂度太高 - Cyclomatic complexity for index is too high 圈复杂度对于方法来说太高了 - cyclomatic complexity is too high rubocop for method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM