-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New principle: Naming of factory methods #378
Comments
For Temporal we took our |
IMO there are several categories of things here:
And then the async variants:
Guidance here should probably be integrated with https://w3ctag.github.io/design-principles/#constructors . |
In addition to the categories that @domenic mentioned, Ecma262 JavaScript also has some “coercion” functions that are constructors being called as if they were ordinary functions, without The iterator-helpers proposal also uses this pattern: |
Should it not be forbidden to use the same name of an instance method ( Take for example the MDN Response document. They just omit the |
MDN is working on fixing their unfortunate URL-scheme choices from many years ago, and such unfortunate choices should not constrain web platform API design. |
Agreed with @domenic. Also, naming static and instance methods the same can actually be a very good practice in certain cases where a method does the same thing, the instance one operating on the instance itself, and the static one on the first parameter (though I can't think of an example of that off the top of my head). |
The original PR (#471) included this sentence:
I think this is actually an antipattern that we should actively discourage. This should have been
Furthermore, the way this is phrased is inaccurate: for DOM elements the parent object is OTOH, things like |
* Add guidance for factory method naming Resolves #378. * Update index.bs Co-authored-by: Amy Guy <[email protected]> * Update index.bs Co-authored-by: Amy Guy <[email protected]> * Update index.bs * Update index.bs Co-authored-by: Amy Guy <[email protected]> * Update index.bs Co-authored-by: Amy Guy <[email protected]> * Update index.bs Co-authored-by: Lea Verou <[email protected]> * Update index.bs Co-authored-by: Sangwhan "fish" Moon <[email protected]> * Update index.bs * Update index.bs * Update index.bs --------- Co-authored-by: Amy Guy <[email protected]> Co-authored-by: Lea Verou <[email protected]>
This was brought up in our discussion of
Response.json()
in w3ctag/design-reviews#741We should have guidelines for naming factory methods. Currently the web platform is pretty inconsistent:
document.createXXX()
for creating nodes, alsodocument.createRange()
,document.createTreeWalker()
etcdocument.initXXXEvent()
factory methodsfrom()
methodscreateImageBitmap()
which is oddly placed in the global scope (?!)Object.create()
CanvasRenderingContext2D#createXXX()
Array.from()
Object.fromEntries()
String.fromCharCode()
audioContext.createXYZ()
Response.json()
DOMQuad.fromRect()
Perhaps we should have guidance that factory methods should start with
create
orfrom
to make it obvious that they are factory methods? Not sure we should restrict it further, as these are not interchangeable.Though there are also things like
Date.parse()
, which are fine as-is…The text was updated successfully, but these errors were encountered: