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]
.
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:
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.
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.