如何使用if语句重构此代码

ifmq2ha2  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(400)

我有if语句的代码(如下)。有可能重构这段代码吗?因为在这个方法中是丑陋的if语句:(

getOverallAmount(estimate) {
    const {installments} = estimate;
    const {vipValue} = estimate;
    if (installments !== 1) {
      const oneInstallment = vipValue / installments;
      const allWithOneInst = vipValue - (oneInstallment * installments) + 1;
      return oneInstallment + allWithOneInst;
    }
    return vipValue;
  }
zwghvu4y

zwghvu4y1#

使用三元运算符而不是 if 声明:

getOverallAmount(estimate) {
  const {installments} = estimate;
  const {vipValue} = estimate;
  const oneInstallment = vipValue / installments;
  const allWithOneInst = vipValue - (oneInstallment * installments) + 1;

  return (installments !== 1) ? oneInstallment + allWithOneInst : vipValue;
}

相关问题