首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优惠券系统优化

优惠券系统优化
EN

Code Review用户
提问于 2012-11-22 15:23:33
回答 1查看 300关注 0票数 3

我的优惠券系统有以下代码。它应该能工作,但我相信我可以优化它。

欢迎提出任何建议。

代码语言:javascript
复制
@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
EN

回答 1

Code Review用户

回答已采纳

发布于 2012-11-22 15:56:54

我认为这重复了逻辑:

代码语言:javascript
复制
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设置为值,并在此之后对其进行更改,这种变量的重用会使代码难以理解。

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/18913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档