Re: Swift and parameter names
Re: Swift and parameter names
- Subject: Re: Swift and parameter names
- From: Roland King <email@hidden>
- Date: Thu, 25 Jun 2015 22:45:23 +0800
> On 25 Jun 2015, at 16:50, email@hidden wrote:
>
>
>> On 2015/06/25, at 15:49, Britt Durbrow <email@hidden> wrote:
>>
>> IMHO, named parameters at call sites are one of the things that makes Objective-C great; and I am VERY happy that Swift-2 enforces this.
>
> I agree whole heartedly.
> But will call out that it is a considered and thoughtful design by Cocoa and Foundation Framework engineers. Objective-C doesn't require it.
> Look at the old Next manual... There were a few obtuse methods.
>
> The IDE takes the burden of typing away.
> The conventions established help promote good coding practices that enable clarity and reduce mistakes and confusion.
> The (general) consistency and effort towards consistency those folks work towards is for everyone who codes to do it better.
> For themselves to more easily support developers consuming the APIs and end customers to have better quality products.
I like where it ended up with Swift 2.0. In a vacuum I would possibly have gone for all parameters named and not 'all but the first one’ (with some exceptions for constructors) but I see how we got there and it does fit the transliteration of API from ObjC to Swift. And that standard transliteration, especially in the latest Swift, has made switching from ObjC to Swift much easier so I’m all for bringing that function naming convention over.
I think one of the patterns I had to learn to make it work was naming functions like you do in objective C. I had no problem in that language doing
-(Felony*)doSomethingWithJello:(Jello*)jello peaShooter:(PeaShooter*)peaShooter batmanMask:(BatmanMask*)batmanMask;
when I first started writing Swift it looked like C to me, so my function name just became the verb and I got this
func doSomething( jello : Jello, peaShooter : PeaShooter, batmanMask : BatmanMask )->Felony
and when you have that it does’t make sense you don’t need to name the Jello. Once I started naming the functions in the same way I would have done the original objC one, it all made more sense
func doSomethingWithJello( jello : Jello, peaShooter : PeaShooter, batmanMask : BatmanMask )->Felony
On the odd occasion that I write more C-name-like functions then I usually double up the first parameter so they are all named when you call them. And if I have an initialiser which can only obviously take one argument, especially with generics where the argument is often ‘initialValue’, I underscore it away.
95% of the time I find I follow the conventions however and code is pretty readable. Similarly so is other people’s.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden