Swift’s Named Parameters

You can have internal and external names. What next?

Naming conventions are extremely important in programming. Further to this, when you use functions you can have internal and external names which should make a programmer’s life easier. That is, if it is possible to understand them — although this short guide should help you out!

Image for post
Image for post
Photo by Luca Bravo on Unsplash

Prerequisites:

  • Be able to produce a “Hello, World!” iOS application (guide HERE)
  • Some basic knowledge of functions (guide HERE)
  • Completion handlers are mentioned later on, if you are unfamiliar with these you can skip this section or read the guide! (guide HERE)

Terminology

Argument labels (External names): The name of a variable that is used externally when a function is called

Function: A group of statements that together can perform a function

Function signature: The header for a function, specifying the return type and parameters for a function

Parameter names (Local names): The name of a variable that is used internally within a function

Parameters: A type of variable that is used to pass information into functions

Using local function names

Using parameters in a function can make the meaning of a function clearer

func quickRoute(from: Int, to: Int) {
// calculation to decide the best route from (from) to (to)
}
quickRoute(from: 1, to: 2)

This route function can refer to something like a function that calculates a route from a point on a map to a destination (the points on the map are represented here as Integers).

Now the example above uses local function names, and this is fine for a function like the one above.

You might want something a little more complicated.

But you don’t need an argument label

An external name is not necessary, and if you are sure that the meaning of your parameters is clear you might create a function like the following:

func concatString(_ s1: String,_ s2: String, _ with: String) -> String {
return s1 + with + S2
}
concatString("first", "second", ", ")

Each parameter is preceded by an underscore _ and a space, indicating that there is not an argument label

But, when calling the function with

concatString("first", "second", ", ")

It can be argued that the parameters are not clear. I mean, the output is given as first, second but how do you know that by just looking at the function? What if you are developer working on this code in a different location to the person who originally wrote this code? What if you are working on this code in a month, or a year.

We can do better. When doing something like omitting an argument label you need to think about whether there is a sensible reason to do that.

Default parameter values

So imagine the situation. You sometimes want to pass a value to a parameter, and sometimes you want to have a default. In this case, Swift has you covered!

func createName(firstName: String, middleName: String = "", surname: String) {
print (firstName, middleName, surname)
}
createName(firstName: "Steven", surname: "Curtis")
createName(firstName: "Steven", middleName: "Peter", surname: "Curtis")

now both ways to call createName are valid — whether a person has or has not got a middle name.

Now I know what you are thinking, there is no real reason to use argument labels here. Go on, be like that.

We can create the function with such a condition, but we must place the parameters that don’t have a default value before those that do. If not you will get the following error and (un) helpful message from the Swift compiler

Image for post
Image for post

so we are reduced to writing the following function

func createName(firstName: String, surname: String, middleName: String = "") {
print (firstName, middleName, surname)
}
createName("Steven", "Curtis")
createName("Steven", "Curtis", "Peter")

I would argue that the last way of calling the function, createName("Steven","Curtis", "Peter") is unacceptable as the order has been lost.

So aren’t we at square one with regards to omiting parameter names. I would say no, not quite.

Adding and arguments to existing functions without breaking the code

Imagine the situation. You’ve got completely working code (even with tests, you professional programmer). You want to add some functionality to your code, and you don’t want to break you existing hard work and tests. One typical strategy is to cry.

A better strategy is to use a default parameter for your function.

Now as we have seen createName as defined above.

func createName(firstName: String, surname: String, middleName: String = "")

we can call this with or without the middleName.

createName(firstName: "Steven", surname: "Curtis")
createName(firstName: "Steven", middleName: "Peter", surname: "Curtis")

So if we had the initial signature without the the middleName, and then added it later with the default parameter we would not break the first call to the function.

Naming a function as a parameter

Now this needs you to have some understanding of completion handlers. If you don’t have this prerequisite knowledge, please feel free to skip this section.

Callbacks are everywhere in the Swift SDK.

These are named, and without going into to much detail we can take an example that you might be familiar with (UIAlertController)

self.present(alertController, animated: true, completion: nil)

you can see that the completion parameter is named, well, completion.

We can of course create our own completion handler and name this as we like:

func myFunc(completion: (Bool)->()) {
completion(true)
}
// called with
myFunc(completion: {complete in
print (complete)
}
)

Conclusion

Swift is really flexible in terms of naming your functions and even naming the parameters within the declaration of your functions.

The challenge is rather to name them well to produce clear and readable code. We all set out with the intention of creating great maintainable code but this does not always work out quite as well as we intend to, and as ever the solution to this is thought as we move through the process of designing and publishing our code.

If I have one wish for you, it is that you take the naming of your functions and parameters seriously. Through the use of this tutorial you are now able to do just that!

In any case, as ever, happy coding!

Want to get in contact? Try the link here:

https://twitter.com/stevenpcurtis

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store