当您使用一堆链接的三元运算符编写代码时,它会变得更简洁,通常可读性更低。
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;
}());
这可以简化。检查!dotted后,dotted一定为真,成为多余:
true && a
转换为:
a
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 (all)
return true;
if (implied && almost)
return true;
if (directory && isDot)
return true;
return false;
}());
为了保持足够好,您可以随时在此处停止,因为您知道代码简单且高效。
当然……这可以简化。最后一个if 语句可以更改为return:
if (a)
return true;
return false;
转换为:
return a;
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 (all)
return true;
if (implied && almost)
return true;
return directory && isDot;
}());
这当然可以通过将最后一个 if 再次转换为 return 来简化:
if (a)
return true;
return b;
转换为:
return 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;
if (all)
return true;
return (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);
}());
...再一次:
if (a)
return false;
return b;
转换为:
return !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;
return !(directory && !isDot) && (
(!dotted) ||
(all) ||
(implied && almost) ||
(directory && isDot)
);
}());
这可以通过使用De Morgan's laws进一步简化:
!(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;
return (!directory || isDot) && (
(!dotted) ||
(all) ||
(implied && almost) ||
(directory && isDot)
);
}());
你有它,就像逻辑可以得到的一样简单。当然,您可以选择将变量扩展回其原始定义,但我建议您不要这样做。我实际上鼓励您不要简化if..return 语句的简单链。
如果您使代码更简洁,则阅读和理解更具挑战性,这使得调试更具挑战性。很有可能我在“简化”代码时在这篇文章的某个地方犯了一个错误,并且在阅读&& 和|| 运算符系列时,如果犯了错误,这并不是很明显。