I'm still confused as to exactly how the Swift team expects us to name our functions/methods. With Swift 2.0, much of the parameter naming inconsistencies have been removed but the "first parameter does not get an external name" default remains. The Swift book says to make prepositions part of the function name, as in "incrementBy(x, numberOfTimes: y)" to fit this weird rule. Is there a reason not to prefer "increment(by: x, numberOfTimes: y)" ? The latter makes a lot more sense if, like me, you're not coming from Objective-C but started with Swift. To confuse me even further, the Swift API now has methods that seem to agree with me. An example of this is the new stride method which is "stride(through: x, by: y)" and not "strideThrough(x, by: y)". Can I conclude this whole "first parameter does not get an external name" thing really is only about Objective-C compatibility and that the Swift team agrees it does not make sense anymore in Swift?
Function naming guidelines
I'd like to know if there are any clear guidelines too. Although in the standard library as of Xcode 7 beta 6, there are only 4 public methods with an external parameter name for their first parameter:
let seq1 = 0.stride(through: 4, by: 2) // <-- #1
let seq2 = 0.stride(to: 4, by: 2) // <-- #2
var ar = [1, 2, 3]
ar.removeAll(keepCapacity: true) // <-- #3
let escaped = UnicodeScalar("a").escape(asASCII: true) // <-- #4Here is the text you mentioned from the Swift book:
"
Methods in Swift are very similar to their counterparts in Objective-C. As in Objective-C, the name of a method in Swift typically referes to the method's first parameter using a preposition such as with, for or by, as seen in the incrementBy(_:) method from the preceding Counter class example. The use of a preposition enables the method to be read as a sentence when it is called.
Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default. This convention matches the typical naming and calling convention you will be familiar with from writing Objective-C methods, and makes for expressive method calls without the need to qualify your parameter names.
/.../ Sometimes it's useful to provide an external parameter name for a method's first parameter, even though this is not the default behaviour. To do so, you can add an explicit external name yourself.
"
It doesn't say anything more about when/why it might be "useful to provide an external parameter name for a method's first parameter". Perhaps the parameter names for the above #3 and #4 (which are not prepositions) are examples of this.
But, as you say, according to the guidelines I guess #1 and #2 should really be:
let seq1 = 0.strideThrough(4, by: 2)
let seq2 = 0.strideTo(4, by: 2)
The current rules are surely only in-place to make it easier to interoperate with Objective-C. They do not make sense. Here is sense as I see it:
A function will return a noun, in which case the name of the function outside the parentheses is that noun.
A function is a predicate, and the name of the function outside the parentheses is a verb.
That sense has yet to fail me.
I'm not saying that the naming rules of Swift are fantastic. But what does your sense say about this function:
print("hello")Print (in this context) is a verb, yet the function is not a predicate. So it is misnamed according to you. It returns Void, which is a noun, "in which case the name of the function outside the parentheses is that noun", so instead of print("hello"), it should be void("hello")?
(Or do you mean print is a predicate, like in Prolog or something?)