javascript - How to avoid ternary on complicated boolean expression -
in linting javascript, ran across no-unneeded-ternary
warning on complex ternary option of mine.
i know how solve on simple boolean expressions:
var obvious = (1 === 1) ? true : false; // can become: var obvious = (1 === 1);
however, on below boolean expression, don't know how narrow down without fear of breaking happens complicated:
const include = (options.directory && file !== '.') ? false : (!dotted) ? true : (dotted && options.all) ? true : (dotted && !implied && options.almostall) ? true : (options.directory && file === '.') ? true : false;
what proper shorthand implementation of this?
taking stab @ it:
const include = !(options.directory && file !== '.') || (!dotted) || (dotted && options.all) || (dotted && !implied && options.almostall) || (options.directory && file === '.');
is correct?
when write code using bunch of chained ternary operators, becomes more terse , typically less readable.
const include = (options.directory && file !== '.') ? false : (!dotted) ? true : (dotted && options.all) ? true : (dotted && !implied && options.almostall) ? true : (options.directory && file === '.') ? true : false;
to break down, first expand using module pattern:
include = (function () { //set simple names concepts: var directory = options.directory; var isdot = file === '.'; var = options.all; var = 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; }());
this can simplified. after checking !dotted
, dotted
must true, , becomes redundant:
true &&
converts to:
a
include = (function () { //set simple names concepts: var directory = options.directory; var isdot = file === '.'; var = options.all; var = 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; }());
as matter of leaving enough alone, feel free stop here knowing code simple , efficient.
of course...this can simplified. last if
statement can changed return
:
if (a) return true; return false;
converts to:
return a;
include = (function () { //set simple names concepts: var directory = options.directory; var isdot = file === '.'; var = options.all; var = options.almostall; if (directory && !isdot) return false; if (!dotted) return true; if (all) return true; if (implied && almost) return true; return directory && isdot; }());
which of course can simplified converting last if
return
again:
if (a) return true; return b;
converts to:
return || b;
include = (function () { //set simple names concepts: var directory = options.directory; var isdot = file === '.'; var = options.all; var = options.almostall; if (directory && !isdot) return false; if (!dotted) return true; if (all) return true; return (implied && almost) || (directory && isdot); }());
...and again:
include = (function () { //set simple names concepts: var directory = options.directory; var isdot = file === '.'; var = options.all; var = options.almostall; if (directory && !isdot) return false; if (!dotted) return true; return (all) || (implied && almost) || (directory && isdot); }());
...and again:
include = (function () { //set simple names concepts: var directory = options.directory; var isdot = file === '.'; var = options.all; var = options.almostall; if (directory && !isdot) return false; return (!dotted) || (all) || (implied && almost) || (directory && isdot); }());
...and again:
if (a) return false; return b;
converts to:
return !a && b;
include = (function () { //set simple names concepts: var directory = options.directory; var isdot = file === '.'; var = options.all; var = options.almostall; return !(directory && !isdot) && ( (!dotted) || (all) || (implied && almost) || (directory && isdot) ); }());
this can simplified further using de morgan's laws:
!(a && b)
converts to:
!a || !b
include = (function () { //set simple names concepts: var directory = options.directory; var isdot = file === '.'; var = options.all; var = options.almostall; return (!directory || isdot) && ( (!dotted) || (all) || (implied && almost) || (directory && isdot) ); }());
and there have it, simple logic can get. could, of course, choose expand variables original definition, encourage not to. encourage not simplify beyond simple chain of if..return
statements.
if make code terser it's more challenging read , understand makes more challenging debug. it's quite i've made mistake somewhere in post while "simplifying" code, , it's not obvious when reading series of &&
, ||
operators if mistakes made.
Comments
Post a Comment