Consistency in The Negative Result Values Through Expansion of null's Role

Andreas Rossberg rossberg at
Fri Aug 17 07:03:48 PDT 2012

On 16 August 2012 23:47, Brendan Eich <brendan at> wrote:
> Andreas Rossberg wrote:
>> On 16 August 2012 00:35, Rick Waldron<waldron.rick at>  wrote:
>>> It would be far worse to have a different type of value as a return,
>>> right?
>> Actually, no. It is far better to have something that produces failure
>> as immediately and as reliably as possible when used improperly.
>> Sentinel values are a prominent anti-pattern.
> Spoken like a true ML'er :-P.

Well, in the pre-ML world I've been brought up in, it was still
considered good software engineering practice, independent of
language, static or dynamic (and ML has its share of violations, too
;) ).

> -1 as an out-of-band (for non-negative indexes) return code is actually
> easier to test

Why is it easier to compare against constants -1, NaN, or "" than to,
say, null or undefined? And if so, wouldn't that be something that
might be worth correcting? Question mark operator, anyone?

  let index  = s.find(...)
  if (?index) {   // equivalent to index !== undefined
  } else {
    // err...

> and works well with certain code patterns, compared to a
> differently typed code. JS is not alone here, tons of precedent even in
> other dynamic languages that do have try/catch and even matching.

There is a lot of precedence in other languages and systems, too (esp
of C heritage). At the same time, it also seems to be folklore that it
is not the best way to approach API design. And all things equal,
wouldn't we want JS to be better than other languages? :)

Ultimately, it boils down to careful API design. For some operations
there is a useful neutral value to return in edge cases. For others,
there isn't.

> The issue is "failure". Sometimes not finding the wanted character index is
> not failure but just a condition to test that leads to an alternative
> strategem.

Yes, but it better be failure to forget the test! Returning undefined
ensures that relatively reliably, returning a sentinel index doesn't.

> Then the conciseness and clarity of the test matters, and typeof
> rv != "number"is the wrong tool compared to rv < 0.

I agree that typeof is the wrong tool, but rv !== undefined seems
reasonable to me.


More information about the es-discuss mailing list