-
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
Golden Path #63
Comments
I would agree here but what about optional binding? Sometimes there are a big mess of I suppose we could replace that with many checks for |
Checking for nil is definitely going against swift in my opinion. Then when you're done you're left to use forced unwrapping which feels even worse. — On Tue, Dec 2, 2014 at 6:54 PM, Greg Heo notifications@github.com wrote:
|
I think the scenario we should discourage is nesting a big mess of E.g.
Seems bad. Instead, I propose this is cleaner:
|
Hm - I worry about that second approach - it seems more error-prone than nested if lets. What if someone adds a |
If they added another Maybe there isn't a good solution to this issue, and it should be addressed on a case by case basis? For example, start by questioning, "Why do I have four optionals here...?" Unless others have a better suggestion on how to address this, perhaps this should simply be closed...? |
I'd worry a little bit too, especially if there might be "in between" work to do, e.g.: if let object1= optional1 {
// do something with object1 here
if let object2 = optional2 {
if let object3 = optional3 {
// do something with objects 1,2,3
}
} I guess you could split that up into two if blocks, one for just object1 and another for all of 1,2,3. I feel like Swift is going to get multiple bindings some day, like this: if let object1 = optional1 && object2 = optional2 {
} In that case, I'd have to argue against the usual golden path and say it's an established Swift idiom to have the "happy path" inside the conditional. |
Closing this issue as the group seems to agree that the Golden Path has undesirable consequences in Swift. As @gregheo mentions, hopefully Swift will have multiple :] |
With the introduction of I think many agree this is a good idea in Swift now and are already following it in their writing. |
Also, multiple |
I agree. Example from above in Swift 2.0 should look more like this:
|
Approved. I will be adding a section about golden path and guard in the style guide. @lukewar I like your example but ... that semicolon ... 😆 |
lol, jumping between |
How about dropping the
|
I'm against dropping the Reasoning: (1) If you include a Not Allowed:
(2) If you have a mix of Potentially Unclear:
I'd propose:
|
Use of var is deprecated (and will be an error in Swift 3) so that argument is out. Also, if you order correctly you can omit the let with where. func doSomethingWithOptionalNumber(number1: Int?, number2: Int?) {
guard let number1 = number1, number2 = number2 where number1 > number2
else { // COMPILER ERROR
print("Number criteria not met!")
return
}
// ... here be magic...
} That said, I understand that a style guide shouldn't be a straight jacket. While I may elide the |
@rayfix Admittedly, these were contrived examples. 😉 However, you definitely won't always be able to reorder to get rid of the necessity for the second Regarding,
You can only have 😱 😱 🙀 Jokes aside though, only
|
👍 |
To steal from our Objective-C style guide, I propose the inclusion of the Golden Path rule in this guide:
Golden Path
When coding with conditionals, the left hand margin of the code should be the "golden" or "happy" path. That is, don't nest
if
statements. Multiple return statements are OK.Preferred:
Not Preferred:
The text was updated successfully, but these errors were encountered: