关于检查条件

时间:2012-05-31 17:26:34

标签: java

  public static final int FALSE = 0;
  public static final int TRUE = 10;

请告诉我一件事我在下面的代码下面有

     if (!walgreensRewardsSupport.isPreProdOrTrainingMode()
        && walgreensRewardsSupport.isWagRewardsRedeemptionEnabled()
        && null != walgreensRewardsInfoLine
        && null != walgreensRewardsInfoLine.getMemberFullProfile()
        && (null != walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints())
        && (walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints().getCurrentBalance() != 0)
        && !transaction
            .getBooleanValue(ITransactionHashtableWag.REWARDS_REDEMPTION_ZIPCODE_LIMIT_REACHED)
        && !walgreensRewardsRedemptionSupport.isLoyaltyRedemptionTenderLineExist(transaction)
        // No need to force recalculation each time, recalculation happens after any tender line is
        // added to transaction.
        && walgreensRewardsRedemptionSupport.getRedemptionEligibleTransactionAmount(transaction,
            true, false).isGreaterThanZero()) {
      return TRUE;
    }   
else {
      return FALSE;
    }

  }

现在我必须在它之间添加条件...检查是否 walgreensRewardsInfoLine.getMemberFullProfile()。getRewardPoints()。的GetValues()。大小() 是0然后返回False ..我已按以下方式完成..

     if (!walgreensRewardsSupport.isPreProdOrTrainingMode()
        && walgreensRewardsSupport.isWagRewardsRedeemptionEnabled()
        && null != walgreensRewardsInfoLine
        && null != walgreensRewardsInfoLine.getMemberFullProfile()
        && (null != walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints())
        && (walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints().getCurrentBalance() != 0)
        && !transaction
            .getBooleanValue(ITransactionHashtableWag.REWARDS_REDEMPTION_ZIPCODE_LIMIT_REACHED)
        && !walgreensRewardsRedemptionSupport.isLoyaltyRedemptionTenderLineExist(transaction)
        // No need to force recalculation each time, recalculation happens after any tender line is
        // added to transaction.
        && walgreensRewardsRedemptionSupport.getRedemptionEligibleTransactionAmount(transaction,
            true, false).isGreaterThanZero()) {
      return TRUE;
    }else if (walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints().getCurrentBalance() == 0)
{
return FALSE
}   
else {
      return FALSE;
    }

  }

请指教是否正确.. !!

2 个答案:

答案 0 :(得分:2)

在第一个代码块中,您有:

if (condition) {
  return TRUE;
} else {
  return FALSE;
}

这可以简化为:

return (condition) ? TRUE : FALSE;

如果TRUE和FALSE只是布尔值,

return (condition);

在第二个代码块中,您有:

if (condition1) {
  return TRUE;
} else if (condition2) {
  return FALSE;
} else {
  return FALSE;
}

除非你期望评估第二个条件产生副作用,否则else-if没有任何价值。

另请注意,对私有方法的一些深思熟虑的使用可以使这些代码更容易理解。你可以像这样封装大部分条件:

private boolean hasRewardsPointsBalance(InfoLine infoLine) {
  if (null == infoLine) return false;
  MemberProfile profile = infoLine.getMemberFullProfile();
  if (null == profile) return false;
  RewardsPoints rewards = profile..getRewardPoints();
  if (null == rewards) return false;
  return (rewards.getCurrentBalance() != 0);
}

答案 1 :(得分:0)

最好只将逻辑补码添加到逻辑上并将其组合在一起以返回true的条件集。

例如:

 if (!walgreensRewardsSupport.isPreProdOrTrainingMode()

    && walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints()
       .getValues().size() != 0

    && walgreensRewardsSupport.isWagRewardsRedeemptionEnabled()
    && null != walgreensRewardsInfoLine
    && null != walgreensRewardsInfoLine.getMemberFullProfile()
    && (null != walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints())
    && (walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints().getCurrentBalance() != 0)
    && !transaction
        .getBooleanValue(ITransactionHashtableWag.REWARDS_REDEMPTION_ZIPCODE_LIMIT_REACHED)
    && !walgreensRewardsRedemptionSupport.isLoyaltyRedemptionTenderLineExist(transaction)
    // No need to force recalculation each time, recalculation happens after any tender line is
    // added to transaction.
    && walgreensRewardsRedemptionSupport.getRedemptionEligibleTransactionAmount(transaction,
        true, false).isGreaterThanZero()) {

  return TRUE;


这比你编码的要复杂得多。