6 ms·
JavaScript has the guard operator. It can really clean up your control flow when you know it's a guard operator and not just logical AND.
by Waterluvian 9y ago
JavaScript has the guard operator. It can really clean up your control flow when you know it's a guard operator and not just logical AND.
- timoxley 9y agoA big gotcha with `&&` as a guard in JS is that it can return any falsey typed value if you don't coerce the guard to a boolean. e.g. const check(cond) => cond && otherValue If `cond` is falsey, it returns `cond`. This means the function could return any of: `false`, `undefined`, `null`, '', `0` or `NaN`. This is a fairly common issue I see with React + JSX: <div>{cond && <SomeComponent />}</div> If `cond` is `false`, `null`, `undefined` or '', everything will be just fine, but if `cond` happens to be a zero or `NaN`, suddenly you have a weird `0` or `NaN` rendering in your page. Oops. Low cost, much safer guard: const check(cond) => !!cond && otherValue
- Waterluvian 9y agoGood point. Good example. I imagine it works well in my cases but I haven't come across cases like your example where it breaks down.
- acjohnson55 9y ago...until you do. That's the problem with antipatterns. It's impossible to remain alert to the edge cases, and then they eventually bite you in production. Better to lint them away. But it's difficult to convince people not to do something really convenient if they haven't gotten bitten yet.
- sgustard 9y agoYes, back when CoffeeScript was in vogue the ? operator handled this case nicely: cond? && otherValue
- sergeykish 9y agoQuite interesting. For a moment I've thought it is another JS issue but no. It's React who's strange https://github.com/facebook/react/blob/v0.11.2/src/browser/ui/ReactDOMComponent.js#L184 https://github.com/facebook/react/blob/v0.11.2/src/browser/u... https://github.com/facebook/react/blob/v0.11.2/src/utils/traverseAllChildren.js#L126 https://github.com/facebook/react/blob/v0.11.2/src/utils/tra... Same behavior on Ruby: def content_markup(children) case children when String, Numeric children when NilClass, TrueClass, FalseClass return else # ... end end content_markup 'foo' #=> "foo" content_markup 0 #=> 0 content_markup true #=> nil content_markup false #=> nil content_markup nil #=> nil
- arghwhat 9y agoLogical AND in JavaScript can be used as a guard, by why in the world would you think of it as anything other than a logical AND? If you start thinking it is a "guard operator", I would think that going down the wrong path, which might promote misunderstanding of the behavior.
- timoxley 9y ago> which might promote misunderstanding of the behavior This isn't a misunderstanding, binary logical operators in JS short-circuit like this by design. I believe && and || returned a boolean value in the past, but were explicitly changed to support this behaviour.
- arghwhat 9y agoQuoting ECMA-262 First Edition (June 1997), Chapter 11, Section 11 (Binary logical operators)[1]: The production LogicalANDExpression : LogicalANDExpression && BitwiseORExpression is evaluated as follows: 1. Evaluate LogicalANDExpression. 2. Call GetValue(Result(1)). 3. Call ToBoolean(Result(2)). 4. If Result(3) is false, return Result(2). 5. Evaluate BitwiseORExpression. 6. Call GetValue((Result(5)). 7. Return Result(6). That is, the behavior has always been "If the first expression is false-ish, return the first expression, otherwise return the second expression" ("BitwiseORExpression" is a class of expressions that include a lot of things, including equality operators). JavaScript does not have any operators that is not explicitly listed in a version of ECMA-262. It would be correct to refer to the construct as a "guard", but incorrect to refer to it as a "guard operator". Calling it "guard operator" also does not promote an understanding of the underlying construct. 1: https://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%201st%20edition,%20June%201997.pdf https://www.ecma-international.org/publications/files/ECMA-S...
- timoxley 9y ago> the behavior has always been The ECMA spec only defines Javascript 1.3 and above. See this description for logical operators for Javascript 1.1: https://web.archive.org/web/20060318153542/wp.netscape.com/eng/mozilla/3.0/handbook/javascript/index.html https://web.archive.org/web/20060318153542/wp.netscape.com/e...