<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Clean Code: Nested conditionals</title>
	<atom:link href="http://www.danhulton.com/blog/2009/05/12/cleaning-up-your-code-nested-conditionals/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.danhulton.com/blog/2009/05/12/cleaning-up-your-code-nested-conditionals/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=cleaning-up-your-code-nested-conditionals</link>
	<description>Coffee, code, contemplation.</description>
	<lastBuildDate>Fri, 27 Aug 2010 20:32:15 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Dan Hulton</title>
		<link>http://www.danhulton.com/blog/2009/05/12/cleaning-up-your-code-nested-conditionals/comment-page-1/#comment-104</link>
		<dc:creator>Dan Hulton</dc:creator>
		<pubDate>Wed, 13 May 2009 15:27:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.danhulton.com/blog/?p=65#comment-104</guid>
		<description>Geoff - I use this frequently when I need to have multiple steps in a transaction and if any of them fail, just roll the whole damn thing back.  There&#039;s no point in executing any further steps if any of the previous steps fail.  I suppose in the future, using actual code examples instead of highly theoretical ones is a good idea.

And I rather like your last suggestion in the first post, chaining them all together on one line - much cleaner.

I hate having these giant pyramids in my code, and my solution is - at least to my eyes - much cleaner.  That said, yours is cleaner still.

Avid - check out &quot;short-circuiting&quot;.  You&#039;ll find (at least in PHP, where I do the majority of my coding (and I should have made that explicit in the article)) that if do_chunk_a() returns with false, none of the other functions will be evaluated as the expression is already guaranteed to return false.</description>
		<content:encoded><![CDATA[<p>Geoff &#8211; I use this frequently when I need to have multiple steps in a transaction and if any of them fail, just roll the whole damn thing back.  There&#8217;s no point in executing any further steps if any of the previous steps fail.  I suppose in the future, using actual code examples instead of highly theoretical ones is a good idea.</p>
<p>And I rather like your last suggestion in the first post, chaining them all together on one line &#8211; much cleaner.</p>
<p>I hate having these giant pyramids in my code, and my solution is &#8211; at least to my eyes &#8211; much cleaner.  That said, yours is cleaner still.</p>
<p>Avid &#8211; check out &#8220;short-circuiting&#8221;.  You&#8217;ll find (at least in PHP, where I do the majority of my coding (and I should have made that explicit in the article)) that if do_chunk_a() returns with false, none of the other functions will be evaluated as the expression is already guaranteed to return false.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AvidElite</title>
		<link>http://www.danhulton.com/blog/2009/05/12/cleaning-up-your-code-nested-conditionals/comment-page-1/#comment-103</link>
		<dc:creator>AvidElite</dc:creator>
		<pubDate>Wed, 13 May 2009 14:17:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.danhulton.com/blog/?p=65#comment-103</guid>
		<description>Your first example implies that B and C will only execute if A succeeds, and C will only execute if A and B succeed, whereas your &quot;solution&quot; will execute all three regardless of the outcome of the others.  I would have done this:

$success=true;
if ($success) { $success=A(); }
if ($success) { $success=B(); }
if ($success) { $success=C(); }

Or, because they&#039;re being split up, I&#039;d want to report to the user on these failures.  So:

$success=true;
if ($success) { $success=A(); }
if ($success) { $success=B(); } else { errorA(); }
if ($success) { $success=C(); } else { errorB(); }
if (!$success) { errorC(); }

But I think a little nesting would be easier to read then that.</description>
		<content:encoded><![CDATA[<p>Your first example implies that B and C will only execute if A succeeds, and C will only execute if A and B succeed, whereas your &#8220;solution&#8221; will execute all three regardless of the outcome of the others.  I would have done this:</p>
<p>$success=true;<br />
if ($success) { $success=A(); }<br />
if ($success) { $success=B(); }<br />
if ($success) { $success=C(); }</p>
<p>Or, because they&#8217;re being split up, I&#8217;d want to report to the user on these failures.  So:</p>
<p>$success=true;<br />
if ($success) { $success=A(); }<br />
if ($success) { $success=B(); } else { errorA(); }<br />
if ($success) { $success=C(); } else { errorB(); }<br />
if (!$success) { errorC(); }</p>
<p>But I think a little nesting would be easier to read then that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Geoff again</title>
		<link>http://www.danhulton.com/blog/2009/05/12/cleaning-up-your-code-nested-conditionals/comment-page-1/#comment-102</link>
		<dc:creator>Geoff again</dc:creator>
		<pubDate>Tue, 12 May 2009 22:53:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.danhulton.com/blog/?p=65#comment-102</guid>
		<description>Addendum:  I&#039;d generally avoid using lazy evaluation in a chained series of &amp;&amp; to do a series of steps with significant side effects.  If do_chunk_X() just returns a value and doesn&#039;t modify anything, then chaining is probably acceptable.

if (object-&gt;Initialized() &amp;&amp; parameter-&gt;Initialized())
    object-&gt;DoSomethingWith(parameter);

In C++ the Initialized functions would often be declared const, and not modify the objects they&#039;re called on.  Thus, they are conceptually replaced with a true or false, and nothing is hidden.

If each of the chunks has significant side effects, it&#039;s misleading, to me, to write the code as if it&#039;s just checking if something is true/false, which using the return value directly in a chained &amp;&amp; appears to do.  Better to put the significant actions on their own line, even if it requires some nested ifs or similar, so it&#039;s obvious that several different significant tasks are being done.  You can also then comment to explain each task separately.</description>
		<content:encoded><![CDATA[<p>Addendum:  I&#8217;d generally avoid using lazy evaluation in a chained series of &amp;&amp; to do a series of steps with significant side effects.  If do_chunk_X() just returns a value and doesn&#8217;t modify anything, then chaining is probably acceptable.</p>
<p>if (object-&gt;Initialized() &amp;&amp; parameter-&gt;Initialized())<br />
    object-&gt;DoSomethingWith(parameter);</p>
<p>In C++ the Initialized functions would often be declared const, and not modify the objects they&#8217;re called on.  Thus, they are conceptually replaced with a true or false, and nothing is hidden.</p>
<p>If each of the chunks has significant side effects, it&#8217;s misleading, to me, to write the code as if it&#8217;s just checking if something is true/false, which using the return value directly in a chained &amp;&amp; appears to do.  Better to put the significant actions on their own line, even if it requires some nested ifs or similar, so it&#8217;s obvious that several different significant tasks are being done.  You can also then comment to explain each task separately.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Geoff</title>
		<link>http://www.danhulton.com/blog/2009/05/12/cleaning-up-your-code-nested-conditionals/comment-page-1/#comment-101</link>
		<dc:creator>Geoff</dc:creator>
		<pubDate>Tue, 12 May 2009 22:36:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.danhulton.com/blog/?p=65#comment-101</guid>
		<description>Uhm... what?  That is a huge loss in terms of readability.

Instead of the natural / obvious meaning of &quot;if (test) then (task)&quot;, you&#039;ve got an unusual and unusual-looking abuse of a language feature.

If the code chunks are rather large, then splitting them off into their own functions is a good idea, and they can certainly return a bool to indicate success or failure.  This can be used in a compact series of nested ifs that&#039;s quite easy to follow as it all fits on one screen, and appropriate variable names are quite good at self-documentation:

bool success = do_chunk_a();
if (success) {
    success = do_chunk_b();
    if (success) {
        success = do_chunk_c();
    }
}

... which is the whole code block, and easy enough to follow since it&#039;s all visible at once.

Or, if there&#039;s no subsequent code in the current function, and you really just dislike the nesting, abort the function upon partial failure:

bool success = do_chunk_a();
if (!success)
    return false;
success = do_chunk_b();
if (!success)
    return false;
return do_chunk_c();

Or, if you can&#039;t return upon partial failure, but want to do away with the nesting, use a simpler and more explicit way to chain the successes from each call:

bool success = do_chunk_a();
if (success)
    success = do_chunk_b();
if (success)
    success = do_chunk_c();

Or, if you really want to reduce your code, why not:

bool success = do_chunk_a() &amp;&amp; do_chunk_b() &amp;&amp; do_chunk_c();

This even avoids the user having to figure out the assignment of success to itself business.

Any of these alternatives are *much* easier to read than your suggestion, IMO.</description>
		<content:encoded><![CDATA[<p>Uhm&#8230; what?  That is a huge loss in terms of readability.</p>
<p>Instead of the natural / obvious meaning of &#8220;if (test) then (task)&#8221;, you&#8217;ve got an unusual and unusual-looking abuse of a language feature.</p>
<p>If the code chunks are rather large, then splitting them off into their own functions is a good idea, and they can certainly return a bool to indicate success or failure.  This can be used in a compact series of nested ifs that&#8217;s quite easy to follow as it all fits on one screen, and appropriate variable names are quite good at self-documentation:</p>
<p>bool success = do_chunk_a();<br />
if (success) {<br />
    success = do_chunk_b();<br />
    if (success) {<br />
        success = do_chunk_c();<br />
    }<br />
}</p>
<p>&#8230; which is the whole code block, and easy enough to follow since it&#8217;s all visible at once.</p>
<p>Or, if there&#8217;s no subsequent code in the current function, and you really just dislike the nesting, abort the function upon partial failure:</p>
<p>bool success = do_chunk_a();<br />
if (!success)<br />
    return false;<br />
success = do_chunk_b();<br />
if (!success)<br />
    return false;<br />
return do_chunk_c();</p>
<p>Or, if you can&#8217;t return upon partial failure, but want to do away with the nesting, use a simpler and more explicit way to chain the successes from each call:</p>
<p>bool success = do_chunk_a();<br />
if (success)<br />
    success = do_chunk_b();<br />
if (success)<br />
    success = do_chunk_c();</p>
<p>Or, if you really want to reduce your code, why not:</p>
<p>bool success = do_chunk_a() &amp;&amp; do_chunk_b() &amp;&amp; do_chunk_c();</p>
<p>This even avoids the user having to figure out the assignment of success to itself business.</p>
<p>Any of these alternatives are *much* easier to read than your suggestion, IMO.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
