Better function parameter labels in Swift

Swift 3

Deciding on a good function name will help you later to remember what the function does and what it is expecting from you for the parameters you pass. I’m a big fan of long function names. If it takes the guessing out of what the function does, then I’m all for it. I can’t see any real advantage to having it short.

What is not used often enough is more explicit parameter names to go along with the great function name we created.

Consider you have a function that returns the day of the week by passing it an index which is an Int.

Well, it is obvious enough from the function name what this does, but index is used three times.

Using _ to avoid using a parameter label

What we could do is lose the parameter label by using _ before the index parameter which will be used by the function itself.

When calling the function, we do not need to use a label at all.

This is better, but we can shorten the function name by allowing the parameter label to explain what we need for this function, and the variable index was named in a way that does the rest.

Allowing the function parameter to act as a label only

The for label allows us to have a placeholder that isn’t used in the function itself. We can take advantage of this to achieve a shorter function name. Xcode will display that it requires an Int on auto completion of the function, so there is never a need to put types in the label. You wouldn’t use forInt for example. This is how Xcode displays it.


The parameter label can help us shorten the function name, and provide a clearer idea what the parameter should be, and all this can be done without changing code in the function itself.

Leave a Reply

Your email address will not be published. Required fields are marked *