Swift: Replace the ternary conditional operator with `if`/`then`/`else`

Originator:rix.rob
Number:rdar://23012130 Date Originated:07-Oct-2015 12:57 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode 7.0.1 (7A1001)
Classification:Enhancement Reproducible:Always
 
Summary:
The ternary operator is hard to read. The condition is introduced without any fanfare, and the consequences are separated by punctuation that bears little relation to its intent.

I suggest replacing it with an `if`/`then`/`else` expression which enjoys the same semantics, i.e. any time you have this:

	x ? y : z

you can replace it with this much clearer expression:

	if x then y else z

This also formats well over multiple lines, introducing each term with a keyword which explains its purpose quite clearly:

	if condition
		then consequence
		else alternative

Note that this suggestion does _not_ add Ruby-esque postfix conditions:

	doSomething() if condition

IMO, in the general case, that form suffers the same sort of problem that makes the ternary conditional operator hard to read.


Steps to Reproduce:
N/A

Expected Results:
N/A

Actual Results:
N/A

Regression:
N/A

Notes:
N/A

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!