If Else vs. Switch: The Surprising Winner

In the world of SwiftUI development, a recurring question often surfaces: Should we use If-Else statements or Switch statements? This discussion aims to shed light on the topic and guide you through the decision-making process. We'll delve into the subtleties of each approach, from managing conditional logic to enhancing readability and maintainability. So, put on your coding hat, and let's dive into the complexities of choosing between If Else and Switch statements in SwiftUI to ensure your code is as sleek as your UI.

If you're grappling with the tutorial blues, I'm here to help. As an experienced SwiftUI developer, I'm ready to guide you. Visit rebeloper.com/mentoring. Schedule a call with me now.

Today, we're going to discuss handling different cases within our views, using a demo project I've created.

If else statement

Let's start by setting up a condition, say, 'is on.' We'll have a state private var 'is on' and set the default value to false.

Now, suppose we want to display a text 'is on' if the state variable 'is on' is true, and 'is off' if it's false. The most straightforward and intuitive way to do this is the If-Else statement.

Switch statement

However, this approach might seem a bit off. What if the state variable is not just a simple Boolean? What if we could have multiple cases? That's when a switch statement comes into play. In our switch statement, we'll have cases for both true and false.

Now, you might think using a switch statement for a simple Boolean is overkill, and I agree. But consider this: what if the state variable were not a Boolean? In such cases, the switch approach makes your code much more readable. If you're using If-Else statements in some cases and switch statements in others, your code loses its consistency. For this reason, I advocate for the switch approach.

Ternary operator

However, there's a caveat. If you find yourself repeating the same text in your code, you're not following good practice. Luckily, there's a better way: the ternary operator. This operator is perfect for our situation because we're dealing with simple text, not different view types.

The ternary operator allows us to write a single line of code that's easier to read and less redundant. We can even improve on this by only changing the 'on' and 'off.' Instead of writing the same text repeatedly, we use the ternary operator to decide whether it should be 'on' or 'off.'

Conclusion

While the ternary operator works well for simple text, I would recommend using the switch statement for different view types. If you're not going to use many switch statements in your code, then the if statement will suffice.

Ultimately, the choice between 'if else vs switch' depends on the coder and the specific code at hand.

ARTICLES I RECOMMEND

Revolutionize App Development: Integrate AI Code Completion into Xcode

GitHub Copilot in Xcode: The FUTURE of Coding

ChatGPT in Xcode: The Ultimate Developer's Toolkit

I made an App UI with ChatGPT

How to use ChatGPT to Write Code