Swift-ing my way through - Internal or External Parameters

We become so agnostic to compiler telling us how to do things and for the sake of getting shit done and quite comfortably, we never bother why. Unlike any other programming language Swift has a concept of Internal and External parameters.

When I started writing code in Swift I saw a weird little underscore while calling methods with parameters. I asked people around if I was doing something wrong and mostly all of them replied, “ Must be some convention” and so I too moved on. Not long ago, I ran into and issue and decided to do what is dreaded by most — RTFM!

And there it was, in all its glory; taken verbatim from Apple developer library.

Modifying External Parameter Name Behavior for Methods
Sometimes it’s useful to provide an external parameter name for a method’s first parameter, even though this is not the default behavior. To do so, you can add an explicit external name yourself.
Conversely, if you do not want to provide an external name for the second or subsequent parameter of a method, override the default behavior by using an underscore character (_) as an explicit external parameter name for that parameter.

No, seriously. Stop. Read again. Chances are if you did not already know this you’ll have an epiphany just like me, “Oh so thats why the Swift always tabbed out the first parameter!”. While it may not affect most of your work but you can definitely use it to make your code look clean and readable.

So this is pretty basic, to internalize any parameters use the underscore before the name. Goes without saying, since the first one is internalized already you do not need to underscore first parameter. I made up a quick method which you can playground.

func myCatSeemsToHaveSuperpowers(name: String, superpower: String){
print(“Don’t let the world know that \(name) has \(superpower) power”)

So the corresponding method call is something like this:

myCatSeemsToHaveSuperpowers(“Romew”, superpower: “Double back-flip”)

By just reading the method call here does not give much context about the first one. Hence, Swift allows externalization. So the same method looks like this now:

func myCatSeemsToHaveSuperpowers(name name: String, superpower: String){
 print(“Don’t let the world know that \(name) has \(superpower) power”)

and the call to it is:

myCatSeemsToHaveSuperpowers(name: “Romew”, superpower: “Double back-flip”)

So, unless you have reasons like keep parameter names super secret from the annoying colleague Larry or you’re just to lazy to write parameter name twice, this might seem a good practice (Don’t forget to be good to Larry, uhhmm eventually).

However, I would give a word of warning to people who work with both Swift and maintain the legacy code of Objective-C. So, say if you write a function in Swift with all external parameters and you want to call it in Objective-C then the method call look like this:

[self myCatSeemsToHaveSuperpowersWithname:@“Romew” superpower:@“Double back-flip”];

The compiler appends the Withname to the method name. This seems a bit confusing. Especially if you are debugging something and you search the name of the function, Xcode will catch you off-guard as it will not show the two functions as the same. I wish this gets fixed. With WWDC just around the corner, who knows!

But if you are all Swift, you need not worry.