<?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/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	
	>
<channel>
	<title>
	Comments on: How to Design Early Returns in C++ (Based on Procedural Programming)	</title>
	<atom:link href="https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/</link>
	<description>Jonathan Boccara&#039;s blog</description>
	<lastBuildDate>Mon, 03 Sep 2018 01:58:00 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.3</generator>
	<item>
		<title>
		By: Rud Merriam		</title>
		<link>https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/#comment-1266</link>

		<dc:creator><![CDATA[Rud Merriam]]></dc:creator>
		<pubDate>Mon, 03 Sep 2018 01:58:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3601#comment-1266</guid>

					<description><![CDATA[An old code curmudgeon here who first wrote FORTAN in &#039;68 but still doing C++ today for robotics. These examples are both terrible. A few years ago the fallacy of them was illustrated in testing web site certificates. The comment by Vincent Zalzal  is much better but is only applicable for booleans. A local variable should be used and set by each &quot;if&quot; and return. That removes the &quot;interrupt flow of code&quot; responsibility of &#039;return&#039; which should never be used. 

One of the rules of Structured Programming (SP) was one entry and one exit. Using multiple returns violates that rule by providing multiple exits. Newer paradigms did not replace the rules for SP but extended them.]]></description>
			<content:encoded><![CDATA[<p>An old code curmudgeon here who first wrote FORTAN in &#8217;68 but still doing C++ today for robotics. These examples are both terrible. A few years ago the fallacy of them was illustrated in testing web site certificates. The comment by Vincent Zalzal  is much better but is only applicable for booleans. A local variable should be used and set by each &#8220;if&#8221; and return. That removes the &#8220;interrupt flow of code&#8221; responsibility of &#8216;return&#8217; which should never be used. </p>
<p>One of the rules of Structured Programming (SP) was one entry and one exit. Using multiple returns violates that rule by providing multiple exits. Newer paradigms did not replace the rules for SP but extended them.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/#comment-1263</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Sun, 02 Sep 2018 14:51:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3601#comment-1263</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/#comment-1252&quot;&gt;Hel Noisemaker&lt;/a&gt;.

Yes, you&#039;re right Hel. Fixed it, thanks.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/#comment-1252">Hel Noisemaker</a>.</p>
<p>Yes, you&#8217;re right Hel. Fixed it, thanks.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Daniel		</title>
		<link>https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/#comment-1255</link>

		<dc:creator><![CDATA[Daniel]]></dc:creator>
		<pubDate>Wed, 29 Aug 2018 21:10:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3601#comment-1255</guid>

					<description><![CDATA[Hi, I can&#039;t help but to notice that leap Year requires != 0 comparisions after modulo operation. But in your examples I look at that as pseudocode.
As for me I prefer second approach when blocks in if-statement are short enough. As for leap year I&#039;d use short cirquiting as offered in comments below. 
I know Kevlin oftenly uses FizzBuzz example. I&#039;ve watched his talks with pleasure. :)]]></description>
			<content:encoded><![CDATA[<p>Hi, I can&#8217;t help but to notice that leap Year requires != 0 comparisions after modulo operation. But in your examples I look at that as pseudocode.<br />
As for me I prefer second approach when blocks in if-statement are short enough. As for leap year I&#8217;d use short cirquiting as offered in comments below.<br />
I know Kevlin oftenly uses FizzBuzz example. I&#8217;ve watched his talks with pleasure. 🙂</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Victor Dyachenko		</title>
		<link>https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/#comment-1254</link>

		<dc:creator><![CDATA[Victor Dyachenko]]></dc:creator>
		<pubDate>Wed, 29 Aug 2018 11:16:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3601#comment-1254</guid>

					<description><![CDATA[I like this version more:

bool isLeapYear(int year)
{
    return year % 4 == 0 &#038;&#038; (year % 100 != 0 &#124;&#124; year % 400 == 0);
}]]></description>
			<content:encoded><![CDATA[<p>I like this version more:</p>
<p>bool isLeapYear(int year)<br />
{<br />
    return year % 4 == 0 &amp;&amp; (year % 100 != 0 || year % 400 == 0);<br />
}</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: jhk		</title>
		<link>https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/#comment-1253</link>

		<dc:creator><![CDATA[jhk]]></dc:creator>
		<pubDate>Wed, 29 Aug 2018 01:35:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3601#comment-1253</guid>

					<description><![CDATA[I think it&#039;s depends on the case.
I would use early return mostly when procedure is out of pre-condition. 

assume i wanna property &#039;y&#039; from class &#039;X&#039; through pointer to process. i would do null-check from the beginning.

bool SomeRandomFunctionProcessYtoBool(X* x)
{
     if (x == nullptr)
     {
          //I might add some error logging or check here to track down what happen
          return false;
     }
     if (more preconditions)
     {
          return false;
     }

     auto y = x-&#062;y;
     

     //do some magic with y and return bool
}

and in this case I would consider early return without &#039;else&#039; is more cleaner. and its is more practical case when we care about early return i think.]]></description>
			<content:encoded><![CDATA[<p>I think it&#8217;s depends on the case.<br />
I would use early return mostly when procedure is out of pre-condition. </p>
<p>assume i wanna property &#8216;y&#8217; from class &#8216;X&#8217; through pointer to process. i would do null-check from the beginning.</p>
<p>bool SomeRandomFunctionProcessYtoBool(X* x)<br />
{<br />
     if (x == nullptr)<br />
     {<br />
          //I might add some error logging or check here to track down what happen<br />
          return false;<br />
     }<br />
     if (more preconditions)<br />
     {<br />
          return false;<br />
     }</p>
<p>     auto y = x-&gt;y;</p>
<p>     //do some magic with y and return bool<br />
}</p>
<p>and in this case I would consider early return without &#8216;else&#8217; is more cleaner. and its is more practical case when we care about early return i think.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Hel Noisemaker		</title>
		<link>https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/#comment-1252</link>

		<dc:creator><![CDATA[Hel Noisemaker]]></dc:creator>
		<pubDate>Tue, 28 Aug 2018 20:30:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3601#comment-1252</guid>

					<description><![CDATA[Is it me, or &quot;bool isLeapYear(int year) {if (year % 400){return true;}...&quot; is just plainly wrong? It means &quot;if remainder of year/400 != 0 then it&#039;s leap year&quot; which is wrong, no?]]></description>
			<content:encoded><![CDATA[<p>Is it me, or &#8220;bool isLeapYear(int year) {if (year % 400){return true;}&#8230;&#8221; is just plainly wrong? It means &#8220;if remainder of year/400 != 0 then it&#8217;s leap year&#8221; which is wrong, no?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/#comment-1250</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Tue, 28 Aug 2018 09:45:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3601#comment-1250</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/#comment-1246&quot;&gt;Brad Davis&lt;/a&gt;.

Interesting! It would be nice to make a performance benchmark between the two versions. I&#039;m writing this in my to-do list]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/#comment-1246">Brad Davis</a>.</p>
<p>Interesting! It would be nice to make a performance benchmark between the two versions. I&#8217;m writing this in my to-do list</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: EvilTeach		</title>
		<link>https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/#comment-1249</link>

		<dc:creator><![CDATA[EvilTeach]]></dc:creator>
		<pubDate>Mon, 27 Aug 2018 17:09:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3601#comment-1249</guid>

					<description><![CDATA[Here is another philosophy, common cases first.

&lt;pre&gt;
bool isLeapYear(int year)
{
    if (year % 4 != 0)              // For 3 out of 4 years it’s not a leap year (75%) = .75
        return false;
    else
        if (year % 100 != 0)        //     In 1 out of 4 years, 24 out of 25 cases it is not a leap year (25% * 96%) = .24
            return true;
        else
            if (year % 400 != 0)    // But in those 1 out of 4 years, 24 out of 25 cases, 3 out of 4 cases are not leap years (25% * 4% * 75%) = .0075
                return false;
            else
                return true;        // But in those 1 out of 4 years, 24 out of 25 cases, 1 out of 4 cases are leap years. (25% * 4% * 25%) = .0025
                                    // We will all be dead before this fires again.
}
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>Here is another philosophy, common cases first.</p>
<pre>
bool isLeapYear(int year)
{
    if (year % 4 != 0)              // For 3 out of 4 years it’s not a leap year (75%) = .75
        return false;
    else
        if (year % 100 != 0)        //     In 1 out of 4 years, 24 out of 25 cases it is not a leap year (25% * 96%) = .24
            return true;
        else
            if (year % 400 != 0)    // But in those 1 out of 4 years, 24 out of 25 cases, 3 out of 4 cases are not leap years (25% * 4% * 75%) = .0075
                return false;
            else
                return true;        // But in those 1 out of 4 years, 24 out of 25 cases, 1 out of 4 cases are leap years. (25% * 4% * 25%) = .0025
                                    // We will all be dead before this fires again.
}
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: jft		</title>
		<link>https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/#comment-1247</link>

		<dc:creator><![CDATA[jft]]></dc:creator>
		<pubDate>Sat, 25 Aug 2018 12:11:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3601#comment-1247</guid>

					<description><![CDATA[For me, if I had to choose, I would always go with code #2. There is no need for an else following a return and for me having the redundant else clauses gets in the way of reading the code - especially if all the if statements are indented! However, this might be a bad example to choose as the function &#039;degrades&#039; to simply

bool isLeapYear(unsigned int year)
{
	return ((((year % 4) == 0) &#038;&#038; (year % 100)) &#124;&#124; ((year % 400) == 0));
}

which I find easier to read than either code #1 or code #2.

I think there is a debate around using multiple returns in a function as some would say that code #1 should be coded as

bool isLeapYear(unsigned int year)
{
	bool ret = false;
 
	if ((year % 400) == 0)
		ret = true;
	else if ((year % 100) == 0)
		ret = false;
	else if ((year % 4) == 0)
		ret = true;
 
	return ret;
}

with just one entry and one exit - which can be &#039;simplified to&#039;

bool isLeapYear(unsigned int year)
{
	bool ret = false;
 
	if ((year % 400) == 0)
		ret = true;
	else
		if ((year % 100))
			if ((year % 4) == 0)
				ret = true;
 
	return ret;
}


by only setting ret to true when needed as the default value for ret is false and therefore doesn&#039;t need setting again to false. From this, of, course, the one-line function at the start of this post results.


When I was taught structured programming in the late 1970&#039;s using Pascal, our Professor of Computer Programming taught us that a function should only have one entry and one exit. In c the convention was that tests for error conditions etc at the start of a function could have their own return so as not to impose too many levels of if nesting within the main code of the function. And this is a convention I would still agree with today.]]></description>
			<content:encoded><![CDATA[<p>For me, if I had to choose, I would always go with code #2. There is no need for an else following a return and for me having the redundant else clauses gets in the way of reading the code &#8211; especially if all the if statements are indented! However, this might be a bad example to choose as the function &#8216;degrades&#8217; to simply</p>
<p>bool isLeapYear(unsigned int year)<br />
{<br />
	return ((((year % 4) == 0) &amp;&amp; (year % 100)) || ((year % 400) == 0));<br />
}</p>
<p>which I find easier to read than either code #1 or code #2.</p>
<p>I think there is a debate around using multiple returns in a function as some would say that code #1 should be coded as</p>
<p>bool isLeapYear(unsigned int year)<br />
{<br />
	bool ret = false;</p>
<p>	if ((year % 400) == 0)<br />
		ret = true;<br />
	else if ((year % 100) == 0)<br />
		ret = false;<br />
	else if ((year % 4) == 0)<br />
		ret = true;</p>
<p>	return ret;<br />
}</p>
<p>with just one entry and one exit &#8211; which can be &#8216;simplified to&#8217;</p>
<p>bool isLeapYear(unsigned int year)<br />
{<br />
	bool ret = false;</p>
<p>	if ((year % 400) == 0)<br />
		ret = true;<br />
	else<br />
		if ((year % 100))<br />
			if ((year % 4) == 0)<br />
				ret = true;</p>
<p>	return ret;<br />
}</p>
<p>by only setting ret to true when needed as the default value for ret is false and therefore doesn&#8217;t need setting again to false. From this, of, course, the one-line function at the start of this post results.</p>
<p>When I was taught structured programming in the late 1970&#8217;s using Pascal, our Professor of Computer Programming taught us that a function should only have one entry and one exit. In c the convention was that tests for error conditions etc at the start of a function could have their own return so as not to impose too many levels of if nesting within the main code of the function. And this is a convention I would still agree with today.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Brad Davis		</title>
		<link>https://www.fluentcpp.com/2018/08/24/how-to-design-early-returns-in-c-based-on-procedural-programming/#comment-1246</link>

		<dc:creator><![CDATA[Brad Davis]]></dc:creator>
		<pubDate>Fri, 24 Aug 2018 19:09:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3601#comment-1246</guid>

					<description><![CDATA[All of this code puts the 400 test before the 100 test before the 4 test.  If your inputs are evenly distributed among an input range then this means that 3 our of every 4 years is going through 2 more branches than it needs to.   Why test 1999 against 100 and 400 when you can eliminate it as a possible leap year with a test against 4?]]></description>
			<content:encoded><![CDATA[<p>All of this code puts the 400 test before the 100 test before the 4 test.  If your inputs are evenly distributed among an input range then this means that 3 our of every 4 years is going through 2 more branches than it needs to.   Why test 1999 against 100 and 400 when you can eliminate it as a possible leap year with a test against 4?</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
