timoxley
"You locked out? Hang on a moment"
Goes into a Subway, comes back with a coat hanger.
Less than a minute later the car is unlocked.
"Wow you're pretty good at that"
"I literally just got out of prison today... for car theft."
Laughter all around
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/e...
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.
function(err, results) {
if (err) // …
}
It is assumed that this function will be executed asynchronously, and thus it's not possible for anything to consume the return value anyway.If you want to control the return value, simply return on a new line or use the void operator:
return void handleError(err)I often find inelegant, yet straightforward solutions are generally better options than dense, mathematically pure solutions, simply because inelegant code usually relies on fewer assumptions. Noting that code rarely/never evolves the way we expect it to, apply Occam's razor and strike a balance.
For the purposes of this post, it is assumed that any errors produced in those calls will automatically bubble up the stack to some caller or to the top level. In my experience, most JS code is not written with fine-grained error checking around every expression, if there's any explicit error handling at all. The example code isn't intentionally omitting any mess, rather it appears to me to be fairly typical.
The "handing" at the top is mainly for checking function preconditions. e.g. "does it even make sense to proceed?". Early returns help to decouple precondition checking from the important logic of the function, which IMO makes these types of checks easier to write and maintain and thus more likely to exist.
Agreed on early returns being questionable for "two variations of the same concept" though, these days I generally use if/else for situations like that.
> ...making the code look less complicated than it actually is...
> I'd rather emphasize the underlying declarative intent, the state machines, and pre/post-conditions.
These statements are strange to me as I personally see early returns as attempts as achieving exactly these goals. I guess people unroll logic in their head in different ways. "flat" to one may appear "nested" to another and vice versa.
if (err) return void handleError(err)
And in non-promise-based async JS, the return value is almost always lost/useless anyway so `return x` has no effect, might as well repurpose `return` for short-circuiting.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