我的优惠券系统有以下代码。它应该能工作,但我相信我可以优化它。
欢迎提出任何建议。
@price_to_pay = @booking_request.guests * @table_offer.price_cents / 100
@remaining = @coupon.current_amount - @price_to_pay
if @remaining > 0
@coupon.current_amount = @remaining
@price_to_pay = 0
elsif @remaining = 0
@coupon.current_amount = 0
@price_to_pay = 0
elsif @remaining < 0
@coupon.current_amount = @remaining
@price_to_pay = @remaining * -1
end
coupon.save发布于 2012-11-22 15:56:54
我认为这重复了逻辑:
price = @booking_request.guests * @table_offer.price_cents / 100
@remaining = @coupon.current_amount - price
@coupon.current_amount = @remaining
@price_to_play = @remaining >= 0 ? 0 : -@remaining
coupon.save请注意,我避免将@price_to_play设置为值,并在此之后对其进行更改,这种变量的重用会使代码难以理解。
https://codereview.stackexchange.com/questions/18913
复制相似问题