<?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: Make your functions functional	</title>
	<atom:link href="https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/</link>
	<description>Jonathan Boccara&#039;s blog</description>
	<lastBuildDate>Sun, 11 Nov 2018 16:16:06 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.3</generator>
	<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-1461</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Sun, 11 Nov 2018 16:16:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=36#comment-1461</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-1459&quot;&gt;Mark C&lt;/a&gt;.

Indeed! I fixed it, thanks Mark.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-1459">Mark C</a>.</p>
<p>Indeed! I fixed it, thanks Mark.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Mark C		</title>
		<link>https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-1459</link>

		<dc:creator><![CDATA[Mark C]]></dc:creator>
		<pubDate>Sat, 10 Nov 2018 20:32:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=36#comment-1459</guid>

					<description><![CDATA[The word is &quot;elide,&quot; not &quot;elude.&quot;]]></description>
			<content:encoded><![CDATA[<p>The word is &#8220;elide,&#8221; not &#8220;elude.&#8221;</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Rud Merriam		</title>
		<link>https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-1265</link>

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

					<description><![CDATA[Global variables were declared bad decades ago for what is almost the inverse of your reasoning. The problem is you don&#039;t know who is changing them in a large body of code. Imagine debugging a system and determining a bad value is in a global variable. How did it get there? You&#039;ve got 2-3 boxes of FORTRAN IV, Algol or Pascal punched cards (about 2,000-3,000 cards) back in the 70s. There is no way of searching for all the uses of the GV. 

Ooops, misspoke since Nicholas Wirth specifically did not allow GVs in Pascal for that very reason.]]></description>
			<content:encoded><![CDATA[<p>Global variables were declared bad decades ago for what is almost the inverse of your reasoning. The problem is you don&#8217;t know who is changing them in a large body of code. Imagine debugging a system and determining a bad value is in a global variable. How did it get there? You&#8217;ve got 2-3 boxes of FORTRAN IV, Algol or Pascal punched cards (about 2,000-3,000 cards) back in the 70s. There is no way of searching for all the uses of the GV. </p>
<p>Ooops, misspoke since Nicholas Wirth specifically did not allow GVs in Pascal for that very reason.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-1264</link>

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

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-1256&quot;&gt;Steve Hawkes&lt;/a&gt;.

That&#039;s an good point, and I can only agree with it. The scope of a global variable makes it harder to deal with, and conversely the smaller the scope of an object, the easier it is to work with it. Thanks for presenting this.
You&#039;ve also brought up an interesting issue about class member functions. On this specific topic, I think that member functions not showing their inputs and outputs is problematic. Especially their outputs, because this makes them perform silent side effects. One way to go about this is to have const member functions. I&#039;m preparing a special post to talk about that.
Thanks again for sharing Steve.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-1256">Steve Hawkes</a>.</p>
<p>That&#8217;s an good point, and I can only agree with it. The scope of a global variable makes it harder to deal with, and conversely the smaller the scope of an object, the easier it is to work with it. Thanks for presenting this.<br />
You&#8217;ve also brought up an interesting issue about class member functions. On this specific topic, I think that member functions not showing their inputs and outputs is problematic. Especially their outputs, because this makes them perform silent side effects. One way to go about this is to have const member functions. I&#8217;m preparing a special post to talk about that.<br />
Thanks again for sharing Steve.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Steve Hawkes		</title>
		<link>https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-1256</link>

		<dc:creator><![CDATA[Steve Hawkes]]></dc:creator>
		<pubDate>Thu, 30 Aug 2018 21:01:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=36#comment-1256</guid>

					<description><![CDATA[&#062;       &quot;... many of us can’t exactly explain why global variables should be avoided. It is not a question of scope. Indeed, global constants have the same scope as global variables, but global constants are generally seen as a Good Thing, because they let you put a label over what would otherwise be &#039;magic values&#039;.&quot;

Isn&#039;t scope what makes a global variable a global variable? So it is all about scope: specifically, how the global scope impacts visibility and lifetime.

Global constants might be a good thing in some applications, at least as long as they are properly contained in namespaces. But what distinguishes these constants from the &quot;bad&quot; global variables is constness, which is also a key factor here.

&#062;    &quot;Some people answer that global variables should be avoided because they cause multithreading issues. They do cause multithreading issues, because a global variable can be accessed from any function, and could be written and read simultaneously from several threads, but I don&#039;t think this is the main issue. Because, like everyone knows, global variables should be avoided even when there is only a single thread in a program.&quot;

The potential for multithreading issues is one concern with global variables, but that is again tied to scope: global variables persist beyond the lifetime of a function scope and thus can be accessed by multiple threads. But this is not unique to globals. Any non-constant data which persists beyond the lifetime of a function call can introduce multithreading issues, including non-const class data members and non-const static data. So impact to multithreading is not the crucial issue here.

&#062;    &quot;I think that global variables are a problem because they break functions. Functions are useful to decompose a program (or another function) into simpler elements, and for this reason, they reduce complexity, and are a tool to improve expressiveness of the code. But to do this, functions must respect certain rules. One of the rules to respect stems from the very definition of a function: A function takes inputs, and provides outputs. It sounds simple, because it is. And to keep it simple, the important thing to understand is that a function must clearly show what its inputs and outputs are. This is where global variables break functions. As soon as there is a global variable, every function in its scope can potentially have this global variable as input and/or output. And this is hidden from the function declaration. So the function has inputs and outputs, but does not tell exactly what they are. Such functions are dysfunctional.&quot;

Indeed, a function should clearly show its inputs and outputs, and abstractly it is helpful if it appears as if there are no external inputs or outputs. But only some functions have no inputs or outputs beyond the ones visible in the function declaration: functions which have no dependencies or effects on external entities or impact to object state. Regardless, a global variable is only one example of something that can introduce external inputs and outputs to a function. Class data members have a similar effect (they persist beyond the lifetime of the function call and are used to input and/or output data in the function), as does any stateful data/property used by the function within its body (such as a file or another object used by the function).

(By the way, wouldn&#039;t all class member functions that use class member data be dysfunctional by the &quot;function must clearly show what its inputs and outputs are&quot; rationale above? Such functions have hidden inputs and/or outputs.)

So it doesn&#039;t appear we&#039;ve hit the mark yet.

Isn&#039;t it specifically scope (when combined with variability) that is what makes global variables undesirable? After all, scope is what makes a global variable different from any other variable--and scope impacts visibility and lifetime. By making variables global, we make them visible outside of the scope of a function or class or source file, killing all of the well-known benefits of name scope. We also make the variables persist for the lifetime of the program, which introduces difficult problems such as static construction and destruction issues and unit testing complexity. These are the properties that make global variables bad.]]></description>
			<content:encoded><![CDATA[<p>&gt;       &#8220;&#8230; many of us can’t exactly explain why global variables should be avoided. It is not a question of scope. Indeed, global constants have the same scope as global variables, but global constants are generally seen as a Good Thing, because they let you put a label over what would otherwise be &#8216;magic values&#8217;.&#8221;</p>
<p>Isn&#8217;t scope what makes a global variable a global variable? So it is all about scope: specifically, how the global scope impacts visibility and lifetime.</p>
<p>Global constants might be a good thing in some applications, at least as long as they are properly contained in namespaces. But what distinguishes these constants from the &#8220;bad&#8221; global variables is constness, which is also a key factor here.</p>
<p>&gt;    &#8220;Some people answer that global variables should be avoided because they cause multithreading issues. They do cause multithreading issues, because a global variable can be accessed from any function, and could be written and read simultaneously from several threads, but I don&#8217;t think this is the main issue. Because, like everyone knows, global variables should be avoided even when there is only a single thread in a program.&#8221;</p>
<p>The potential for multithreading issues is one concern with global variables, but that is again tied to scope: global variables persist beyond the lifetime of a function scope and thus can be accessed by multiple threads. But this is not unique to globals. Any non-constant data which persists beyond the lifetime of a function call can introduce multithreading issues, including non-const class data members and non-const static data. So impact to multithreading is not the crucial issue here.</p>
<p>&gt;    &#8220;I think that global variables are a problem because they break functions. Functions are useful to decompose a program (or another function) into simpler elements, and for this reason, they reduce complexity, and are a tool to improve expressiveness of the code. But to do this, functions must respect certain rules. One of the rules to respect stems from the very definition of a function: A function takes inputs, and provides outputs. It sounds simple, because it is. And to keep it simple, the important thing to understand is that a function must clearly show what its inputs and outputs are. This is where global variables break functions. As soon as there is a global variable, every function in its scope can potentially have this global variable as input and/or output. And this is hidden from the function declaration. So the function has inputs and outputs, but does not tell exactly what they are. Such functions are dysfunctional.&#8221;</p>
<p>Indeed, a function should clearly show its inputs and outputs, and abstractly it is helpful if it appears as if there are no external inputs or outputs. But only some functions have no inputs or outputs beyond the ones visible in the function declaration: functions which have no dependencies or effects on external entities or impact to object state. Regardless, a global variable is only one example of something that can introduce external inputs and outputs to a function. Class data members have a similar effect (they persist beyond the lifetime of the function call and are used to input and/or output data in the function), as does any stateful data/property used by the function within its body (such as a file or another object used by the function).</p>
<p>(By the way, wouldn&#8217;t all class member functions that use class member data be dysfunctional by the &#8220;function must clearly show what its inputs and outputs are&#8221; rationale above? Such functions have hidden inputs and/or outputs.)</p>
<p>So it doesn&#8217;t appear we&#8217;ve hit the mark yet.</p>
<p>Isn&#8217;t it specifically scope (when combined with variability) that is what makes global variables undesirable? After all, scope is what makes a global variable different from any other variable&#8211;and scope impacts visibility and lifetime. By making variables global, we make them visible outside of the scope of a function or class or source file, killing all of the well-known benefits of name scope. We also make the variables persist for the lifetime of the program, which introduces difficult problems such as static construction and destruction issues and unit testing complexity. These are the properties that make global variables bad.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Vincent		</title>
		<link>https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-904</link>

		<dc:creator><![CDATA[Vincent]]></dc:creator>
		<pubDate>Thu, 05 Apr 2018 08:40:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=36#comment-904</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-884&quot;&gt;Jonathan Boccara&lt;/a&gt;.

I tend to encounter 1) more often than 2), but I cannot sacrifice performance too. Therefore, I think I need to use a custom allocator to make memory allocation quicker.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-884">Jonathan Boccara</a>.</p>
<p>I tend to encounter 1) more often than 2), but I cannot sacrifice performance too. Therefore, I think I need to use a custom allocator to make memory allocation quicker.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-884</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Mon, 26 Mar 2018 06:56:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=36#comment-884</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-882&quot;&gt;Vincent&lt;/a&gt;.

Thanks for the clarification Vincent, I think I see your point now: in the case where you repeatedly call a function and are able to pass the same instance of the results vector, you&#039;d waste performance by making the function rebuild a new vector every time. This is an interesting case which I hadn&#039;t considered and I&#039;m not sure how to keep the function functional then.

Do you encounter this situation often?

The case of the article is where you don&#039;t hold the same instance of vector every time you call the function, in which case function1 and function2 cause the same number of dynamic alloc, either before or within the function (my previous comment was unclear btw, function2 doesn&#039;t make an alloc but _causes_ an alloc to be made beforehand).

So to recap, 1) if there has to be one alloc per call, the code is clearer if the function takes care of making it. 2) But if you can pass the same vector to be reused for each function call, you can save performance by passing it to the function (we&#039;d profile it to make sure it&#039;s worth it though). Do you tend to encounter 2) more often than 1) ?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-882">Vincent</a>.</p>
<p>Thanks for the clarification Vincent, I think I see your point now: in the case where you repeatedly call a function and are able to pass the same instance of the results vector, you&#8217;d waste performance by making the function rebuild a new vector every time. This is an interesting case which I hadn&#8217;t considered and I&#8217;m not sure how to keep the function functional then.</p>
<p>Do you encounter this situation often?</p>
<p>The case of the article is where you don&#8217;t hold the same instance of vector every time you call the function, in which case function1 and function2 cause the same number of dynamic alloc, either before or within the function (my previous comment was unclear btw, function2 doesn&#8217;t make an alloc but _causes_ an alloc to be made beforehand).</p>
<p>So to recap, 1) if there has to be one alloc per call, the code is clearer if the function takes care of making it. 2) But if you can pass the same vector to be reused for each function call, you can save performance by passing it to the function (we&#8217;d profile it to make sure it&#8217;s worth it though). Do you tend to encounter 2) more often than 1) ?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: antoine		</title>
		<link>https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-883</link>

		<dc:creator><![CDATA[antoine]]></dc:creator>
		<pubDate>Sun, 25 Mar 2018 22:37:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=36#comment-883</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-672&quot;&gt;Arash Kashani&lt;/a&gt;.

how about std::any ?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-672">Arash Kashani</a>.</p>
<p>how about std::any ?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Vincent		</title>
		<link>https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-882</link>

		<dc:creator><![CDATA[Vincent]]></dc:creator>
		<pubDate>Sat, 24 Mar 2018 13:08:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=36#comment-882</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-881&quot;&gt;Jonathan Boccara&lt;/a&gt;.

Hi Jonathan, function1 need dynamic allocation whenever it is called, function2 need no dynamic allocation whenever it is called. huge performance difference for small vectors. http://quick-bench.com/QhDHBqlVkk_1FmXP-4wWIF9pHCw


My question is how to make functions functional if I need to avoid dynamic memory allocation inside functions because of performance concern?

Thank you for your reply in advance.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-881">Jonathan Boccara</a>.</p>
<p>Hi Jonathan, function1 need dynamic allocation whenever it is called, function2 need no dynamic allocation whenever it is called. huge performance difference for small vectors. <a href="http://quick-bench.com/QhDHBqlVkk_1FmXP-4wWIF9pHCw" rel="nofollow ugc">http://quick-bench.com/QhDHBqlVkk_1FmXP-4wWIF9pHCw</a></p>
<p>My question is how to make functions functional if I need to avoid dynamic memory allocation inside functions because of performance concern?</p>
<p>Thank you for your reply in advance.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-881</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Fri, 23 Mar 2018 16:40:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=36#comment-881</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-880&quot;&gt;Vincent&lt;/a&gt;.

It seems to me that function1 and function2 make exactly the same number of heap allocations (which is, 1 each). And function1 is more readable, both for in prototype and at call site. Do you agree with this?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2016/11/22/make-your-functions-functional/#comment-880">Vincent</a>.</p>
<p>It seems to me that function1 and function2 make exactly the same number of heap allocations (which is, 1 each). And function1 is more readable, both for in prototype and at call site. Do you agree with this?</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
