Did you know arguments in Swift functions can have more than one name? Which is great for making functions self-documenting with sensible naming conventions. Open a Swift Playground and try this.
Admit it, we have all created functions with arguments that made sense at the time. Then six months later we are wondering ‘why did I call that a, and what did b really mean and should I refactor now and risk breaking existing code?’ Let’s say there is a function something like this
func myFunction (email:String, content:String) {
print(“To: \(email)”)
print(“Message: \(content)”)
}
myFunction (“John@email.com”, content:”Let’s do Lunch!”)
From the code, it is not clear from the outside or to anyone else that ‘email’ is an address, or maybe it’s the entire email? If I am calling this function in the future, I do not want to keep looking up the code to remind myself. Sure we could fix this problem in the function with
func myFunction (email:String, content:String) {
let emailAddress = email
print(“To: \(emailAddress)”)
print(“Message: \(content)”)
}
That way at least in debugging we will have a better sense of what that argument is supposed to be, and it will make more sense to our teammates.
But there is a better way, we can call the function with email having a better name but without the need to change the internal code of the function.
func myFunction (to email:String, content:String) {
print(“To: \(email)”)
print(“Message: \(content)”)
}
myFunction (to:”John@email.com”, content:”Let’s do Lunch!”)
See what we did there? We added to in front of email so we can call it using to but the internal code still works with email.
Granted I do not immediately see a big use for this, but it’s a small thing to keep in the back pocket in case you need it, or just to impress your friends at scrum meetings with :)