Javascript 如何避免复杂布尔表达式上的三值化

Javascript 如何避免复杂布尔表达式上的三值化,javascript,boolean,ternary-operator,Javascript,Boolean,Ternary Operator,在linting我的Javascript时,我在我的一个复杂的三元选项上遇到了一个没有不必要的三元警告 我知道如何在简单的布尔表达式上解决这个问题: var obvious = (1 === 1) ? true : false; // can simply become: var obvious = (1 === 1); 然而,在下面的布尔表达式中,我不知道如何适当缩小这个范围,而不必担心打破一些非常复杂的东西: const include = (options.directory &am

在linting我的Javascript时,我在我的一个复杂的三元选项上遇到了一个
没有不必要的三元
警告

我知道如何在简单的布尔表达式上解决这个问题:

var obvious = (1 === 1) ? true : false;
// can simply become:
var obvious = (1 === 1);
然而,在下面的布尔表达式中,我不知道如何适当缩小这个范围,而不必担心打破一些非常复杂的东西:

const include =
  (options.directory && file !== '.') ? false :
  (!dotted) ? true :
  (dotted && options.all) ? true :
  (dotted && !implied && options.almostall) ? true :
  (options.directory && file === '.') ? true :
  false;
什么是正确的速记实现


试一试:
这是否正确?

当您使用一组链式三元运算符编写代码时,代码会变得更简洁,通常可读性较差

const include =
  (options.directory && file !== '.') ? false :
  (!dotted) ? true :
  (dotted && options.all) ? true :
  (dotted && !implied && options.almostall) ? true :
  (options.directory && file === '.') ? true :
  false;
要对此进行分解,我将首先使用模块模式展开它:

include = (function () {
    //set up some simple names for concepts:
    var directory = options.directory;
    var isDot = file === '.';
    var all = options.all;
    var almost = options.almostall;

    if (directory && !isDot)
        return false;

    if (!dotted)
        return true;

    if (dotted && all)
        return true;

    if (dotted && implied && almost)
        return true;

    if (directory && isDot)
        return true;

    return false;
}());
这可以简化。检查
后!虚线
虚线
必须为真,并成为冗余:

转换为:

a
return a;
return a || b;
return !a && b;
!a || !b
作为一件足够好的事情,您可以随意停在这里,知道代码是简单和高效的


当然……这可以简化。最后一个
if
语句可以更改为
返回值

转换为:

a
return a;
return a || b;
return !a && b;
!a || !b
当然,可以通过将最后一个
if
转换为
return
来简化:

转换为:

a
return a;
return a || b;
return !a && b;
!a || !b
……而且:

include = (function () {
    //set up some simple names for concepts:
    var directory = options.directory;
    var isDot = file === '.';
    var all = options.all;
    var almost = options.almostall;

    if (directory && !isDot)
        return false;

    if (!dotted)
        return true;

    return (all) ||
        (implied && almost) ||
        (directory && isDot);
}());
include = (function () {
    //set up some simple names for concepts:
    var directory = options.directory;
    var isDot = file === '.';
    var all = options.all;
    var almost = options.almostall;

    if (directory && !isDot)
        return false;

    return (!dotted) ||
        (all) ||
        (implied && almost) ||
        (directory && isDot);
}());
……而且:

include = (function () {
    //set up some simple names for concepts:
    var directory = options.directory;
    var isDot = file === '.';
    var all = options.all;
    var almost = options.almostall;

    if (directory && !isDot)
        return false;

    if (!dotted)
        return true;

    return (all) ||
        (implied && almost) ||
        (directory && isDot);
}());
include = (function () {
    //set up some simple names for concepts:
    var directory = options.directory;
    var isDot = file === '.';
    var all = options.all;
    var almost = options.almostall;

    if (directory && !isDot)
        return false;

    return (!dotted) ||
        (all) ||
        (implied && almost) ||
        (directory && isDot);
}());
……而且:

include = (function () {
    //set up some simple names for concepts:
    var directory = options.directory;
    var isDot = file === '.';
    var all = options.all;
    var almost = options.almostall;

    if (directory && !isDot)
        return false;

    if (!dotted)
        return true;

    return (all) ||
        (implied && almost) ||
        (directory && isDot);
}());
include = (function () {
    //set up some simple names for concepts:
    var directory = options.directory;
    var isDot = file === '.';
    var all = options.all;
    var almost = options.almostall;

    if (directory && !isDot)
        return false;

    return (!dotted) ||
        (all) ||
        (implied && almost) ||
        (directory && isDot);
}());
转换为:

a
return a;
return a || b;
return !a && b;
!a || !b
这可以通过使用以下方法进一步简化:

转换为:

a
return a;
return a || b;
return !a && b;
!a || !b

这就是你所能得到的,尽可能简单的逻辑。当然,您可以选择将变量扩展回其原始定义,但我建议您不要这样做。事实上,我鼓励您不要在
if..return
语句的简单链之外进行简化


如果您使代码更简洁,那么阅读和理解就更具挑战性,这使得调试更具挑战性。很可能我在这篇文章的某个地方“简化”代码时犯了一个错误,在阅读
&&
|
操作符系列时,如果出现错误,这一点并不明显。

当您使用一组链式三元操作符编写代码时,它会变得更加简洁,通常可读性较差

const include =
  (options.directory && file !== '.') ? false :
  (!dotted) ? true :
  (dotted && options.all) ? true :
  (dotted && !implied && options.almostall) ? true :
  (options.directory && file === '.') ? true :
  false;
要对此进行分解,我将首先使用模块模式展开它:

include = (function () {
    //set up some simple names for concepts:
    var directory = options.directory;
    var isDot = file === '.';
    var all = options.all;
    var almost = options.almostall;

    if (directory && !isDot)
        return false;

    if (!dotted)
        return true;

    if (dotted && all)
        return true;

    if (dotted && implied && almost)
        return true;

    if (directory && isDot)
        return true;

    return false;
}());
这可以简化。检查
后!虚线
虚线
必须为真,并成为冗余:

转换为:

a
return a;
return a || b;
return !a && b;
!a || !b
作为一件足够好的事情,您可以随意停在这里,知道代码是简单和高效的


当然……这可以简化。最后一个
if
语句可以更改为
返回值

转换为:

a
return a;
return a || b;
return !a && b;
!a || !b
当然,可以通过将最后一个
if
转换为
return
来简化:

转换为:

a
return a;
return a || b;
return !a && b;
!a || !b
……而且:

include = (function () {
    //set up some simple names for concepts:
    var directory = options.directory;
    var isDot = file === '.';
    var all = options.all;
    var almost = options.almostall;

    if (directory && !isDot)
        return false;

    if (!dotted)
        return true;

    return (all) ||
        (implied && almost) ||
        (directory && isDot);
}());
include = (function () {
    //set up some simple names for concepts:
    var directory = options.directory;
    var isDot = file === '.';
    var all = options.all;
    var almost = options.almostall;

    if (directory && !isDot)
        return false;

    return (!dotted) ||
        (all) ||
        (implied && almost) ||
        (directory && isDot);
}());
……而且:

include = (function () {
    //set up some simple names for concepts:
    var directory = options.directory;
    var isDot = file === '.';
    var all = options.all;
    var almost = options.almostall;

    if (directory && !isDot)
        return false;

    if (!dotted)
        return true;

    return (all) ||
        (implied && almost) ||
        (directory && isDot);
}());
include = (function () {
    //set up some simple names for concepts:
    var directory = options.directory;
    var isDot = file === '.';
    var all = options.all;
    var almost = options.almostall;

    if (directory && !isDot)
        return false;

    return (!dotted) ||
        (all) ||
        (implied && almost) ||
        (directory && isDot);
}());
……而且:

include = (function () {
    //set up some simple names for concepts:
    var directory = options.directory;
    var isDot = file === '.';
    var all = options.all;
    var almost = options.almostall;

    if (directory && !isDot)
        return false;

    if (!dotted)
        return true;

    return (all) ||
        (implied && almost) ||
        (directory && isDot);
}());
include = (function () {
    //set up some simple names for concepts:
    var directory = options.directory;
    var isDot = file === '.';
    var all = options.all;
    var almost = options.almostall;

    if (directory && !isDot)
        return false;

    return (!dotted) ||
        (all) ||
        (implied && almost) ||
        (directory && isDot);
}());
转换为:

a
return a;
return a || b;
return !a && b;
!a || !b
这可以通过使用以下方法进一步简化:

转换为:

a
return a;
return a || b;
return !a && b;
!a || !b

这就是你所能得到的,尽可能简单的逻辑。当然,您可以选择将变量扩展回其原始定义,但我建议您不要这样做。事实上,我鼓励您不要在
if..return
语句的简单链之外进行简化


如果您使代码更简洁,那么阅读和理解就更具挑战性,这使得调试更具挑战性。很可能我在这篇文章的某个地方“简化”代码时犯了一个错误,在阅读
&&
|
操作符系列时,如果出现了错误,这一点并不明显。

在我看来——尽管有时为复杂的条件提出一行代码看起来确实令人印象深刻,像这样的模式是不可读的,代码也不是很可继承的。我同意优化等等。。但是在我看来,帮助函数中的
if..else
在这种情况下保持可读性并没有什么坏处——尽管有时为复杂条件提供一行代码看起来确实令人印象深刻,但像这样的模式是不可读的,代码也不是很可继承的。我同意优化等等。。但在本例中,助手函数中的if..else不会影响保持可读性