|
7 | 7 | <link href="https://www.w3.org/StyleSheets/TR/2021/W3C-ED" rel="stylesheet">
|
8 | 8 | <meta content="Bikeshed version e20e08949, updated Wed Jan 24 11:50:28 2024 -0800" name="generator">
|
9 | 9 | <link href="https://www.w3.org/TR/design-principles/" rel="canonical">
|
10 |
| - <meta content="a46392a0f25e103d30e02c596c9b471d1307765f" name="revision"> |
| 10 | + <meta content="c28ea6474f7b4b7a9debea583ccd23b59a3ed668" name="revision"> |
11 | 11 | <style>
|
12 | 12 | table.data {
|
13 | 13 | text-align: left;
|
@@ -3611,6 +3611,29 @@ <h4 class="no-num no-toc heading settled" id="casing-rules"><span class="content
|
3611 | 3611 | examples that violate the above rules are <code class="idl"><a data-link-type="idl" href="https://xhr.spec.whatwg.org/#xmlhttprequest" id="ref-for-xmlhttprequest①">XMLHttpRequest</a></code> and <code class="idl"><a data-link-type="idl" href="https://html.spec.whatwg.org/multipage/semantics.html#htmlhtmlelement" id="ref-for-htmlhtmlelement">HTMLHtmlElement</a></code>. Don’t follow their example; instead always capitalize your
|
3612 | 3612 | initialisms, even if they are repeated.</p>
|
3613 | 3613 | </div>
|
| 3614 | + <h4 class="no-num no-toc heading settled" id="factory-naming"><span class="content">Start factory method names with <code>create</code> or <code>from</code></span><a class="self-link" href="#factory-naming"></a></h4> |
| 3615 | + <p>Factory method names should start with <code>create</code> or <code>from</code>, optionally followed by a more specific noun.</p> |
| 3616 | + <p>If a factory method constructs a new empty object, |
| 3617 | +prefix the method name with <code>create</code>. |
| 3618 | +However, if your factory method creates an object from existing data, |
| 3619 | +prefix the method name with <code>from</code>.</p> |
| 3620 | + <p>Factory methods should be an exception, not the norm, and only used <a href="#constructors">for valid reasons</a>. |
| 3621 | +An example of valid usage of a factory method is |
| 3622 | +when an object is being created also requires association |
| 3623 | +with the parent object |
| 3624 | +(e.g. <code>document.createXXX()</code>).</p> |
| 3625 | + <p>Use the prefix <code>from</code> when there is a source object expected to be |
| 3626 | +converted to a target object. |
| 3627 | +For example, <code>Foo.fromBar()</code> would imply |
| 3628 | +that a <code>Foo</code> object will be created using a <code>Bar</code> object.</p> |
| 3629 | + <p>A common pattern is to name generic factory methods <code>create()</code> or <code>from()</code>.</p> |
| 3630 | + <p>Avoid inventing other prefixes |
| 3631 | +and using of legacy prefixes |
| 3632 | +unless there is a strong reason to do so. |
| 3633 | +A reason to make an exception would be to maintain consistency with |
| 3634 | +existing factory methods |
| 3635 | +under the same object, such as <code>document.initXXX()</code>. |
| 3636 | +New factory methods should not follow this convention.</p> |
3614 | 3637 | <h3 class="heading settled" data-level="12.6" id="naming-unsafe"><span class="secno">12.6. </span><span class="content">Warn about dangerous features</span><a class="self-link" href="#naming-unsafe"></a></h3>
|
3615 | 3638 | <p>Where possible, mark features that weaken
|
3616 | 3639 | the guarantees provided to developers
|
|
0 commit comments