<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=""><div class="">Counter-proposal that eliminates the nested ifs.</div><div class=""><br class=""></div>If I have functions with long if blocks, I typically use switches on named tuples.<div class=""><br class=""></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="">switch</span>&nbsp;(condition1:&nbsp;..., condition2: ...) {</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">case</span>&nbsp;(condition1:&nbsp;<span style="color: rgb(187, 44, 162);" class="">false</span>, condition2:&nbsp;<span style="color: rgb(187, 44, 162);" class="">true</span>):</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; ...</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="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">case</span>&nbsp;(condition1:&nbsp;<span style="color: rgb(187, 44, 162);" class="">_</span>, condition2:&nbsp;<span style="color: rgb(187, 44, 162);" class="">false</span>):</div><div class=""><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp; &nbsp; &nbsp; &nbsp; ...</span></div><div class=""><span style="font-family: Menlo; font-size: 11px;" class=""><br class=""></span></div><div class=""><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">case</span><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp;</span><span style="font-family: Menlo; font-size: 11px;" class="">(condition1:</span><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp;</span><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">true</span><span style="font-family: Menlo; font-size: 11px;" class="">, condition2:</span><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp;</span><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">true</span><span style="font-family: Menlo; font-size: 11px;" class="">)&nbsp;</span><span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">where&nbsp;</span><span style="font-family: Menlo; font-size: 11px;" class="">condition3</span><span style="font-family: Menlo; font-size: 11px;" class="">:</span></div><div class=""><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp; &nbsp; &nbsp; &nbsp; ...</span></div><div class=""><span style="font-family: Menlo; font-size: 11px;" class=""><br class=""></span></div><div class=""><div class=""><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">case</span><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp;</span><span style="font-family: Menlo; font-size: 11px;" class="">(condition1:</span><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp;</span><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">true</span><span style="font-family: Menlo; font-size: 11px;" class="">, condition2:</span><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp;</span><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">true</span><span style="font-family: Menlo; font-size: 11px;" class="">)</span><span style="font-family: Menlo; font-size: 11px;" class="">:</span></div><div class=""><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp; &nbsp; &nbsp; &nbsp; ...</span></div></div><div class=""><span style="font-family: Menlo; font-size: 11px;" class="">}</span></div><div class=""><br class=""></div><div class="">This way, you are reminded after each long block about your current scope, and the compiler checks for unhandled cases.</div><div class=""><br class=""></div><div class="">Etan</div><div class=""><br class=""></div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On 12 Dec 2015, at 22:35, Greg Parker via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 12, 2015, at 5:41 AM, Al Skipp via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 12 Dec 2015, at 13:31, Marc Knaup via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">I'm also against any kind of markers to annotate the end of a block.</span><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""><div class="">If the code inside a if/guard/loop/do/func/var/… block is so long that the developer thinks about annotating the closing curly brace then they should instead break it down into smaller pieces. Move parts into separate functions and replace it with calls to these functions.</div><div class="">This makes the block shorter, much easier to understand and the function names will aid as additional documentation.</div></div></div></blockquote></div><br class=""><div class="">I completely agree.&nbsp;</div><div class=""><br class=""></div><div class="">If code becomes so impenetrable that you need to annotate the end of blocks to assist comprehension, then the code needs refactoring.</div></div></div></blockquote><br class=""></div><div class="">Or a better editor. Modern code editors have affordances to improve readability of such code, such as highlighting matching braces, coloring nested scopes, or folding and unfolding scopes.</div><div class=""><br class=""></div><div class="">One advantage of the editor-based approaches is that they don't make editing more difficult. You don't need to write any scope annotations and you don't need to keep them up to date as the code changes.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">--&nbsp;</div><div class="">Greg Parker &nbsp; &nbsp; <a href="mailto:gparker@apple.com" class="">gparker@apple.com</a>&nbsp; &nbsp; &nbsp;Runtime Wrangler</div><div class=""><br class=""></div><br class="">
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=tTTJ5sn5y0uc3ODSZa-2BndLNwXCDS7T2cq5OlDDhG0Rsy1Rfl-2F2nC7NpxylSH-2Flg4L34JAyM2h5gKACTYteuNZjgzFkOJErGPAz1KFQTfA5gHDYbANUskVGjcZcBEoEeFiw0xJFSRSbc-2BOvEjHSxkhHW1iKRr-2FDtwvPdiuA09jcY6863lbQ5KAcYIgi31tpFf3gF6rZNtE39Rgx-2Fo8VjFT98MHe-2BjD4Q8YN9N-2B0zxIC0-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>
_______________________________________________<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></blockquote></div><br class=""></body></html>