-
Notifications
You must be signed in to change notification settings - Fork 351
Add rule to prefer if expressions over ternary operators in single-expression return values #399
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
Changes from 3 commits
12b0b54
232ac07
f245a80
84c82be
3be06c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1476,6 +1476,50 @@ _You can enable the following settings in Xcode by running [this script](https:/ | |
|
|
||
| </details> | ||
|
|
||
| - <a id='prefer-if-expressions-over-ternary-operators'></a>(<a href='#prefer-if-expressions-over-ternary-operators'>link</a>) **Prefer `if` expressions over ternary operators for single-expression return values.** Use ternary operators for conditions nested in other expressions, such as SwiftUI modifier conditions. Generally prefer `if` expressions for assignments after `=` operators. | ||
|
|
||
| <details> | ||
|
|
||
| <!-- ai-skill-include: autocorrect doesn't apply to conditional assignments after `=`, but this is still a good best practice --> | ||
|
|
||
| [](https://swiftformat.info/rules/prerelease#ifExpressions) | ||
|
|
||
| ```swift | ||
| // WRONG | ||
| var destination: Planet { | ||
| spaceship.hasWarpDrive | ||
| ? .proximaCentauri | ||
| : .mars | ||
| } | ||
|
|
||
| // RIGHT | ||
| var destination: Planet { | ||
| if spaceship.hasWarpDrive { | ||
| .proximaCentauri | ||
| } else { | ||
| .mars | ||
| } | ||
| } | ||
|
|
||
| // ALSO RIGHT. Single-line ternaries are permitted. | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot, wrap after the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 3be06c7. |
||
| var color: Color { spaceship.velocity > 0 ? .orange : .blue } | ||
|
|
||
| // ALSO RIGHT. Use ternaries for conditions nested in other expressions, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot, please add an "ALSO RIGHT" for a computed var ternary where the ternary is on a single line rather than wrapped. Single-line ternaries are permitted. Use the color example.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in f245a80. |
||
| // like SwiftUI modifier conditions. | ||
| Image(.spaceship) | ||
| .foregroundStyle(spaceship.velocity > 0 ? .orange : .blue) | ||
|
|
||
| // PREFERRED. `if` expressions are also generally preferred over ternaries for assignments. | ||
| let destination: Planet = | ||
| if spaceship.hasWarpDrive { | ||
| .proximaCentauri | ||
| } else { | ||
| .mars | ||
| } | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| - <a id='wrap-if-statement-bodies'></a>(<a href='#wrap-if-statement-bodies'>link</a>) **Wrap `if` statement and `if` expression bodies onto multiple lines.** | ||
|
|
||
| <details> | ||
|
|
||
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.
@copilot add a
### Whylike other rules with: If expressions are more readable than ternary expressions, especially for multiple nested conditions or multi-line values.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.
Addressed in 84c82be.