简体   繁体   English

如何有效地缩短这些 if 语句?

[英]How to efficiently shorten these if statements?

I am working on a project and in the code I added a few if statements.我正在做一个项目,并在代码中添加了一些 if 语句。 I was then told that I can do it on line and more efficiently.然后有人告诉我,我可以在线完成,而且效率更高。 The way I did works perfectly but I need to refactor to get it accepted.我所做的方式完美无缺,但我需要重构以使其被接受。 Could you please help me out?你能帮帮我吗? I have tried ternary operator as you can see in the examples below but it's still not that short正如您在下面的示例中所见,我已经尝试过三元运算符,但它仍然没有那么短

Assuming that we have two arrays arr1 and arr2 and the following code is implemented to check if their lengths.假设我们有两个数组arr1arr2并且执行以下代码来检查它们的长度。

const hasValArr1 = ():boolean => return arr1.length > 0
const hasValArr2 = ():boolean => return arr2.length > 0

Now the interesting part if statements现在有趣的部分if 语句

const isEmpty():boolean => {

if (!hasValArr1() && !hasValArr2()) return false

else if (hasValArr1() && hasValArr2()) return true 

else if (!hasValArr1() && hasValArr2()) return true 

else if (hasValArr1() && !hasValArr2()) return true

}

using ternary operator使用三元运算符

 (!hasValArr1() && !hasValArr2()) ? false

:(hasValArr1() && hasValArr2()) ? true 

:(!hasValArr1() && hasValArr2()) ? true 

:(hasValArr1() && !hasValArr2()) && true

How would you go to write this in a more readable and efficient way?您将如何以更易读和更有效的方式编写此代码? Thanks in advance!提前致谢!

The implementation does not match the name of the method.实现与方法的名称不匹配。 The name of the method is isEmpty but it returns false if both arrays don't have a value: if (!hasValArr1() && !hasValArr2()) return false该方法的名称是isEmpty但如果两个数组都没有值,则返回 false: if (!hasValArr1() && !hasValArr2()) return false

So the name should be: hasAnyValue or doArraysHaveAnyValue or something of that sorts.所以名称应该是: hasAnyValuedoArraysHaveAnyValue或类似的东西。

As for simplification, you can simply use ||至于简化,你可以简单地使用|| :

const doArraysHaveAnyValue(): boolean => {
    return hasValArr1() || hasValArr2();
}

The reason this is better is that it is easier to read, and gives preference to using "positive" instead of negation with !这更好的原因是它更容易阅读,并且优先使用“积极”而不是否定与!

No need to create separate function for check array length, you can directly use in isEmpty() function and get boolean value无需创建单独的函数来检查数组长度,您可以直接在isEmpty()函数中使用并获取布尔值

const isEmpty():boolean => {
    return arr1.length > 0 || arr2.length > 0
}

I think you can我想你可以

сonst isEmpty():boolean => {

    if (!hasValArr1() && !hasValArr2()) 
       return false
    
       return true;
}

or:或者:

const isEmpty():boolean => {
   return (!hasValArr1() && !hasValArr2())
}

or if you want to check whether the both arrays have values:或者如果你想检查两个数组是否有值:

const HasArraysData():boolean => {
   return (hasValArr1() && hasValArr2())
}

and it becomes simpler to read code:并且阅读代码变得更简单:

if (HasArraysData)

or:或者:

if (!HasArraysData)

Without questioning the premise of the question, you can write :不质疑问题的前提,你可以写:

const isEmpty():boolean => {
   return hasValArr1() || hasValArr2()
}

So as you write, only the first case you want to return false , other cases you want to return only true , that is why your codes will be like below所以在你写的时候,只有第一种情况你想返回 false ,其他情况你只想返回 true ,这就是为什么你的代码会像下面这样

const isEmpty():boolean => {常量 isEmpty():boolean => {

if (!hasValArr1() && !hasValArr2()) return false如果 (!hasValArr1() && !hasValArr2()) 返回 false

else return true否则返回真

}; };

If you want to proceed with the approach you mentioned, Then there are few suggestions or nice to have :如果您想继续使用您提到的方法,那么建议很少或很高兴:

  • Instead of return arr1.length > 0 you can simply use return arr1.length而不是return arr1.length > 0你可以简单地使用return arr1.length

  • No need to use multiple conditions as you are checking if both the arrays contains value or not.无需使用多个条件,因为您正在检查两个数组是否包含值。 Hence, you can simply modify your isEmpty function like this :因此,您可以像这样简单地修改您的isEmpty函数

     const isEmpty() : boolean => { return (!hasValArr1() && !hasValArr2()) }

My suggestion is instead of checking arr1 and arr2 length in the separate methods.我的建议是不要在单独的方法中检查 arr1 和 arr2 长度。 You can check that in isEmpty() method itself.您可以在isEmpty()方法本身中进行检查。

const isEmpty() : boolean => {
    return !arr1.length && !arr2.length
}
const isEmpty():boolean => {
    return hasValArr1() || hasValArr2()
}

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

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