New if let shorthand for optional binding

⋅ 3 min read ⋅ Swift Swift 5.7

Table of Contents

In Swift 5.7 (Xcode 14), there is a new shorthand syntax for Optional binding[1].

Here is how we do optional binding before Swift 5.7

let foo: String? = "optional string"

if let foo = foo {
// foo contains a value
}

The new if-let shorthand is a syntax for shadowing[2] an existing optional variable, which is a very common pattern.

We no longer need right-hand expression (= foo).

let foo: String? = "optional string"

if let foo { // no longer need = foo
// foo contains a value
}

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

What is the problem with the current optional binding

It is a good practice to name our variable as descriptive as possible. This might lead to lengthy variable names.

let productListResponse: Array<Product>? = ...
let productNameLabel: UILabel? = ...

if let productNameLabel = productNameLabel,
let productListResponse = productListResponse,
productListResponse.count > 0 {
...
}

The problems with a long variable name and optional binding are:

  1. Redundancy. We have to repeat our lengthy variable name without much value out of it.
  2. Hard to read. The code becomes verbose and difficult to read.
  3. This might lead to shorter, less descriptive names for the unwrapped variables.

Using a shorter, less descriptive name for the unwrapped variables is a workaround that we used for this problem before Swift 5.7.

let productListResponse: Array<Product>? = ...
let productNameLabel: UILabel? = ...

if let label = productNameLabel,
let response = productListResponse,
response.count > 0 {

}

Optional is a core of Swift language. Having to do optional binding every day might make us unconsciously name every variable shorter.

let response: Array<Product>? = ...
let label: UILabel? = ...

if let label = label,
let response = response,
response.count > 0 {

}

This small problem led to reduction in the clarity of our codebase.

What is the benefit of if-let shorthand

Instead of encouraging short variable names, the new shorthand allows descriptive variable names while maintaining readability.

The new if-let shorthand lets us omit the right-hand expression, which makes our code shorter without losing its function.

let productListResponse: Array<Product>? = ...
let productNameLabel: UILabel? = ...

if let productNameLabel,
let productListResponse,
productListResponse.count > 0 {

}

The compiler automatically shadows the existing variable with the same name.

The above code is transformed into this by the compiler.

if let productNameLabel = productNameLabel,
let productListResponse = productListResponse,
productListResponse.count > 0 {

}

This new syntax applies to all places where we normally use optional binding.

if let foo { ... }
if var foo { ... }

else if let foo { ... }
else if var foo { ... }

guard let foo else { ... }
guard var foo else { ... }

while let foo { ... }
while var foo { ... }

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Conclusion

The new if-let shorthand is one of the small changes that I think can make a nice impact on our day-to-day coding habits.

You can read more detail about this change in the SE-0345 proposal.


  1. https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID333 ↩︎

  2. Variable shadowing occurs when a variable declared within a certain scope has the same name as a variable declared in an outer scope. This outer variable is said to be shadowed by the inner variable. ↩︎


Read more article about Swift, Swift 5.7, or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
How to change status bar color in SwiftUI

In SwiftUI, we have no direct way to change a status bar style. But we can indirectly control it through two view modifiers.

Next
How to change SwiftUI list background color

In iOS 16, we finally got a native way to change the background color of a list view in SwiftUI.

← Home