[swift-evolution] When to use argument labels (a new approach)

Jonathan Tang jonathan.d.tang at gmail.com
Wed Feb 3 00:17:44 CST 2016


On Tue, Feb 2, 2016 at 4:32 PM, Dave Abrahams via swift-evolution <
swift-evolution at swift.org> wrote:

>
> This thread is related to the review of new API guidelines, but it's not
> a review thread; it's exploratory.  The goal is to come up with
> guidelines that:
>
> * describe when and where to use argument labels
> * require labels in many of the cases people have asked for them
> * are understandable by humans
> * preserve important semantics communicated by existing APIs.
>
> Here's what I'm thinking
>
> 1. If and only if the first argument could complete a sentence*
>    beginning in the base name and describing the primary semantics of
>    the call, it gets no argument label:
>
>      a.contains(b)  // b completes the phrase "a contains b"
>      a.mergeWith(b) // b completes the phrase "merge with b"
>
>      a.dismiss(animated: b) // "a, dismiss b" is a sentence but
>                             // doesn't describe the semantics at all,
>                             // thus we add a label for b.
>
>      a.moveTo(x: 300, y: 400) // "a, move to 300" is a sentence
>                               // but doesn't describe the primary
>                               // semantics, which are to move in both
>                               // x and y.  Thus, x gets a label.
>
>      a.readFrom(u, ofType: b) // "a, read from u" describes
>                               // the primary semantics, so u gets no
>                               // label. b is an
>                               // option that tunes the primary
>                               // semantics
>
>    [Note that this covers all the direct object cases and, I believe,
>    all the default argument cases too, so maybe that exception can be
>    dropped.  We still need the exceptions for full-width type
>    conversions and indistinguishable peers]
>
>    Note: when there is a noun in the base name describing the role of the
>    first argument, we skip it in considering this criterion:
>
>       a.addObserver(b) // "a, add b" completes a sentence describing
>                        // the semantics.  "Observer" is omitted in
>                        // making this determination.
>
> * We could say "clause" here but I think making it an *independent*
>   clause doesn't rule out any important use-cases (see
>   https://web.cn.edu/kwheeler/gram_clauses_n_phrases.html) and at that
>   point, you might as well say "sentence," which is a more
>   universally-understood term.
>
>
+1


> 2. Words that describe attributes of an *already-existing* instance
>    should go in the base name rather than in a label:
>
>       a.tracksHavingMediaType("Wax Cylinder")      // yes
>       a.removeFirstTrackHavingMediaType("BetaMax") // yes
>
>       a.tracks(mediaType: "Wax Cylinder")          // no
>       a.removeFirstTrack(havingMediaType: "BetaMax") // no
>
>    [yes, we could use "With" instead of "Having", but it's more
>    ambiguous]
>
>    Words that describe attributes of an instance *to be created* should
>    go in argument labels, rather than the base name (for parity with
>    initializers):
>
>       AudioTrack(mediaType: "BetaMax")                   // initializer
>       trackFactory.newTrack(mediaType: "Wax Cylinder")   // yes
>
>       trackFactory.newTrackWithMediaType("Wax Cylinder") // no
>
>
Very mixed feelings on this, probably adding up to a +0.2 or so.  I'll
mention a couple concerns that I haven't seen anyone raise:

Please consider the first-class function case when naming.  Particularly
since Swift encourages passing functions around as objects rather than
using string selectors.  #2 implies that the prepositional phrase will
appear when *referencing* the method (vs. calling it):

  let ops = [
    self.removeFirstTrackHavingMediaType,
    self.addTrackWithMediaType
    self.populateTrackOperationsForMediaType
    self.play
  ]

vs.

  let ops = [
    self.removeFirstTrack
    self.addTrack
    self.populateTrackOperations
    self.play
  ]

The second option wins on verbosity, but the first arguably gives more
clarity as to what the methods actually do.  Also, the second has a
potentially annoying semantic problem: if you have overloads for these
methods that differ only in keyword, Swift won't be able to disambiguate
them:

  // Compile error: Invalid redeclaration of removeFirstTrack
  func removeFirstTrack(havingMediaType: String) { ... }
  func removeFirstTrack(named: String) { ... }
  func removeFirstTrack(byArtist: String) { ... }

  // Compile error only when the function is referenced
  func removeTrack(n: Int, named: String)
  func removeTrack(n: Int, byArtist: String)
  let f = self.removeTrack   // named: or byArtist:?

  // Legal...
  func removeFirstTrackHavingMediaType(_: String) { ... }
  func removeFirstTrackNamed(_: String) { ... }
  func removeFirstTrackByArtist(_: String) { ... }

Unless the Swift compiler were to change to make the former set legal, I
think this is a powerful argument in favor of this proposal, because
otherwise you may find that the compiler prevents you from writing the code
that the guidelines encourage.  You might also find that changing the type
of an overload means you have to change the name to prevent a collision,
which could be very surprising to users.

My other reservation is the flip side of this argument: when you use a
function in a first-class context, often you *want* to leave things
unspecified.  After all, the whole point of parameterized code is so that
it can be used in more contexts.  This comes most often with containers or
other generic functions:

  myStringArray.map(myString.appendContentsOf)    // wordy and awkward
  myStringArray.map(myString.append)  // cleaner

I'd consider this a less severe problem, but something I'd still like
considered, particularly given the existence of several higher-order
functions in the standard library.


> 3. (this one is separable) When the first argument is the *name* or
>    *identifier* of the subject in the base name, do not label it or
>    describe it in the base name.
>
>       a.transitionToScene(.GreatHall)               // yes
>       a.transitionToSceneWithIdentifier(.GreatHall) // no
>
>       let p = someFont.glyph("propellor")           // yes
>       let p = someFont.glyphWithName("propellor")   // no
>       let p = someFont.glyph(name: "propellor")     // no
>
>
>
+1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160202/f424e07b/attachment.html>


More information about the swift-evolution mailing list