简体   繁体   English

如何减少长布尔表达式的复杂度?

[英]How to reduce complexity of a long boolean expression?

I have a boolean expression to evaluate that is pretty long with a lot of different cases: 我有一个布尔表达式来评估它,在许多不同的情况下都相当长:

return (a && b && c) ||
(d && e && f) ||
...

This should return a boolean! 这应该返回一个布尔值!

a, b, c, d, e and f being simple equality comparison and being all different. a,b,c,d,e和f是简单的相等比较,并且都不同。 This continues for 12 lines and gives me a Cyclomatic Complexity of 44 这持续了12行,给我带来了44的圈复杂度

I tried to look at the Map object in order to reduce the complexity but did not find a way to do it. 我试图查看Map对象以降低复杂性,但没有找到一种方法。

How could I reduce the complexity of such an expression ? 我如何降低这种表达式的复杂性?

You can not reduce the complexity of the given expression. 您不能降低给定表达式的复杂度。

A slower approach, but maybe a little more structured, is to group the expressions and evaluate with Array#some for the outer array and Array#every for the inner arrays with a short circuit. 较慢的方法(但可能更结构化)是对表达式进行分组,并使用Array#some评估外部数组,使用Array#every评估内部数组,并进行短路。

return [[a, b, c], [d, e, f]].some(a => a.every(Boolean));

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

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