-
Notifications
You must be signed in to change notification settings - Fork 312
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
Add rule to prefer using if/switch expressions when initializing a new property with the result of a conditional statement #250
Conversation
3b9dbdf
to
7f1041a
Compare
Maybe this is just my bias here, but I'm not in favor of this change. I tend to follow the mantra of "better to be clear than clever". In my opinion based on the examples above, the code produced in this new pattern is much harder to understand. Maybe some developers will prefer this new style, and that is ok, but I don't think we should lint away the old approach which has merit. |
@jpsoultanis, interested to hear which aspects of the pattern you find harder to understand. You may have noticed this confusing code in the PR body a few minutes ago: var planetLocation = planet.star {
"The \(star.name) system"
} else {
"Rogue planet"
} this was a typo and was not valid Swift, it was supposed to be: var planetLocation = if let star = planet.star {
"The \(star.name) system"
} else {
"Rogue planet"
} I just updated that example and it is now correct. |
I see, that addresses my own confusion. I still think the "wrong" approach is more explicit and clear, but this may be just because I am used to it. If I had to articulate it, the issue is that I've trained my brain to scan and when I see an |
I think I understand what you mean. I think there is some natural tension with how this formatting defies our existing expectations of how an if / switch statement looks: let planetLocation = if let star = planet.star {
"The \(star.name) system"
} else {
"Rogue planet"
} I'm curious to hear your thoughts about this alternative formatting, where we insert a line break before the let planetLocation =
if let star = planet.star {
"The \(star.name) system"
} else {
"Rogue planet"
} This is the formatting used in most of the examples in the Swift Evolution proposal for this feature, and has two improvements over the other formatting:
Curious how your thoughts on this formatting compare to formatting used in the examples in the PR body @jpsoultanis. Regardless of whether or not we merge this PR, it would make sense to choose a preferred formatting for if / switch expressions. Even if we don't merge this PR, engineers will be permitted to write code using this functionality (unless we specifically disallow it). |
I actually like the Swift 5.9 syntax. The most convincing argument in favor of the new syntax relates to the type system:
However, I'm really used to the pre-5.9 way of doing things (especially when I'm reading code). I'd prefer to gradually introduce this new syntax, rather than putting it in the style guide as the preferred way of doing things right now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am glad to see the engagement on this PR!
@calda , I am going to hold off on approving this PR given that there is ongoing discussion. If you feel like we should move ahead with this proposal, please tag me and I can review again with the aim of approving.
I am in favor of this proposal. Related to this discussion, though, I would prefer the alternate syntax for the reasons you outlined @calda .
let planetLocation =
if let star = planet.star {
"The \(star.name) system"
} else {
"Rogue planet"
}
I am personally in favor of the "less is more" approach to explicit types in Swift, so I like how this new Swift 5.9 syntax can avoid the need to provide an explicit type in a variable declaration. Related to what @nmkel999 quoted, I find that not specifying types in code makes it easier to perform sweeping refactors.
I also love that this new syntax causes the compiler to diagnose that let
can be used instead of var
.
README.md
Outdated
@@ -901,6 +901,111 @@ _You can enable the following settings in Xcode by running [this script](resourc | |||
} | |||
``` | |||
|
|||
* <a id='conditional-assignment'></a>(<a href='#conditional-assignment'>link</a>) **When initializing a new property with the result of a conditional statement (e.g. an if or switch statement), use a single if/switch expression where possible** rather than definining an uninitialized property and initializing it on every branch of the following conditional statement. [![SwiftFormat: conditionalAssignment](https://img.shields.io/badge/SwiftFormat-conditionalAssignment-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#conditionalAssignment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we can have a more specific/descriptive slug than conditional-assignment
but I don't feel strongly that we need to make a change.
@calda I much prefer the syntax you mentioned with the line break: let planetLocation =
if let star = planet.star {
"The \(star.name) system"
} else {
"Rogue planet"
} Makes it much more clear at-a-glance that we are performing a variable assignment. Thanks for your work on this 😄 |
#253 adopts the formatting we discussed above, where we insert a line break after the assignment operator. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am approving since I believe that there is general alignment on moving ahead with this proposal. Thanks @calda !
let actualLocaton = galaxy.name ?? "the universe" | ||
planetLocation = "Rogue planet somewhere in \(actualLocation)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Co-authored-by: Michael Bachand <michael.bachand@airbnb.com>
Co-authored-by: Michael Bachand <michael.bachand@airbnb.com>
18d4cae
to
24c0b97
Compare
Summary
This PR proposes a new rule to prefer using if/switch expressions when initializing a new property with the result of a conditional statement.
Support for this syntax was added in Swift 5.9, and autocorrect is implemented via the conditionalAssignment SwiftFormat rule.
Reasoning
There are several benefits to using an if/switch expression over simply performing assignment on each branch of the following conditional statement:
var
is unnecessary.Examples
Please react with 👍/👎 if you agree or disagree with this proposal.