<div dir="ltr">Good/important examples.<div><br></div><div>The problem here seems to be that &quot;sin&quot;, &quot;mean&quot;, &quot;remainder&quot;, etc. read nicely as noun phrases, but the guidelines would recommend -ed/-ing verb forms in this case (methods with both mutating and nonmutating variants), not noun phrases:</div><div><br></div><div>    &quot;<i>When a mutating method is described by a verb, name its nonmutating counterpart according to the &#39;ed/ing&#39; rule</i>.&quot;</div><div><br></div><div>...but we don&#39;t have a verb to which to apply -ed or -ing. (&quot;sined&quot;? &quot;meaned&quot;? &quot;remaindered&quot;?)</div><div><br></div><div>I agree that &quot;sinInPlace()&quot;, &quot;meanInPlace()&quot;, &quot;remainderInPlace()&quot; seem to work pretty well, but I&#39;m aware that I&#39;m drawing on experience with the old names of sortInPlace(), subtractInPlace(), etc., and in fact this case is worse because the first words aren&#39;t really verbs (you can&#39;t sine something <i>in place</i> any more than you can sine it). &quot;takeSine()&quot; doesn&#39;t sound great either.</div><div><br></div><div>One option is to simply not offer mutating versions of these methods; then users can write &quot;y=y.remainder()&quot; with only two extra characters (four with spaces). IMO this is awkward; it also kinda necessitates an isUniquelyReferenced check to avoid unnecessary copying.</div><div class="gmail_extra"><br></div><div class="gmail_extra"><br clear="all"><div><div><div dir="ltr"><div>Jacob &quot;no useful suggestions to see here&quot; Bandes-Storch<br></div></div></div></div>
<br><div class="gmail_quote">On Sat, Jan 23, 2016 at 11:36 PM, Tyler Fleming Cloutier via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word">This is a very real problem. It has come up a nontrivial number of times during my attempt to conform <a href="https://github.com/mattt/Surge" target="_blank">Surge</a> to the API Guideline. My work in progress version is <a href="https://github.com/TheArtOfEngineering/Jolt" target="_blank">here</a>.<div><br></div><div>Surge is in essence Swift a wrapper around the Accelerate framework. Originally the Surge API was constructed as a collection of global functions that mirrored the Accelerate framework exactly. I’m currently attempting to bring the API in line with the API Guideline and to extend CollectionType to have methods with these operations.</div><div><br></div><div>Thus I have three different types of API end points.</div><div><br></div><div>- Global functions</div><div>- Non-mutating methods</div><div>- Mutating methods</div><div><br></div><div>These distinctions could potentially be important given that these are all performance critical vector operations. Also given that this is extending the Accelerate framework many functions are named in mathematical terms, which makes them more difficult to name according to the current guidelines. Consider the following functions.</div><div><br></div><div>First let’s consider a function like `add` which operates on vectors and whose output is also a vector. This function is straightforward to conform to the API Guidelines, the result of which is quite satisfying. </div><div><br></div><div>The global function is called `add`, the mutating method is called `add`, and the non-mutating method is called `adding`.</div><div><span style="white-space:pre-wrap">        </span></div><div><span style="white-space:pre-wrap">        </span>add(x, y) vs x.add(y) vs x.adding(y)</div><div><br></div><div>Excellent.</div><div><br></div><div>Next, however, consider the vector function `sum`. In this case the global function is named `sum`. There is no corresponding mutating method for `sum` since it does not output a vector. Should the non-mutating method therefore be called `sum` or `summed`? The Guideline is not so explicit about this. You could conceivably make an argument for either, although I think `sum` would win out because there is no non-mutating version. If there is no current mutating version for an API, but there could conceivably be one in the future, I assume that it should use the ‘ed/ing&#39; rule. </div><div><br></div><div>sum(myVec) vs myVec.sum() // &lt;- this is non-mutating</div><div><div><br></div><div>So far, acceptable.</div><div><br></div><div>What about the function `sin`? </div><div><br></div><div>Again, the global function is just `sin`. How, though, do you distinguish the mutating and non-mutating methods?</div><div><br></div><div>sin(x) vs x.sin() vs x.sined()?</div><div><br></div><div>Fortunately, the API Guideline does have a comment on this. In the case of `sin`, it is reasonable to only provide the global function in accordance with mathematical convention. The problem is that memory constricted environments may require that the calculation be done in place. What that should look like is not clear.</div><div><br></div><div>What about functions that are almost always abbreviated by convention like `ceil`?</div><div><br></div><div>ceil(x) vs x.ceil() vs x.ceiled()</div><div><br></div><div>In this case the InPlace prefix can save us. x.ceilInPlace() would work.</div><div><br></div><div>Another good one is `remainder`.</div><div><br></div><div>remainder(x) vs x.remainder() vs x.remaindered() ?</div><div><br></div><div>These are just a few examples, but there are many many others in this API alone that the Guidelines struggle to address. Most are problematic for the same reasons outlined above. If it weren’t necessarily critical to reduce memory use it would suffice to just include the global function for many of them.</div><div><br></div><div>e.g.</div><div>reciprocal</div><div>threshold</div><div>floor</div><div>abs -&gt; ???</div><div>exp -&gt; x.exponentiated()?</div><div>exp2 or expSquared</div><div>logb</div><div>mean</div><div>dot</div><div>cross</div><div><br></div><div>For some functions like `dot` we could conceivably rely on the fact that familiar users would know that the return type is not the same as the arguments. I would argue however, that, if we were going to rely on the type information of the returned value then perhaps that’s what should be relied on exclusively to determine whether a method is mutating or not. </div><div><br></div><div>All mutating methods should return Void and all non mutating methods should have @warn_unused_result. Thus the context would distinguish it’s mutability. Not a huge fan of this, but with the unused warning result it’s not too bad.</div><div><br></div><div>For example.</div><div><br></div><div>let y = [...]</div><div>let x = y.remainder()</div><div><br></div><div>vs</div><div><br></div><div>var y = […]</div><div>y.remainder()</div><div><br></div><div>I hope this helps to give a little bit of color on a real world example outside of the standard library.</div><div><br></div><div>Thanks,</div><div><br></div><div>Tyler</div><div><div><div><br></div><div><br><div><blockquote type="cite"><div>On Jan 23, 2016, at 11:00 AM, Dave Abrahams via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div><br>on Fri Jan 22 2016, Joe Groff &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br><br><blockquote type="cite"><blockquote type="cite">On Jan 22, 2016, at 1:57 PM, Jeff Kelley via swift-evolution<br>&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br><br><blockquote type="cite">On Jan 22, 2016, at 4:53 PM, Joe Groff via swift-evolution<br>&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br></blockquote></blockquote><br><blockquote type="cite"><blockquote type="cite">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">mailto:swift-evolution@swift.org</a>&gt;&gt;<br>wrote:<br><br>How do you handle naming non-mutating versions of these operations?<br>Conjugating other irregular verbs also imposes a barrier on<br>developers whose first language is not English.<br></blockquote><br>Swift could use “after” as a prefix, or something similar.<br><br>array.sort()<br>array.afterSort()<br><br>I was tempted to say “afterSorting()” but that has the same problems mentioned above.<br></blockquote><br>That&#39;s reminiscent of the way the classic Cocoa naming guidelines<br>cleverly avoided these issues by using the &#39;did-&#39; prefix consistently<br>instead of ever conjugating verbs into preterite tense. &#39;after&#39; is a<br>bit awkward, though, as are any other equivalent prefixes i can think<br>of that have the same effect on the past participle (havingSplit?<br>bySplitting?)<br></blockquote><br>&quot;splitting&quot; works perfectly well on its own, IMO.  If there&#39;s an<br>argument to the method, you&#39;ll want some kind of preposition like &quot;At&quot;<br>or &quot;On&quot; afterwards.<br><br>I think the real problem cases for this guideline as written are the<br>ones where there&#39;s no underlying verb, like &quot;union&quot;, or where the verb<br>already has a strong non-mutating connotation, like &quot;exclusiveOr.&quot;<br>Those are the ones that really cry out for a different convention, like<br>&quot;InPlace.&quot;  <br><br>In developing the guidelines, we had some internal arguments about<br>whether it was worth adding complexity to accomodate those cases, or<br>whether we should simply not mention them and let them get sorted out on<br>a case-by-case basis when they come up in code review.  We didn&#39;t reach<br>consensus, so this would be a useful area in which to get community<br>input.<br><br>-- <br>-Dave<br><br>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></div></blockquote></div><br></div></div></div></div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div>