Back to all reviewers

Prefer safe optional handling

ghostty-org/ghostty
Based on 3 comments
Swift

Always use Swift's built-in mechanisms for safely handling optional values instead of force unwrapping or manual nil handling. Specifically: 1. Use optional binding (`if let`, `guard let`) instead of checking for nil and force unwrapping:

Null Handling Swift

Reviewer Prompt

Always use Swift’s built-in mechanisms for safely handling optional values instead of force unwrapping or manual nil handling. Specifically:

  1. Use optional binding (if let, guard let) instead of checking for nil and force unwrapping:
// Avoid this pattern
if savedWindowFrame != nil {
    let originalFrame = savedWindowFrame!  // Risky force unwrap
}

// Prefer this pattern
if let savedWindowFrame {
    let originalFrame = savedWindowFrame  // Safe access
}
  1. Use collection transformation methods like flatMap or compactMap to handle optional collections elegantly:
// Avoid this pattern
return controllers.reduce([]) { result, c in
    result + (c.surfaceTree.root?.leaves() ?? [])
}

// Prefer this pattern
return controllers.flatMap {
    $0.surfaceTree.root?.leaves() ?? []
}
  1. Always add appropriate guards to check if objects exist within expected hierarchies or collections before performing operations:
@IBAction func toggleMaximize(_ sender: Any) {
    guard let window = window else { return }
    guard surfaceTree.contains(sender) else { return }  // Check hierarchy membership
    // Implementation...
}

These patterns make code more concise, readable, and less prone to runtime crashes due to unexpected nil values.

3
Comments Analyzed
Swift
Primary Language
Null Handling
Category

Source Discussions