Did you find this helpful? Why not support me to bring you even more content.
Click here to Buy Me a Coffee and say Thanks!

DISCLAIMER: This post is older than one year and maybe out of date.

Changing Function Argument Names Magically

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.

The Problem

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

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

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.

The Solution

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.

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.

The Wrap

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 🙂

Share Button