<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Fri, May 6, 2016 at 10:50 AM Kyle Lahnakoski <<a href="mailto:klahnakoski@mozilla.com">klahnakoski@mozilla.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div bgcolor="#FFFFFF" text="#000000">
<br>
<div><span style="line-height:1.5">Of course, a couple `try` nested statements make the indentation
worse. For example, we wish to ignore problems in dealing with
todo items, like above, but the todo items are complicated; each
is a dict() of details. Failure to deal with one of the details
is ignored, but the rest of the details are still attempted:</span></div></div><div bgcolor="#FFFFFF" text="#000000"><pre><code>def process_todo(todo):
____pre_process()
____for t in todo:
________try:
____________for u, v in t.items():
________________try:
____________________process()
________________except Exception, e:
____________________continue
________except Exception, e:
____________break
____post_process()</code></pre></div></blockquote><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">The refactoring that first comes to mind is to make the inner portion a separate function:</span></div><div><span style="line-height:1.5"><br></span></div><div><div> def process_one(thing):</div><div> for u, v in thing.items():</div><div> try:</div><div> process()</div><div> except Exception as e:</div><div> continue</div><div><br></div><div> def process_group(group):</div><div> pre_process()</div><div> for thing in group:</div><div> try:</div><div> process_one(thing)</div><div> except Exception as e:</div><div> break</div><div> post_process()</div></div><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">This avoids the excessive indentation and helps improve the reading of how an error in the group will break but an error in one will continue. Does that not satisfy?</span></div></div></div>