Say What You Mean (in JavaScript)
iterative.ly
iterative.ly
It's scalable. What if you have to test a third expression? Surely this:
foo() && bar() && buzz()
is more readable than
if(foo()) if(bar()) buzz()
It's flexible. Since an "if" statement is not an expression in most languages, you have to be careful where you place it. For example, what if you wanted to pass the result to a function?
qux(foo() && bar() && buzz());
Javascript (and a few other languages) have the neat feature with boolean operators that they return the result of the last tested expression. So the above example would pass the result of buzz() to qux if all statements are true, otherwise it would pass false. Here is the same code with "if" statements:
var res = false;
if(foo()) if(bar()) res = buzz();
qux(res);
There are many more examples of why you should be used to constructing expressions with boolean operators. It doesn't matter much for the simplistic example in the article, but the same technique allows you do write more complex code in succinct ways.
In all of jlongster's examples, the result of the '&&' operator is being used. So, someone reading the code can mentally consider the '&&'s to be logical operators.
However, in the article's examples, the result was not being used at all. So, the '&&' was being used not as a 'logical operator' but as a 'conditional operator', which is a non-intuitive use.
Also, '&&' and '||' are not Boolean operators -- they are logical operators, where order matters for short-circuiting.
function f(p1, p2, p3){ p2 || ( p2 = "default); }
and i have a buddy that likes to do "poor man's unless":
if(something && other_thing || third_thing); else{ }
which helps you from having to figure out the logical inversion or have the ! to track down, but is a little weird
However, the way it's written in the article, the intent is to merely test foo() and then execute bar() if foo() returns true. Even though I'm familiar with &&, it took me a minute to understand this as well. I'd be eqaully annoyed seeing this in production code.
foo() or die();
idiom to be quite readable. And if you're familiar with that, this is a pretty straightforward extension.
(if (foo) (bar))
equals to (and (foo) (bar))The main flaw of the technique is that the author uses the '&&' not as a logical operator (the intuitive use) but as a conditional operator.
'&&' is the 'Logical AND'. The statement "foo() && bar();" just doesn't make initial sense, because the logical result of the expression is thrown out.
After thinking this, the programmer probably will realize that the functions are actually called to evaluate the expression. And then finally (hopefully) the programmer realizes that they are evaluated short-circuit.
So, the code may be functional but it is not very readable.
Actually, the result is not thrown out: the && operator produces the value of its first operand if the first operand is falsy, otherwise it produces the value of the second operand.
For what it's worth, there's also a procedural reading of "foo() && bar()" along the lines of "fill out this form AND win a prize!".
I've used it for so long that it's come to mean "if the first exists, then do the second", and not just "this expression is true if both are true", and I don't really confuse the two.
If
function (x) {
x = x || someDefaultValue;
for setting non-falsy default argument values isn't a JavaScript idiom, I don't know what is.with x || (x = someDefaultValue), what you read is how it is evaluated. if x is true, the rhs is never evaluated. with the above, the assignment happens either way.
As to "pushing some strange variant": Guilty as charged. Have you tried CoffeeScript? It's fantastic. The code you gave above would be written as
(x) ->
x ||= someDefaultValueNope. I'm apprehensive about adding another layer to the development process when I know JS very well and don't find it at all limiting. So I'm probably not part of your target audience.
if(foo()) bar();
No {} necessary if it's just a single command.But I'm making a larger point: JavaScript tends to be a lot less readable than code written in other languages not just because of the language itself but because of the byte-squeezing habits web developers have picked up over the years. I think JavaScript tools have advanced to the point that we can finally drop those habits.
In the first case, maintenance programmers will thank you. In the second, users processing millions of records will thank you.