重构if语句JavaScript

时间:2019-02-08 20:39:11

标签: javascript

我想知道如何重构它?我再说一遍,我觉得这不是写它的最好方法:

if (operator === "+") {
    strength += 2;
    up = 4 * strength;
    if (up > 40) up = 40;
    final.base += up;
} else if (operator === "-") {
    up = 4 * strength;
    if (up > 40) up = 40;
    final.base -= up;
    strength -= 2;
}

由于位置很重要,我真的没有找到适当重构的方法。有没有办法清除此功能?

7 个答案:

答案 0 :(得分:9)

如果以后不使用up,则可以更紧凑地编写它。

Math.min

答案 1 :(得分:2)

对于保留up变量,我将执行以下操作:

if (operator === "+")
{
    up = Math.min(4 * (strength += 2), 40);
    final.base += up;
}
else if (operator === "-")
{
    final.base -= (up = Math.min(4 * strength, 40));
    strength -= 2;
}

如果不需要up变量,可以简化为:

if (operator === "+")
{
    final.base += Math.min(4 * (strength += 2), 40);
}
else if (operator === "-")
{
    final.base -= Math.min(4 * strength, 40);
    strength -= 2;
}

如果您不需要up变量,并且也只有+-运算符,那么您可以这样做:

strength += (operator === "+") ? 2 : 0;
final.base += (operator === "+" ? 1 : -1) * Math.min(4 * strength, 40);
strength -= (operator === "-") ? 2 : 0;

答案 2 :(得分:2)

我的回答不是重构if..else,而是要提前考虑您的应用如何增长,这是做出正确选择的原因。

在具有复杂逻辑的大型应用中,您必须抽象方法以使您的代码更灵活。

例如,让Operations类抽象if..else开关(您可以扩展该开关)怎么样?

class Operations {
  static plus (base, strength) {
    base = parseInt(base);
    strength = parseInt(strength);
    
    strength += 2;
    base += Math.min(40, 4 * strength);
    
    return [base, strength];
  }
  
  static minus (base, strength) {
    base = parseInt(base);
    strength = parseInt(strength);
    
    base -= Math.min(40, 4 * strength);
    strength -= 2;
    
    return [base, strength];
  }

  static do (operation) {
    const operators = {
      '+' : Operations.plus,
      '-' : Operations.minus
    }

    const args = Object.values(arguments).slice(1);
    
    if (!operators[operation]) {
      return args;
    }

    return operators[operation].apply(null, args);
  }
}

   
let final = {base: 10};
let strength = 10;
let newBase, newStrength;

console.log('Before. base:', final.base, 'strength:', strength);

// NO IF ELSE ON OPERATOR (:
[newBase, newStrength] = Operations.do('+', final.base, strength);
strength = newStrength;
final.base = newBase;

console.log('After "+" operation. base:', final.base, 'strength:', strength);

[newBase, newStrength] = Operations.do('-', final.base, strength);
strength = newStrength;
final.base = newBase;

console.log('After "-" operation. base:', final.base, 'strength:', strength);

答案 3 :(得分:1)

要解决复制问题,您可以添加一个 <h1>De zakelijke VR/AR hardware leverancier van Benelux</h1> <div class="img-wrapper"> <img class="img-responsive" src="vrgt.png"> </div> <a href="#" class="btn">Meer Info</a>因子,因为此处更改的大部分只是 sign

multiplier

注意

这仅在let multiplier = 1; if (operator === "-") multiplier = -1; up = 4 * strength; strength += multiplier * 2; if (up > 40) up = 40; final.base += multiplier * up; operator-时有效。如果是+之类的字符,就好像操作符是*

答案 4 :(得分:1)

您可以将操作放在一个对象中:

const obj = {
    "+" : {
        strength : prevStrength => prevStrength + 2,
        finalBase: (prevFinalBase , up) =>  prevFinalBase + Math.min(40, 4 * strength)
    },  
    "-" : {
        strength : prevStrength => prevStrength - 2,
        finalBase: (prevFinalBase , up) => prevFinalBase - Math.min(40, 4 * strength)
    }
}

strength = obj[operator].strength(strength);
finalBase = obj[operator].finalBase(finalBase);

var operator = "+";
var strength = 3;
var finalBase = 5;

const obj = {
  "+": {
    strength: prevStrength => prevStrength + 2,
    finalBase: (prevFinalBase, up) => prevFinalBase + Math.min(40, 4 * strength)
  },
  "-": {
    strength: prevStrength => prevStrength - 2,
    finalBase: (prevFinalBase, up) => prevFinalBase - Math.min(40, 4 * strength)
  }
}

strength = obj[operator].strength(strength);
finalBase = obj[operator].finalBase(finalBase);

console.log({
  strength,
  finalBase
})

答案 5 :(得分:0)

您可以考虑将逻辑编写为一系列条件三元运算符:

let isAdd = operator === '+'
strength += isAdd ? 2 : 0;
up = 4 * strength;
if(up > 40) up = 40;
final.base += isAdd ? up : (-1 * up)
strength -= isAdd ? 0 : 2;

答案 6 :(得分:0)

如何?

 /*ASCII Code For "-" = 45, "+" = 43*/
 operator = (44-operator.charCodeAt(0));
 if(operator > 0) strength += 2;
 up = 4*strength;
 if(up > 40) up = 40;
 final.base = final.base + (operator*up);
 if(operator < 0) strength -= 2;