[swift-evolution] ternary operator ?: suggestion
Drew Crawford
drew at sealedabstract.com
Fri Dec 11 19:31:14 CST 2015
Strong -1, for all the reasons Andrey gave, although I think his point could benefit from examples.
Consider this case from my codebase, for which ?: is natural:
///http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html
func uriEncode(string: String, encodeSlash: Bool) -> String {
var encodedString = ""
for char in string.characters {
if (char >= "A" && char <= "z") || (char >= "a" && char <= "z") || (char >= "0" && char <= "9") || char == "_" || char == "-" || char == "~" || char == "." {
encodedString += "\(char)"
}
else if char == "/" {
encodedString += encodeSlash ? "%2F" : "\(char)"
}
else {
let literal = String(char)
for byte in literal.utf8 {
encodedString += String(format: "%%2X", arguments: [Int(byte)])
}
}
}
return encodedString
}
I think replacing with `let foo` and scopes lengthens, emphasizes, and draws attention to a very minor detail, de-emphaizing and obscuring the main idea of the function.
> On Dec 11, 2015, at 7:18 PM, Andrey Tarantsov via swift-evolution <swift-evolution at swift.org> wrote:
>
> Strong -1; this turns a quick one-liner into a multiline monstrosity. When massaging coordinates and layouts in iOS apps, ?: is often helpful to handle corner cases inline.
>
> A.
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list