Hacker News new | ask | show | jobs
by nardi 3796 days ago
The point here (no pun intended) is to stop repeating noun phrases at the call site. Your call site says "point" twice for no reason:

    path.addLine(toPoint: CGPoint(x: 100, y: 0))
See the "toPoint: CGPoint". It's useless repetition. The new form eliminates it:

    path.addLineTo(CGPoint(x: 100, y: 0))
It's clear you're adding a line to a point, because it says "Point" right there. Even if you have a variable for this point already, it works great:

    path.addLineTo(point)
Or maybe this is a more specifically named point?

    path.addLineTo(centerPoint)
It's clear from all that you're adding a line to a point. Swift has an emphasis on concise syntax, and removing the repetition, IMO, is a nice win in readability.
1 comments

You do have a good point, and something I didn't explicitly notice before. This does seem to have the indirect consequence of enforcing some sort of type information in the variable name, but I expect many iOS developers do that regardless.

It'd also be interesting to see the impact on readability when you have longer variable names. `point` definitely makes it more concise here. But if you had several points within the same scope, readability may suffer given type information is typically at the end of a variable name?

  path.addLineTo(childViewTopLeftPoint)
  path.addCurveTo(childViewTopRightPoint, withControlPoint1: arbitrary1Point, andControlPoint2: arbitrary2Point)

  path.addLine(toPoint: childViewTopLeftPoint)
  path.addCurve(toPoint: childViewTopRightPoint, withControlPoint1: arbitrary1Point, andControlPoint2: arbitrary2Point)
I feel the second version here allows me to bypass obtaining the type information "..Point" from the variable name when reading.

Interestingly, I wonder how this type of method would be converted:

  addArcWithCenter(center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool)
You'd perhaps think of addArcWith(center: CGPoint), but then you'd need to have 'center' in the variable name to convey meaning. Keeping addArcWithCenter maintains obj-c status quo. addArc(withCenter: CGPoint) is more Swifty, but you may have repetition if your variable is named centerPoint, or similar. I have a feeling it would be kept as is.