AND and OR in if statement
No, please don't do this. While I understand why you want this, I feel like this is unnecessary and will just introduce yet another way to write inconsistent and confusing JavaScript code 😕.
- Jonathan
—
Life is a game and we’re all just high density pixels.
On May 24 2016, at 9:15 pm, Francis Clavette <clavette.francis at gmail.com> wrote:
Hi,
I’d like to be able to use AND for && and OR for || in conditional
statements in a future version of ECMAScript. It’s a feature I’ve always been wanting since transitioning from PHP. It’s also much cleaner :
if ($scope.newSourceModal.isShown() && $scope.newsource.type == "book" && (!$scope.insertingFromScan || $scope.ok))
==============
if ($scope.newSourceModal.isShown() AND $scope.newsource.type == "book" AND (!$scope.insertingFromScan OR $scope.ok))
Could you explain how your second example is cleaner? All I see is that it's longer; the rest is merely a matter of taste.
JavaScript's syntax was, amongst others, heavily influenced by C-family languages (which went for &&
and ||
as well as &
and |
).
Next, I think it's confusing that, although we're calling it "bitwise and" and "bitwise or", we would only be able to write and
or or
in a logical context; this choice is entirely arbitrary. Bitwise operators would still need to be &
and |
since there's no way to tell what your intent is just from the syntax.
IMHO, the current operators &&
, &
, ||
,|
are the cleanest implementation of and
and or
that I could think of.
It's not feasible anyway because AND and OR are already valid identifiers:
a = foo() &&(bar)
is equivalent to a = foo() && bar
, but
a = foo() AND(bar)
is equivalent to
a = foo(); AND(bar);
Le 25 mai 2016 à 03:29, kdex <kdex at kdex.de> a écrit :
IMHO, the current operators
&&
,&
,||
,|
are the cleanest implementation ofand
andor
that I could think of.
I don’t think so: It is not ideal that the longest forms, &&
and ||
, are by far the most current ones. But this is history that we cannot change.
Having to sets of logical operators, &&
/||
versus and
/or
, with different precedence, is very useful in idiomatic Perl, where they are typically used to express control-flow. But much less so in idiomatic JS.
This is a non-starter, as AND and OR are already valid identifiers.
Devils advocate: if you were to measure usage of identifiers named “and” or “or”, on the web, how substantial do you think their use would be? And even if they were used a fair bit, that hasn’t really stopped the introduction of other contextual keywords in the past.
If you really wanted to, you could find a way to make this work. But I’m not saying we need to turn JS into Python or Perl.
I’d like to be able to use AND for && and OR for || in conditional statements in a future version of ECMAScript. It’s a feature I’ve always been wanting since transitioning from PHP. It’s also much cleaner :
if ($scope.newSourceModal.isShown() && $scope.newsource.type == "book" && (!$scope.insertingFromScan || $scope.ok))
==============
if ($scope.newSourceModal.isShown() AND $scope.newsource.type == "book" AND (!$scope.insertingFromScan OR $scope.ok))
Francis