Re: APIs that have boolean arguments defaulting to true

On Tue, 12 Nov 2013, Jonas Sicking wrote:
> On Tue, Nov 12, 2013 at 3:37 AM, Boris Zbarsky <bzbarsky@mit.edu> wrote:
> > 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.
> 
> Yup. Arguments defaulting to true has always seemed surprising to me. 
> WebIDL, default behavior for explicit undefined, etc aside, a missing 
> argument always looks falsy to me. The fact that it isn't in XHR.open 
> took me quite a while to get used to.

Agreed.

> I would probably go even further and say that having string arguments 
> that default to something other than the empty string, or numeric 
> arguments default to something other than 0, or objects that default to 
> something other than null, in cases where empty string/0/null is valid 
> values, seems like a bad idea to me.

That too.

However, lest we decide therefore to make such things invalid in WebIDL... 
there's lots of APIs that we've ended up with not because we think they're 
good, but because we were in a constrained situation and what we picked 
was the least bad solution. Even ignoring those cases, there's also the 
occasional case where it might make sense.

For example, here are strings that don't default to "" in HTML:

document.open()'s first argument defaults to "text/html".
setRangeText()'s last argument should be an enum (it predates them), and 
therefore is defined as a string that defaults to "preserve".
HitRegionOptions.cursor defaults to "inherit".
window.open()'s target argument defaults to "_blank".

Of those, two are legacy and one is fixable with enums, but the default 
for HitRegionOptions.cursor seems legit to me (the empty string throws, 
in fact, because it's not a valid CSS value).

-- 
Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

Received on Friday, 15 November 2013 22:31:31 UTC