3 ms·
Named arguments is a HUGE thing for ergonomics. It makes code many times more readable, since it automatically documents intent much more clearly. The code link
by user-the-name 6y ago
Named arguments is a HUGE thing for ergonomics. It makes code many times more readable, since it automatically documents intent much more clearly. The code linked here is nowhere near representative of regular Swift code.
As a very basic made-up example, the real value of named arguments is code like this:
FileManager.copy(path1, path2)
What does that do? There is no way to tell which file is copied to which. In Swift, this is written as:
FileManager.copy(from: path1, to: path2)
Suddenly, there is no need to look up the function to figure out what it does. It is perfectly clear.
- todd8 6y agoThe first use I recall for named parameters was in Algol60. One could write: copyfile(path1)to:(path2) Instead of: copyfile(path1, path2) I always liked that idea, but found Objective-C’s version of this less attractive. Smalltalk had named parameters too. Algol60, to me, had the nicest syntax for this.
- user-the-name 6y agoSwift has improved nicely on the Objective-C style, I think.
- ChrisMarshallNY 6y agoIf you were being "idiomatic," you'd probably write it like so: MyOwnFileManager.copyFileFrom(_ path1: String, to path2: String) Apple has started to make it like that.
- user-the-name 6y agoNo, that is the old Objective-C style. It is definitely not idiomatic.
- ChrisMarshallNY 6y agoIn the more recent Swift and library releases, I have had to change my code to fit the above pattern, as I get deprecation errors all the time. But you are correct that it started with ObjC. That doesn't make it a bad thing. It seems that Apple is starting to circle back. I prefer the initial one that you posited. I'm pretty sure the Swift style guide suggests the way you mentioned, and not the ObjC way. BTW: I deleted the post where I talk about where I saw this. It seems folks just want to argue, and that's not why I come here.
- user-the-name 6y agoI've not seen any of these in years now. Where have they been popping up?
- deleted 6y ago[deleted]