Sitemap
Bala Baskar Bollisetty

Bala Baskar Bollisetty

๐Ÿš€ Swift 5.9 Just Got Smarter: Writing Cleaner & More Expressive Code with If Expressions & Ternary Operator!

3 min readMar 7, 2025

--

Swift continues to evolve, introducing features that make code cleaner, more readable, and more expressive. Swift 5.9 improves conditional assignments with:
โœ… If expressions โ€” Allowing structured conditional assignments.
โœ… Enhanced ternary operator (? :) โ€“ Making short conditional expressions more powerful.

These updates make Swift 5.9 one of the most developer-friendly versions yet! ๐Ÿš€

In this post, weโ€™ll explore three ways to write conditional expressions in Swift:
โœ” Traditional If-Else Statements
โœ” The Ternary Operator (? :) (Improved in Swift 5.9)
โœ” The New If Expression

๐Ÿ”ฅ The Old Way: If-Else Statements

Before Swift 5.9, assigning values based on conditions required declaring a variable first and modifying it inside an if-else block:

let temperature: Int = 30
var weather: String
if temperature > 25 {
weather = "It's a hot day! โ˜€๏ธ"
} else {
weather = "It's a cool day! โ„๏ธ"
}

โŒ Issues with the Old Approach:

  • More lines of code than necessary.
  • Had to declare the variable first and mutate it later.
  • Assignments happened inside an if-else block.

๐Ÿš€ The New Way (Swift 5.9 โ€” If as an Expression)

With Swift 5.9, you can now assign values directly using if as an expression:

let temperature: Int = 30
let weather = if temperature > 25 {
"It's a hot day! โ˜€๏ธ"
} else {
"It's a cool day! โ„๏ธ"
}

โœ… Cleaner & more readable
โœ… No need for variable mutation
โœ… Allows multiline assignments in a structured way

This makes assignments more expressive while keeping the code structured.

๐Ÿท Swift 5.9 Enhanced Ternary Operator (? :)

Swift 5.9 also improves the ternary operator (? :), making short conditional expressions more powerful:

let temperature: Int = 30
let weather = temperature > 25 ? "It's a hot day! โ˜€๏ธ" : "It's a cool day! โ„๏ธ"

โœ… Advantages of the Improved Ternary Operator:

โœ” More readable โ€” Better handling of conditional expressions.
โœ” Shorter & expressive โ€” Reduces unnecessary lines of code.
โœ” More efficient โ€” Works seamlessly with other Swift 5.9 enhancements.

Get Bala Baskar Bollisettyโ€™s stories inย yourย inbox

Join Medium for free to get updates fromย thisย writer.

Now, you can use both if expressions and the ternary operator for cleaner, more expressive code! ๐Ÿš€

๐ŸŽฏ Comparing All Three Approaches

Letโ€™s see another example comparing if-else, the ternary operator, and the new if expression side by side:

Before Swift 5.9: Using If-Else Statement

let age = 20
var category: String
if age >= 18 {
category = "Adult"
} else {
category = "Minor"
}

Using the Improved Ternary Operator in Swift 5.9

let age = 20
let category = age >= 18 ? "Adult" : "Minor"

Now in Swift 5.9: If as an Expression

let age = 20
let category = if age >= 18 {
"Adult"
} else {
"Minor"
}

โœ… Why Swift 5.9 Makes Code Cleaner

โœ” Cleaner & more readable โ€” improves clarity over traditional if-else.
โœ” No need for variable mutation โ€” eliminates unnecessary var.
โœ” More expressive Swift code โ€“ allows both short and structured conditional assignments.
โœ” Swift 5.9 gives you the choice โ€“ use either if expressions or the ternary operator (? :) based on readability.

๐Ÿ’ก When to Use Each Approach?

Approach Pros Cons If-Else Statement Easy to understand, widely used More lines of code, requires variable mutation Ternary Operator (? :) Short, concise, optimized in Swift 5.9 Hard to read with complex conditions If as an Expression (Swift 5.9) Clean, readable, structured Not supported in older Swift versions

Best Use Cases:

โœ” Use the ternary operator (? :) for simple conditions when readability is not affected.
โœ” Use if as an expression when you need a structured, multiline conditional assignment.
โœ” Use traditional if-else when performing multiple actions instead of a single assignment.

๐Ÿ’ก Why Does This Matter?

Swift is constantly evolving to improve the developer experience. With Swift 5.9, developers get:
๐Ÿ”น More concise code โ€” Fewer lines, better readability.
๐Ÿ”น Less error-prone logic โ€” Reduces unnecessary variable mutations.
๐Ÿ”น Better maintainability โ€” Simplifies logic and enhances clarity.

If you want cleaner, more expressive Swift code, Swift 5.9โ€™s If expressions and improved Ternary Operator (? :) are game changers! ๐Ÿš€

๐Ÿ”— Ready to Try It?

Have you used these features yet? What do you think about if expressions vs. ternary operators? Letโ€™s discuss in the comments! ๐Ÿ’ฌ๐Ÿ‘‡

#Swift #Swift5 #Swift5.9 #Xcode #iOSDevelopment #CleanCode #SwiftTips #iOS

--

--