๐ Swift 5.9 Just Got Smarter: Writing Cleaner & More Expressive Code with If Expressions & Ternary Operator!
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: Stringif 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-elseblock.
๐ 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: Stringif 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