Skip to content

Add rule to prefer if expressions over ternary operators in single-expression return values - #399

Merged
calda merged 5 commits into
masterfrom
cal--if-expressions
Aug 6, 2026
Merged

Add rule to prefer if expressions over ternary operators in single-expression return values#399
calda merged 5 commits into
masterfrom
cal--if-expressions

Conversation

@calda

@calda calda commented Jul 31, 2026

Copy link
Copy Markdown
Member

Summary

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.

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

Reasoning

If expressions are easier to read than ternary operators, especially when nesting multiple conditions. If expressions are more modern and support more functionality, like if let conditions.

@calda
calda force-pushed the cal--if-expressions branch from 72b12c4 to 12b0b54 Compare July 31, 2026 17:23
Comment thread README.md
}
}

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

Comment thread README.md

[![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.

Comment thread README.md
}
}

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

@calda
calda merged commit 78a999a into master Aug 6, 2026
6 checks passed
@calda
calda deleted the cal--if-expressions branch August 6, 2026 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants