From 12b0b54c7e432c90e4720ce1945f39d005d91eb1 Mon Sep 17 00:00:00 2001 From: Cal Stephens Date: Fri, 31 Jul 2026 10:02:14 -0700 Subject: [PATCH 1/5] Add rule to prefer if expressions over ternary operators in single-expression return values --- README.md | 41 +++++++++++++++++++ .../AirbnbSwiftFormatTool/airbnb.swiftformat | 2 + 2 files changed, 43 insertions(+) diff --git a/README.md b/README.md index 2e89b32..68d05c1 100644 --- a/README.md +++ b/README.md @@ -1476,6 +1476,47 @@ _You can enable the following settings in Xcode by running [this script](https:/ +- (link) **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. + +
+ + + + [![SwiftFormat: ifExpressions](https://img.shields.io/badge/SwiftFormat-ifExpressions-7B0051.svg)](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. Use ternaries for conditions nested in other expressions, + // 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 + } + ``` + +
+ - (link) **Wrap `if` statement and `if` expression bodies onto multiple lines.**
diff --git a/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat b/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat index 41998e0..23232c1 100644 --- a/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat +++ b/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat @@ -46,6 +46,7 @@ --else-position same-line # elseOnSameLine --guard-else next-line # elseOnSameLine --single-line-for-each convert # preferForLoop +--single-line-ternary convert # ifExpressions --short-optionals always # typeSugar --semicolons never # semicolons --doc-comments preserve # docComments @@ -125,6 +126,7 @@ --rules sortTypealiases --rules preferForLoop --rules conditionalAssignment +--rules ifExpressions --rules wrapMultilineConditionalAssignment --rules wrapFunctionBodies --rules wrapPropertyBodies From 232ac07d7218c7abe81370f398761667a4ac5dd3 Mon Sep 17 00:00:00 2001 From: Cal Stephens Date: Fri, 31 Jul 2026 12:15:23 -0700 Subject: [PATCH 2/5] Update airbnb.swiftformat --- Sources/AirbnbSwiftFormatTool/airbnb.swiftformat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat b/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat index 23232c1..2174c2a 100644 --- a/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat +++ b/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat @@ -46,7 +46,7 @@ --else-position same-line # elseOnSameLine --guard-else next-line # elseOnSameLine --single-line-for-each convert # preferForLoop ---single-line-ternary convert # ifExpressions +--single-line-ternary preserve # ifExpressions --short-optionals always # typeSugar --semicolons never # semicolons --doc-comments preserve # docComments From f245a80cca8e1a96f7b3c8db013b2989ccc511e2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 3 Aug 2026 18:47:03 +0000 Subject: [PATCH 3/5] docs: add single-line ternary example --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 68d05c1..41db8de 100644 --- a/README.md +++ b/README.md @@ -1501,6 +1501,9 @@ _You can enable the following settings in Xcode by running [this script](https:/ } } + // ALSO RIGHT. Single-line ternaries are permitted. + var color: Color { spaceship.velocity > 0 ? .orange : .blue } + // ALSO RIGHT. Use ternaries for conditions nested in other expressions, // like SwiftUI modifier conditions. Image(.spaceship) From 84c82be3227d5dbf3555c40389be5c2cbdf50448 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 3 Aug 2026 18:48:58 +0000 Subject: [PATCH 4/5] docs: add why section for if expression rule --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 41db8de..d4e26d6 100644 --- a/README.md +++ b/README.md @@ -1484,6 +1484,10 @@ _You can enable the following settings in Xcode by running [this script](https:/ [![SwiftFormat: ifExpressions](https://img.shields.io/badge/SwiftFormat-ifExpressions-7B0051.svg)](https://swiftformat.info/rules/prerelease#ifExpressions) + #### Why? + + If expressions are more readable than ternary expressions, especially for multiple nested conditions or multi-line values. + ```swift // WRONG var destination: Planet { From 3be06c7a15794694579c4520503d9493d3275432 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 3 Aug 2026 18:50:38 +0000 Subject: [PATCH 5/5] docs: wrap single-line ternary computed var example --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d4e26d6..9be75d0 100644 --- a/README.md +++ b/README.md @@ -1506,7 +1506,9 @@ _You can enable the following settings in Xcode by running [this script](https:/ } // ALSO RIGHT. Single-line ternaries are permitted. - var color: Color { spaceship.velocity > 0 ? .orange : .blue } + var color: Color { + spaceship.velocity > 0 ? .orange : .blue + } // ALSO RIGHT. Use ternaries for conditions nested in other expressions, // like SwiftUI modifier conditions.