ruby on rails - 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} ready pick-up" parsehelpers.publish_batching_status(user_id, *parse_events, message) { 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 assignment branch condition size send_deliverer_push_notification high. [16.16/15]
.
how can fix it?
from example clear don't need many of intermediary variables , therefore assignments. parameters building code necessary should moved 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} ready pick-up" parsehelpers.publish_batching_status(order.user_id, *parse_events, message) 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, @sergio tulentsev explained @ great length, should worry code conciseness , readability, not score. score can way spot places require attention, in no way objective measure of code quality.
looking @ example, verbose , not on separation of concerns aspect. line:
role: shopper.find_by(id: order_fulfillment.shopper_id).roles.pluck(:name).first
should better expressed shopper
method this:
class shopper < ... ... def self.role_of(shopper_id) find_by(id: shopper_id).roles.pluck(:name).first end end
then replace line more readable
role: shopper.role_of(order_fulfillment.shopper_id)
something similar done line:
batch.find_by(shopper_id: order_fulfillment.shopper_id).id
the logic placed orderfullfillment
's method.
following approach reach point code consist of smaller building blocks, combinable needed. there other benefits approach such better testability , potential readability. should careful not go opposite extent , make every line separate method, @sergio tulentsev warned us.
Comments
Post a Comment