Group Group Group Group Group Group Group Group Group

iOS 10 By Tutorials - Function naming semantics

This is a topic to discuss any function semantic questions/issue/concerns with Swift 3.0’s migration and the book semantics.

In Chapter 9: Property Animators, page 181 on the pre released book I noticed the following declarations within the CGPoint+Trig.swift file:

  func distance(toPoint point: CGPoint) -> CGFloat {
    return sqrt((point.x - x) * (point.x - x) + (point.y - y) * (point.y - y))
  func normalize(weight: CGFloat) -> CGPoint {
    return CGPoint(x: x / weight, y: y / weight)

The distance(toPoint:) function seems a bit out of place with the Swift 3 API guidelines. Wouldn’t you expect something more akin to the public func distance(from start: String.Index, to end: String.Index) -> String.IndexDistance declaration in String? Something more along the lines of distance(to point: CGPoint) -> CGFloat?

The same can be applied to the utility function:

  var toVector: CGVector {
    return CGVector(dx: x, dy: y)

where the calculated property is very similar to

open var ciColor: CIColor { get }

available in UIColor.

Emulating the semantics, shouldn’t toVector just be vector?

There are quite a few other areas in the book where I see repeating semantics that reflect the older Swift 2 and Objective-C naming conventions. I actually would love to see something similar to the TreeHouse blog post detailing out appropriate naming conventions as with the grammatical rules within the Swift 3.0 API Guidelines, this can be confusing at times.


Regarding the naming semantics of the distance(toPoint:) function, I did find the clause in the Swift API guidelines addressing proper verbiage:

Nick me if I’m wrong lol.