undefined being treated as a missing optional argument

Tab Atkins Jr. jackalmage at gmail.com
Fri Apr 13 12:15:24 PDT 2012


On Fri, Apr 13, 2012 at 12:00 PM, David Herman <dherman at mozilla.com> wrote:
> On Apr 13, 2012, at 11:51 AM, Allen Wirfs-Brock wrote:
>> both sides of this debate seem a little smelly. But, Pretending that undefined isn't a real value seems more smelly to me.
>
> There's no need to think of it as pretending it's not a real value. Think of it as saying that the undefined value is the idiomatic way in JS to represent "no value for the expected type." The smelly thing then is to create API's that both use defaults and accept undefined as a valid input.
>
> Put differently: defaulting based on whether the argument happened to be provided is reporting on what the syntax of the call looked like. But an API doesn't and shouldn't care about the syntax of the call. It just wants a protocol whereby the caller can indicate whether or not they have a value for a given argument. The undefined value is an idiomatic way to do that in JS, and it composes well with other expression forms that already produce undefined when they don't have a value. That puts some burden on programmers not to treat undefined as a normal value, but that's already a cost programmers live with in JS. And that cost is outweighed by the benefit of a more expressive call syntax.

I'm with Dave.

Even in Lisp, where you can ask for explicit indicator of whether a
particular optional or keyword argument was passed or not, delegation
is very annoying because of this problem.  You end up having to add
conditional code to call the same function with slightly different
signatures, just like several people have provided in this thread
already.

You really do need an explicit "I'm not really passing this value"
value, even if it seems smelly or overly meta.  Using undefined for
this is the right thing to do, given existing JS semantics, and the
fact that JS also has null which is approximately the same thing for
most semantic purposes.

~TJ


More information about the es-discuss mailing list