简体   繁体   English

有没有更好的方法来减少多个if语句?

[英]Is there a better way to reduce multiple if statements?

I have a function that validates few different conditions. 我有一个可以验证几种不同条件的函数。 Here is the example of my function: 这是我的功能的示例:

 function checkData() { var errorMsg = "", fld1 = 0, //Number(document.getElementById('fld1').value), fld2 = 5, //Number(document.getElementById('fld2').value), fld3 = 1, //Number(document.getElementById('fld3').value), fld4 = 0; //Number(document.getElementById('fld4').value); if (!fld1) { errorMsg += "Error 1\\n\\n"; } if (fld1 === fld4) { errorMsg += "Error 2\\n\\n"; } if (fld2 > fld4) { errorMsg += "Error 3\\n\\n"; } if (fld3 > 3) { errorMsg += "Error 4\\n\\n"; } if (errorMsg !== "") { var check = confirm(errorMsg + "\\n Do you want to submit the form?"); if (check) { return true; } else { return false; } } return true; } 
 <button onclick="checkData();">Click Here</button> 

In the example above I hard coded some values for testing purpose. 在上面的示例中,我为测试目的硬编码了一些值。 However, I'm wondering if I can refactor this code and find the better way of achieving the same result? 但是,我想知道是否可以重构此代码并找到实现相同结果的更好方法? Would ternary operators fit better? 三元运算符更适合吗? Or there is another way to get this to work? 还是有另一种方法可以使它起作用? Thank you. 谢谢。

In this use-case I think the 'multiple-ifs' solution is quite clear so it is the one to use. 在这种用例中,我认为“多重假设”解决方案非常清楚,因此是一种可以使用的解决方案。

If you want to optimize a bit, I can only suggest 如果您想优化一点,我只能建议

        if(check){
            return true;
        }else{
            return false;
        }

to become 成为

return !!check;

(the two exclamatives simply cast any object to a boolean vale :-)) (这两个感叹号只是将任何对象强制转换为布尔值:-))

The whole check variable is pointless. 整个check变量毫无意义。 So return confirm is all that you need 所以return confirm就是您所需要的

 function checkData() { var errorMsg = "", fld1 = 0, //Number(document.getElementById('fld1').value), fld2 = 5,//Number(document.getElementById('fld2').value), fld3 = 1,//Number(document.getElementById('fld3').value), fld4 = 0;//Number(document.getElementById('fld4').value); if(!fld1){ errorMsg += "Error 1\\n\\n"; } if(fld1 === fld4){ errorMsg += "Error 2\\n\\n"; } if(fld2 > fld4){ errorMsg += "Error 3\\n\\n"; } if(fld3 > 3){ errorMsg += "Error 4\\n\\n"; } return errorMsg !== "" ? confirm(errorMsg + "\\n Do you want to submit the form?") : true } 
 <button onclick="checkData();">Click Here</button> 

You could refactor your if statements using the ternary operator. 您可以使用三元运算符重构if语句。 But chances are that it would make your code far harder to read. 但是有可能会使您的代码难以阅读。 You could replace 您可以更换

if(check){
    return true;
}else{
    return false;
}

With just return check; 只需return check; as this is a boolean statement anyway. 因为无论如何这是一个布尔语句。

Also, as far as readability goes it would be nice to label your field variables something more meaningful, as knowing that fld2 should always be greater than fld4 isn't immediately obvious from the name. 此外,就可读性而言,最好将字段变量标记为更有意义的名称,因为从名称中并不立即知道fld2应该始终大于fld4

And if you don't care about highlighting the specific error codes then you could of course merge some of your checks together and just return false without the error codes specified, but I suspect you will want to keep that functionality. 而且,如果您不在乎突出显示特定的错误代码,那么您当然可以合并一些检查,并在不指定错误代码的情况下返回false ,但是我怀疑您会希望保留该功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM