Swift trades in Objective-C's message passing syntax for something much more akin to function calling. The language keeps the named parameters, but throws in oddly placed parenthesis and commas, resulting in weird, difficult to read code. I am not suggesting Objective-C has The One True Syntax, and I am definitely open to seeing what other ideas the Swift team might come up with. But I do feel strongly that the current syntax is severely lacking in several ways.
- These are not, generally speaking, mathematical functions. They are messages being sent to an object. The mathematical form "a(x)" doesn't even make sense in most cases. This is something almost every popular programming language gets wrong.
- That the first argument to a Swift method loses its name is strong evidence that there is something wrong with the syntax.
- The message forms a single thought, so no unnecessary punctuation should be present, except that which is necessary to distinguish the message from surrounding code. To illustrate, try reading this code out loud:
[baby percentileForWeight:weight atAge:age]
I read that as "bleh baby percentile for weight weight at age age bleh".
Now compare the same in Swift:
That reads as "baby bleh percentile for weight weight comma at age age bleh".
There are far more blehs in the Swift example and, more importantly, they occur in the middle of the thought. Neither is perfect, to be sure, but the Objective-C code has no interruptions in the middle to account for excess punctuation such as periods, parenthesis, and commas.
If nothing else, the comma needs to go.
As stated above, I'm not necessarily tied to the exact syntax used by Objective-C, but it does have some other, less obvious advantages over Swift's. For example, on an English keyboard, [ and ] can by typed with a single key, while ( and ) require a chord. Over the course of a day, those extra keystrokes add up!
Aside from Objective-C's syntax, whose only *real* problem is that it is slightly scary to people coming from other popular programming languages, I don't have any suggestions for alternatives. I would surely love to see Objective-C's message passing syntax brought in to Swift - it would be a tremendous improvement - but I'm also very interested to see what else the Swift team can come up with. Please consider adopting some other syntax for message passing.
Steps to Reproduce
Xcode 6 Beta 1
An excessive example:
CPTPlot* plot = [self plotWithBottomPercentile:bottomPercentile topPercentile:topPercentile fill:fills[i] lineStyle:lineStyle textStyle:textStyle numberFormatter:nf xLoc:xLoc xVal:xVal plotSpace:aGraph.defaultPlotSpace insertIntoPointsArray:plotPoints chartStyle:style chartViewStyle:chartViewStyle];
let plot = plotWithBottomPercentile(bottomPercentile, topPercentile:topPercentile, fill:fills[i], lineStyle:lineStyle, textStyle:textStyle, numberFormatter:nf, xLoc:xLoc, xVal:xVal, plotSpace:aGraph.defaultPlotSpace, insertIntoPointsArray:plotPoints, chartStyle:style, chartViewStyle:chartViewStyle)
Leaving aside the design decision to have a method with so many parameters, this reveals a number of issues with Swift:
- It's a punctuation jamboree!
- Why does the first parameter have a ( in front of it instead of a : ?
- Look at all those commas! What are they needed for?