ios-guidelines icon indicating copy to clipboard operation
ios-guidelines copied to clipboard

Ternary Operator and Avoiding Long-complex Expressions

Open canberkozcelik opened this issue 4 years ago • 1 comments

First of all, nice documentation 👍
Just have one suggestion if you like to check over:
Ternary Operator usage in Patterns topic, relating it to build time problems making usage of ternary a little ambiguous.
Same for nil coalescing operator, I suggest separating "avoiding long-complex expression usage" with those two.
Example:

let top: UIView? = UIView()  
let bottom: UIView? = UIView()  
let width: CGFloat = 10  
let height: CGFloat = 10. 
// WRONG (Complex expression)
let size = CGSize(width: width, height: height + (top?.bounds.height ?? 0) + (bottom?.bounds.height ?? 0) + 33). 
let top: UIView? = UIView()  
let bottom: UIView? = UIView()  
let width: CGFloat = 10  
let height: CGFloat = 10  
// SEPARATION (Complex expression)  
let firstExp = top?.bounds.height ?? 0 + height  
let secondExp = bottom?.bounds.height ?? 0 + 33 
let size = CGSize(width: width, height: firstExp + secondExp). 

Thanks :)

canberkozcelik avatar Oct 24 '20 14:10 canberkozcelik

Hi! You are right, we faced with this situation that's why we are using -warn-long-expression-type-checking swift flag. We should update the ternary operator usage in Patterns topic.

Thanks for this contribution :)

aniltaskiran avatar Nov 06 '20 13:11 aniltaskiran