Swift: Why You Should Love Default Parameter Values

So one of my current favorite features of Swift is the ability to set default parameter values in functions. That’s because default parameter values make it very painless to add additional parameters to an existing function.

As developers, our code is always evolving. Features are added, and existing infrastructure is constantly modified to keep up with the latests requirements. So a function that starts out with only a few required parameters might grow a bit, and default parameter values make it very easy to accommodate this type of change.

In my Seinfeld Quotes app, for example, I have a very simple configure method for the QuoteTableViewCell:

Currently, the configure function takes in only one parameter – a quote object. But let’s say I want to make my tableView more readable by adding a slightly different background color to every alternating cell.

I can now add a color parameter to my configure function.

UITableViewCell Function Swift

 

However, without a default parameter value, this would break my code anywhere I already use this function:

Swift No Default Parameter

By setting the default value for the color parameter, all the places where the cell is configured continue working without any errors. In my case, I just set the color value to white as the default:

default value swift
Now, I can reconfigure which part of my application decides to actually set the color:

Configure Cell Swift

Following this pattern, when the product owner or designer comes and requires the alternate cells to be black with white text color, with default parameter values, you only need to make the change where it’s needed:

Swift Default Parameter Values

As you can see, refactoring and adding additional features to your Swift function will be really nice with default parameter values.

The only thing to keep in mind with default parameter values is the following note from Apple:

Place parameters with default values at the end of a function’s parameter list. This ensures that all calls to the function use the same order for their non-default arguments, and makes it clear that the same function is being called in each case.

Swift Programming Language

Hope you’re enjoying Swift as much as I am!

Enjoy the article? Join over 14,500+ Swift developers and enthusiasts who get my weekly updates.

  • Tom Brodhurst-Hill

    Great discussion, thank you.

    A couple of tweaks:

    1. UITableViewCell is a view class and so shouldn’t know anything about your model, such as your Quote class. I suggest passing the quote string as a parameter instead.

    2. Alternating cell coloring should be done in the willDisplayCell method of the UITableViewController. You don’t really know what color a cell should have unless it is displayed, and the color of a cell can change if another cell is deleted or inserted or if cells are reordered.

    • I would call that high quality, value adding response 🙂

    • Abizer Nasir

      Re point 1. For something called “QuoteTableViewCell” it’s not unreasonable for it to know something about the object it is displaying. Many people (me included) think it is acceptable to pass a model object to the cell and let it deal with how it displays the information from the model. This doesn’t break MVC, as it is still the controller’s responsibility to get the actual model object and pass it to the cell. The controller also has the ability to observe and respond to changes in model objects. I don’t agree that you should just pass a string, and I agree with Natasha’s current method of just passing a quote.

      • thesummersign

        Agreed. I do that all the time.
        My Cells are responsible for that chunk and I pass that as argument to configure: method.

        I make all my cell to conform to a protocol called “dataReceiver” which has this configureCell:(id) method.

        • Love the Protocol idea, especially after watching the Protocol-Oriented Programming in Swift at WWDC15! Thanks!

  • John Kountz

    Can Objective-C call Swift methods with default parameters?

    I tried it and had problems.

    Another great post!!!

    • Chris Wood

      Seems as though it doesn’t work – just ran into that problem today. if we call the function with a default value the “new function” and the function before adding the default value the “original function”, then a workaround for Swift+Obj. C projects is that after making the new function to create another function that matches the original function (call this the second function). Inside of the second function make a call to the new function with the default variable explicitly defined.

  • Tim

    It seems like (even in 2016) any change to a Swift default parameter value recompiles everything. That’s a rather significant limitation.

  • David Moheban

    Was wondering what does the ‘#’ sign mean when it comes before your parameter name?

    thanks