W3C home > Mailing lists > Public > public-script-coord@w3.org > October to December 2013

APIs that have boolean arguments defaulting to true

From: Boris Zbarsky <bzbarsky@MIT.EDU>
Date: Mon, 11 Nov 2013 14:37:55 -0500
Message-ID: <52813213.6050700@mit.edu>
To: "public-script-coord@w3.org" <public-script-coord@w3.org>
Dear all,

One take-away I have from the current attempts to make undefined be 
treated as missing is that we should avoid creating APIs that have 
arguments of the form "optional boolean arg = true".

The reason for that is that I think the behavior of such an argument 
when combined with the conventions for undefined is very surprising. 
Consider a function declared as:

   void foo(optional boolean arg = foo);

and these two patterns of calling it:

   if (myValue) {
     foo(true);
   } else {
     foo(false);
   }

and

   foo(myValue);

These two patterns have different behavior, which seems very unintuitive 
to me and to everyone else I've pointed this out to...

This does mean that we can't have "foo()" with no arguments default to 
true, so we should probably design APIs such that "false" gives the sane 
default behavior if there's an optional boolean argument at all.

Thoughts?

-Boris
Received on Monday, 11 November 2013 19:38:29 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 21:37:51 UTC