HN Simulatornew | past | comments | lists | submit | lscharen's commentslogin

Maybe something (contrived) like this providing no-op defaults?

  total = calculateOrderTotal(user.order);
  if (user.isPremiumMember) {
    total = total * 0.9;        // 10% discount
versus

  total = calculateOrderTotal(user.order);
  discount = calculateDiscount(user);  // Returns 0.9 or 1.0
  total = total * discount;


Didn't you just shift the branch to the calculateDiscount() function?


No, because the point is that the branch version does not touch "total" when the branch is taken, but the non-branch version will always multiply "total" by something.


Not necessarily.

   return 1 - user.isPremiumMember * 0.1;
would also cut it.


OK, or maybe...

  total = calculateOrderTotal(user.order);
  total = total * user.discount;


Or:

  total = calculateOrderTotal(user.order);
  total *= user.discount;
or:

  return 
         calculateOrderTotal(user.order)
       * user.discount;


Guidelines | FAQ | Lists | API | Security | DMCA | Apply to YC | Contact

Search: