I'm in agreement with those opposing this idiom. The Javascript phrase:
obj && obj.prop && obj.prop.send();
is an expression, and (to me at least) should be pure, like a pure function. As used above, this expression is used only for its side effect. I would always prefer to be explicit. I like the proposed optional chaining; but until that's available, write this statement in English:
That’s the really interesting thing about JS or programming in general. There are always multiple ways of solving things. Some of them are definitely a syntactic choice. I’m always in the camp of expanding my toolkit so when I run across it (especially from others code), I know what’s going on. But at the end of the day, you’re the owner of your code, so pick the way that makes the most sense to you 😊
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Why would one prefer the
&&
operator over a conditional? At first glance, the later seems 'better' in many ways:if (x.y !== undefined)
x.y &&
will result in undefined ifx.y
isundefined
, rather than entering anelse
clauseIt's a one-liner, sure, but I would argue ternary operators are a better fit:
x.y ? f(y) : g()
over(x.y && f(y)) || g()
.Not saying you're wrong, but I'm curious about the reasoning.
I'm in agreement with those opposing this idiom. The Javascript phrase:
obj && obj.prop && obj.prop.send();
is an expression, and (to me at least) should be pure, like a pure function. As used above, this expression is used only for its side effect. I would always prefer to be explicit. I like the proposed optional chaining; but until that's available, write this statement in English:
// pedantic
if (obj && obj.prop && typeof obj.prop.send === 'function') {
obj.prop.send();
}
That’s the really interesting thing about JS or programming in general. There are always multiple ways of solving things. Some of them are definitely a syntactic choice. I’m always in the camp of expanding my toolkit so when I run across it (especially from others code), I know what’s going on. But at the end of the day, you’re the owner of your code, so pick the way that makes the most sense to you 😊