-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Guard #111
Comments
I think that since we already have the Golden Path rule, |
If guards are Swift 2's replacement to if let, shouldn't the optionals section be updated to reflect this? Correct me if I'm wrong, also I'm not sure what you mean by the Golden Path rule? |
@mattgabor It is not replacement, it is supplement. It give perfect sense use both. It is like |
@Lweek thanks for the clarification, that makes sense. I still think it should be included in the style guide, unless it's not something used on the website. |
With I'll use |
@mitchellporter This isn't strictly true. You can also use
|
@micpringle You're right that it's not strictly true, but to be fair unless you're in a loop you can't even use I guess I was assuming a scenario where you're using |
I think the general explanation is "it must exit the current scope" which includes my favourite way to do that: I don't know of a concise way to word this, but if the current structure of the code is an if-let with lots of code inside and nothing outside, then that should use Perhaps as Jack mentioned, a simple "guard rather than if-let is the preferred syntax if you need to exit early" is enough? |
Don't forget that Like I said above, and just like with a lot of Swift, using There are obviously cases where |
@gregheo Yep.
@micpringle I keep reminding myself of this every few weeks while working with Swift. Sometimes I notice myself subconsciously wanting to bend Swift to meet my Objective-C way of thinking, if that makes sense. Thank you both for the clarification on everything. |
👍 Yep, respect the Golden Path. :] If you use |
I think that using |
I am going to include a section on the happy path and guard in an upcoming update. (Not sure if it will be in the optionals section or not.) |
Hello!
Can we get some clarity around when
guard
should be preferred overif let
binding?I appreciate Swift 2.0 will (is?) only being released to the general public today, but I'm getting a lot of questions from the iOS team around this and would like it documented in the style guide so there's no ambiguity.
The text was updated successfully, but these errors were encountered: