[英]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.