[whatwg/webidl] PaintOptions example should not have nullable optional fields (Issue #1204)

There's an example in §2.7 Dictionaries as follows:

```idl
dictionary PaintOptions {
  DOMString? fillPattern = "black";
  DOMString? strokePattern = null;
  Point position;
};
```

This example is giving bad design advice since both `fillPattern` and `strokePattern` are both nullable *and* optional. There's no need for them to be nullable.

* `fillPattern` has a default value, which means if omitted, it will be `"black"`, but it can also be explicitly set to `null`. It's not clear why you would design something that can be set to `null`, but with a non-null default value.
* `strokePattern` is a little more reasonable, having a default value of `null`, but this is still unusual. It's best practice to have it simply be optional with no default, so if omitted it will simply be missing from the dictionary, rather than having a `null` value.

Therefore I think it should be rewritten to avoid the nullability:

```idl
dictionary PaintOptions {
  DOMString fillPattern = "black";
  DOMString strokePattern;
  Point position;
};
```

(At a higher level, this feels like a strange design for a `drawRectangle` method, taking the "position" — presumably the top-left corner — in an optional `PaintOptions` dict, but the width and height as direct arguments. I might also suggest making both the `options` argument and the `position` member required, which helps demonstrate when `required` is a good choice. But it's not clear whether that's the intention behind the design.)

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/webidl/issues/1204

You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/webidl/issues/1204@github.com>

Received on Thursday, 29 September 2022 06:43:03 UTC