<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 22, 2016, at 4:12 PM, Ross O'Brien &lt;<a href="mailto:narrativium@gmail.com" class="">narrativium@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">How would we apply this to delegate patterns?<div class="">For example, would we keep tableview(tableView:cellForRowAtIndexPath:), or would we switch to delegate(tableView:cellForRowAtIndexPath:) ?</div><div class="">Or perhaps better, for clarity over which protocol is being conformed to / which property of the delegator is calling the function:</div><div class="">dataSource(tableView:cellForRowAtIndexPath:),</div><div class="">delegate(tableView:didSelectRowAtIndexPath:)</div></div></div></blockquote><div><br class=""></div><div>There would be no argument label for `tableView`; it’s a needless word and there is a strong type information: it’s a TableView.&nbsp;</div><div><br class=""></div><div>Weak type information would be like “key path” below. Yes, it’s String, but it’s actually a specially crafted string that takes the form: “some.nested.values”. A string like “some+234+//23” is not going to work.</div><div><br class=""></div><div>I’m also not really convinced that `removeAt(position: Int)` doesn’t actually fall under both rules. The first parameter is a weak type; it has rules associated with it (non-zero and less than collection size). In the guidelines, it could be argued that it should be `removeAtIndex(position: Int)`. I’d prefer the `removeAt(index position: Int)` version. But, you can kinda make the weak type argument for a lot of scenarios… It’s a little ambiguous to me.</div><div><br class=""></div><div>-David</div><br class=""><blockquote type="cite" class=""><div class=""><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sat, Jan 23, 2016 at 12:00 AM, David Owens II via swift-evolution <span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><blockquote type="cite" class=""><div class=""><div class=""><b class="">Compensate For Weak Type Information&nbsp;as needed to clarify a parameter’s&nbsp;role.<br class=""></b></div><div class=""><br class=""></div>Especially when a parameter type is&nbsp;NSObject,&nbsp;Any,&nbsp;AnyObject, or a fundamental type such&nbsp;Int&nbsp;or&nbsp;String, type information and context at the&nbsp;point of use may not fully convey intent. In this example, the declaration may be clear, but the use site is vague:<br class=""><br class=""></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px" class=""><div class=""><div class=""><font face="Menlo" class="">func add(observer: NSObject, for keyPath: String)</font></div></div><div class=""><div class=""><font face="Menlo" class="">grid.add(self, for: graphics) // vague</font></div></div></blockquote><div class=""><br class="">To restore clarity,&nbsp;precede each weakly-typed parameter with a noun describing its role:<br class=""><br class=""></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px" class=""><div class=""><font face="Menlo" class="">func addObserver(_ observer: NSObject, forKeyPath path: String)</font></div><div class=""><div class=""><font face="Menlo" class="">grid.addObserver(self, forKeyPath: graphics) // clear</font></div></div></blockquote></blockquote><div class=""><br class=""></div><div class="">I don’t understand why to compensate for weak type information we put some of that compensation in the name of the function and other parts of it in the [external] name of the parameter.</div><div class=""><br class=""></div><div class="">If we were going to reference functions like this: addObserver:forKeyPath, then I can understand it. But that’s not the plan of record, it’s to do this: addObserver(_:forKeyPath).&nbsp;</div><div class=""><br class=""></div><div class="">Regardless of the default naming scheme, it seems like the rule should be to use external names to clarify that parameters role.</div><div class=""><br class=""></div><div class=""><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class=""><font face="Menlo" class="">func add(observer observer: NSObject, forKeyPath path: String)</font></div><div class=""><font face="Menlo" class="">grid.add(observer: self, forKeyPath: graphics)</font></div></blockquote></div><div class=""><br class=""></div><div class="">This promotes a very clear and consistent rule: weak type information should be clarified by the parameter’s external name. There are no exceptions for the first parameter. Otherwise, it seems like there is super fine line between this rule and the next one below.&nbsp;</div><div class=""><br class=""></div><div class="">Additionally, this also alleviates my concerns with the default parameter have _ as the external name by default because this addresses the case when&nbsp;it would be desirable to have that name. Further, the case below handles the case when&nbsp;it’s not.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div class=""><b class=""></b></div></div><blockquote type="cite" class=""><div class=""><div class=""><b class="">Omit Needless Words. Every word in a name should convey salient information at the use site.<br class=""></b></div><div class=""><br class=""></div>More words may be needed to clarify intent or disambiguate meaning, but those that are redundant with information the reader already possesses&nbsp;should be omitted. In particular, omit words that&nbsp;merely&nbsp;repeat type information:<br class=""><br class=""></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px" class=""><div class=""><div class=""><font face="Menlo" class="">public mutating func removeElement(member: Element) -&gt; Element?</font></div></div><div class=""><div class=""><font face="Menlo" class="">allViews.removeElement(cancelButton)</font></div></div></blockquote><div class=""><br class="">In this case, the word&nbsp;Element&nbsp;adds nothing salient at the call site. This API would be better:<br class=""><br class=""></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px" class=""><div class=""><div class=""><font face="Menlo" class="">public mutating func remove(member: Element) -&gt; Element?</font></div></div><div class=""><div class=""><font face="Menlo" class="">allViews.remove(cancelButton) // clearer</font></div></div></blockquote><div class=""><br class="">Occasionally, repeating type information is necessary to avoid ambiguity, but in general it is better to use a word that describes a parameter’s&nbsp;role&nbsp;rather&nbsp;than its type. See the next item for details.</div></blockquote><div class=""><br class=""></div><div class="">The description here seems to overlap with the “Compensate for Weak Type Information” rule, especially with the clause: “repeating type information”. It may be better to re-work the example to be `removeItem(member: Element)` to make this distinction more clear that it’s not type information being removed.</div><div class=""><br class=""></div><div class="">Also, by clarifying that statement, the above rule change I suggested would be consistent. Type information clarification goes into the external parameter name, functionality clarification goes into the function name. Those are hard-n-fast rules that are straight-forward.&nbsp;</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""></div><blockquote type="cite" class=""><div class=""><b class="">Be Grammatical</b></div><span class=""><div class=""><b class=""><br class=""></b></div><div class=""><div class="">When&nbsp;a mutating method is described by a verb, name its non-mutating counterpart&nbsp;according to the&nbsp;“ed/ing” rule, e.g. the non-mutating&nbsp;versions of&nbsp;x.sort()&nbsp;and&nbsp;x.append(y)&nbsp;are&nbsp;x.sorted()&nbsp;and&nbsp;x.appending(y).</div></div></span></blockquote><div class=""><br class=""></div><div class="">Is this guideline suggesting that we should design our APIs to generally have both mutating and non-mutaging counterparts?</div><div class=""><br class=""></div><div class="">As other have pointed out, this is also very hard to do all the time. I think the alternatives are worse. It would be nice if there were a way to annotate all member functions as mutating/non-mutating to really by-pass this ambiguity.</div><div class=""><br class=""></div><div class="">Other than the above, the proposal looks pretty good to me.</div><div class=""><br class=""></div><div class="">-David</div><br class=""></div><br class="">_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class=""></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></body></html>