Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->

[![SwiftFormat: ifExpressions](https://img.shields.io/badge/SwiftFormat-ifExpressions-7B0051.svg)](https://swiftformat.info/rules/prerelease#ifExpressions)

```swift

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot add a ### Why like other rules with: If expressions are more readable than ternary expressions, especially for multiple nested conditions or multi-line values.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 84c82be.

// 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot, wrap after the {, but the ternary itself can be on a single line.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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>
Expand Down
2 changes: 2 additions & 0 deletions Sources/AirbnbSwiftFormatTool/airbnb.swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
--else-position same-line # elseOnSameLine
--guard-else next-line # elseOnSameLine
--single-line-for-each convert # preferForLoop
--single-line-ternary preserve # ifExpressions
--short-optionals always # typeSugar
--semicolons never # semicolons
--doc-comments preserve # docComments
Expand Down Expand Up @@ -125,6 +126,7 @@
--rules sortTypealiases
--rules preferForLoop
--rules conditionalAssignment
--rules ifExpressions
--rules wrapMultilineConditionalAssignment
--rules wrapFunctionBodies
--rules wrapPropertyBodies
Expand Down