<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I agree - but this isn’t the case i covered.<div class="">This is the trivial case of an expression</div><div class=""><br class=""></div><div class="">condition ? value1 : value2</div><div class=""><br class=""></div><div class="">which shouldn’t be done using blocks (my example was really an elaboration function)</div><div class=""><br class=""></div><div class="">i know the ternary operator is nice in this case, but isn’t the following just as easy to write and understand ?</div><div class="">it’s certainly easier to add comments - i know it’s a little longer, but … it’s a style thing i guess</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> uriEncode(string: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>, encodeSlash: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> encodedString = <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">""</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> char <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> string.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">characters</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> (char >= <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"A"</span> && char <= <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"z"</span>) || (char >= <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"a"</span> && char <= <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"z"</span>) || (char >= <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"0"</span> && char <= <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"9"</span>) || char == <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"_"</span> || char == <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"-"</span> || char == <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"~"</span> || char == <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"."</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> encodedString += <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"</span>\<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span>char<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">)"</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> char == <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"/"</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> encodeSlash {</div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class=""><span style="font-size: 11px;" class=""> encodedString += </span></font><font color="#d12f1b" face="Menlo" class=""><span style="font-size: 11px;" class="">"%2F” // URL Encoding for Forward Slash</span></font></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> } <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> encodedString += <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"</span>\<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span>char<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">)"</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> literal = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>(char)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> byte <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> literal.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">utf8</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> encodedString += <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>(format: <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"%%2X"</span>, arguments: [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>(byte)])</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> encodedString</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class="">however, if you replace the ternary operator with *anything* we’re asking for someone to come along with and change existing multilayered ?: into the new form.</div><div class=""><br class=""></div><div class="">Such as</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> (heapAlign < valueAlign</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> ? (valueAlign < elementAlign ? elementAlign : valueAlign)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> : (heapAlign < elementAlign ? elementAlign : heapAlign))</div></div><div class=""><br class=""></div><div class="">to</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> heapAlign < valueAlign</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> valueAlign < elementAlign (elementAlign) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span> (valueAlign))</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>else</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> heapAlign < elementAlign (elementAlign) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span> (heapAlign))</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">or</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div class=""><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">return</span></div><div class=""><span style="font-family: Menlo; font-size: 11px;" class=""> if</span><span style="font-family: Menlo; font-size: 11px;" class=""> heapAlign < valueAlign (</span></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> valueAlign < elementAlign (</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> elementAlign</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> ) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span> (</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> valueAlign</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> )</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> ) <span style="color: rgb(187, 44, 162);" class="">else </span>(</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class=""> if</span> heapAlign < elementAlign (</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> elementAlign</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> ) <span style="color: rgb(187, 44, 162);" class="">else</span> (</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> heapAlign</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> )</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> )</div></div><div class=""><br class=""></div><div class="">which really doesn’t help - it’s actually easier read as:</div><div class=""><br class=""></div><div class=""><div class=""><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">return {</span></div><div class=""><span style="font-family: Menlo; font-size: 11px;" class=""> if</span><span style="font-family: Menlo; font-size: 11px;" class=""> heapAlign < valueAlign (</span></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color: rgb(187, 44, 162);" class="">if</span> valueAlign >= elementAlign (</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> return valueAlign</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> )</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> ) <span style="color: rgb(187, 44, 162);" class="">else </span>(</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class=""> if</span> heapAlign >= elementAlign (</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> return heapAlign</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> )</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> )</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> return elementAlign</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div></div></div><div class="">Use inline blocks for complex logic and the existing else if for trivial cases - even on one or two lines</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> width : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>highDef<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span>width<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1920</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">} </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span>width<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">720</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">}</span></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162);" class="">if</span> encodeSlash {<font face="Menlo" style="font-size: 12px;" class=""><span style="font-size: 11px;" class="">encodedString += </span></font><font color="#d12f1b" face="Menlo" style="font-size: 12px;" class=""><span style="font-size: 11px;" class="">"%2F”</span></font>} </div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162);" class="">else</span> {encodedString += <span style="color: rgb(209, 47, 27);" class="">"</span>\<span style="color: rgb(209, 47, 27);" class="">(</span>char<span style="color: rgb(209, 47, 27);" class="">)"</span>}</div><div class=""><br class=""></div></div><div class="">ABR.</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 12 Dec 2015, at 01:31, Drew Crawford via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Strong -1, for all the reasons Andrey gave, although I think his point could benefit from examples.<br class=""><br class="">Consider this case from my codebase, for which ?: is natural:<br class=""><br class=""> ///<a href="http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html" class="">http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html</a><br class=""> func uriEncode(string: String, encodeSlash: Bool) -> String {<br class=""> var encodedString = ""<br class=""> for char in string.characters {<br class=""> if (char >= "A" && char <= "z") || (char >= "a" && char <= "z") || (char >= "0" && char <= "9") || char == "_" || char == "-" || char == "~" || char == "." {<br class=""> encodedString += "\(char)"<br class=""> }<br class=""> else if char == "/" {<br class=""> encodedString += encodeSlash ? "%2F" : "\(char)"<br class=""> }<br class=""> else {<br class=""> let literal = String(char)<br class=""> for byte in literal.utf8 {<br class=""> encodedString += String(format: "%%2X", arguments: [Int(byte)])<br class=""> }<br class=""> }<br class=""> }<br class=""> return encodedString<br class=""> }<br class=""><br class="">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.<br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><blockquote type="cite" class="">On Dec 11, 2015, at 7:18 PM, Andrey Tarantsov via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class="">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.<br class=""><br class="">A.<br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></div></body></html>