Void Function In Ternary

ID

swift.void_function_ternary

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Swift

Tags

code-smell, idiomatic

Description

Reports a ternary expression at the top of an expression statement where both branches are function calls. The ternary operator selects a value; using it to dispatch side effects is anti-idiomatic and hard to scan.

cond ? doA() : doB()                       // FLAW

if cond { doA() } else { doB() }           // OK
let value = cond ? a : b                   // OK — value selection

Rationale

The ternary’s purpose is to pick between two values. Stretching it to "pick which void function to run" loses the readability that made the ternary attractive in the first place — readers have to parse it for control flow even though the syntax says "pure expression." if/else says "decide and run" up front.

Remediation

Rewrite as if cond { …​ } else { …​ }:

if cond {
    doA()
} else {
    doB()
}