<?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"
	>
<channel>
	<title>Comments on: Yet Another Reason final is Your Friend</title>
	<atom:link href="http://enfranchisedmind.com/blog/2007/01/11/yet-another-reason-final-is-your-friend/feed/" rel="self" type="application/rss+xml" />
	<link>http://enfranchisedmind.com/blog/2007/01/11/yet-another-reason-final-is-your-friend/</link>
	<description>Robert Fischer and Brian Hurt on Punditry, Programming Languages, and Other Religious Issues</description>
	<pubDate>Fri, 21 Nov 2008 03:11:09 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: Enfranchised Mind &#187; Functional (Meta)?Programming Stunts for Ruby and Groovy (and a Little Perl)</title>
		<link>http://enfranchisedmind.com/blog/2007/01/11/yet-another-reason-final-is-your-friend/#comment-33405</link>
		<dc:creator>Enfranchised Mind &#187; Functional (Meta)?Programming Stunts for Ruby and Groovy (and a Little Perl)</dc:creator>
		<pubDate>Tue, 24 Jun 2008 18:04:23 +0000</pubDate>
		<guid isPermaLink="false">http://enfranchisedmind.com/blog/archive/2007/01/11/182#comment-33405</guid>
		<description>[...] also started slapping final everywhere &#8212; see Yet Another Reason final Is Your Friend. A ubiquitous use of final actually gave some nice patterns (in the &#8220;macro&#8221; sense of [...]</description>
		<content:encoded><![CDATA[<p>[...] also started slapping final everywhere &#8212; see Yet Another Reason final Is Your Friend. A ubiquitous use of final actually gave some nice patterns (in the &#8220;macro&#8221; sense of [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Enfranchised Mind &#187; 7 Actually Useful Things You Didn&#8217;t Know Static Typing Could Do: An Introduction for the Dynamic Language Enthusiast</title>
		<link>http://enfranchisedmind.com/blog/2007/01/11/yet-another-reason-final-is-your-friend/#comment-33009</link>
		<dc:creator>Enfranchised Mind &#187; 7 Actually Useful Things You Didn&#8217;t Know Static Typing Could Do: An Introduction for the Dynamic Language Enthusiast</dc:creator>
		<pubDate>Mon, 14 Apr 2008 16:31:22 +0000</pubDate>
		<guid isPermaLink="false">http://enfranchisedmind.com/blog/archive/2007/01/11/182#comment-33009</guid>
		<description>[...] been declaring variables at the point of assignment, as I&#8217;ve been advocating ([1], [2], [3]), then you already know that you can usually get rid of null without missing it [...]</description>
		<content:encoded><![CDATA[<p>[...] been declaring variables at the point of assignment, as I&#8217;ve been advocating ([1], [2], [3]), then you already know that you can usually get rid of null without missing it [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Johannes Rudolph</title>
		<link>http://enfranchisedmind.com/blog/2007/01/11/yet-another-reason-final-is-your-friend/#comment-31943</link>
		<dc:creator>Johannes Rudolph</dc:creator>
		<pubDate>Wed, 05 Dec 2007 18:31:30 +0000</pubDate>
		<guid isPermaLink="false">http://enfranchisedmind.com/blog/archive/2007/01/11/182#comment-31943</guid>
		<description>Sorry, but IMO scoping is completely irrelevant wrt Java and Garbage Collection. 

Your variable is only holding a reference to the String object. In each iteration you overwrite the reference and such remove the reference to the old object.

So your bad example is absolutely OK actually. If I understand you correctly the 'solution' to your problem would be like this:

     for(int i = 0; i &#60; 10; i++) {
         String msg = "We are at position " + i;
         System.out.println(msg);
         // no need for 'msg=null;' since it is out of scope automatically?
     }

In this case we used the variable properly. There is no need for 'msg=null' since the variable goes out of scope at the end of every iteration. 

BUT: The compiler is not able to optimize anything. Because it can't know if the String object is still referenced or not. Why? Because you used msg as a parameter to another method and objects are passed to other methods *by reference*. So if the compiler doesn't check if println saves a reference to this String object somewhere it can't optimize anything. (It could know actually if it would do an analysis of the whole program, but it doesn't).
So the 'solution' is absolutely equivalent to your example (probably even in compiled bytecode).

Your argument has a point though: There is a possible memory leak wrt local variables: If you have references to many or big objects in (living) local variables and call some long running functions or do other long lasting tasks these objects can't be freed since the references are not freed.

BTW. the point you are trying to make is probably a valid one talking about C++, since the lifetime of (stack) objects is scoped.</description>
		<content:encoded><![CDATA[<p>Sorry, but IMO scoping is completely irrelevant wrt Java and Garbage Collection. </p>
<p>Your variable is only holding a reference to the String object. In each iteration you overwrite the reference and such remove the reference to the old object.</p>
<p>So your bad example is absolutely OK actually. If I understand you correctly the &#8217;solution&#8217; to your problem would be like this:</p>
<p>     for(int i = 0; i &lt; 10; i++) {<br />
         String msg = &#8220;We are at position &#8221; + i;<br />
         System.out.println(msg);<br />
         // no need for &#8216;msg=null;&#8217; since it is out of scope automatically?<br />
     }</p>
<p>In this case we used the variable properly. There is no need for &#8216;msg=null&#8217; since the variable goes out of scope at the end of every iteration. </p>
<p>BUT: The compiler is not able to optimize anything. Because it can&#8217;t know if the String object is still referenced or not. Why? Because you used msg as a parameter to another method and objects are passed to other methods *by reference*. So if the compiler doesn&#8217;t check if println saves a reference to this String object somewhere it can&#8217;t optimize anything. (It could know actually if it would do an analysis of the whole program, but it doesn&#8217;t).<br />
So the &#8217;solution&#8217; is absolutely equivalent to your example (probably even in compiled bytecode).</p>
<p>Your argument has a point though: There is a possible memory leak wrt local variables: If you have references to many or big objects in (living) local variables and call some long running functions or do other long lasting tasks these objects can&#8217;t be freed since the references are not freed.</p>
<p>BTW. the point you are trying to make is probably a valid one talking about C++, since the lifetime of (stack) objects is scoped.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bhurt-aw</title>
		<link>http://enfranchisedmind.com/blog/2007/01/11/yet-another-reason-final-is-your-friend/#comment-1329</link>
		<dc:creator>bhurt-aw</dc:creator>
		<pubDate>Fri, 12 Jan 2007 03:00:11 +0000</pubDate>
		<guid isPermaLink="false">http://enfranchisedmind.com/blog/archive/2007/01/11/182#comment-1329</guid>
		<description>I am, in fact, not wandering from topic- just stick with me for a moment.

The hot new optimization technique in compilers these days is called Single Static Assignment.  The idea goes like this: you have an intermediate level representation of the code where you have an infinite number of registers- but each register can only be assigned a value once, when it's first used.  This sounds to me like functional programming, only what I call "variables" are here called "registers".  And I'm not the only one who &lt;a HREF="http://www.cs.princeton.edu/~appel/papers/ssafun.ps" rel="nofollow"&gt;thinks this&lt;/A&gt;.  In effect, modern compilers first convert your program to a functional program, and then compile and optimize that.

So it's no surprise to me that Java is fitting the same pattern.  Because, at the end of the day, it's about the computer being able to reason about the code.  For an optimization to be applied, the compiler has to determine two things about a particuler chunk of code: 1) can the optimization be profitably applied, i.e. is the resulting code faster/less memory intensive/smaller/better than before applying the optimization, and 2) can the optimization be safely applied, i.e. is the behavior of the program still the same after the optimization is applied as it was before?  Both of these require reasoning about the program.

There are other advantages to the computer being able to reason about the code, and not just humans.  For example, we can set the computer to finding bugs in our code, or at least proving that certain wide classes of bugs don't exist.  And the more the computer can figure out on it's own, the less we humans have to tell it.  For example, what type a particular variable or expression has.  Functional programming languages are consistently and repeatedly demonstrated to be among the most productive languages, in large part because so much of the specifics are left up to the computer.

So it's no surprise to me that a more functional style of Java programming is more advantageous.  The trend has been obvious for decades, the question is when will the industry wake up to this fact.</description>
		<content:encoded><![CDATA[<p>I am, in fact, not wandering from topic- just stick with me for a moment.</p>
<p>The hot new optimization technique in compilers these days is called Single Static Assignment.  The idea goes like this: you have an intermediate level representation of the code where you have an infinite number of registers- but each register can only be assigned a value once, when it&#8217;s first used.  This sounds to me like functional programming, only what I call &#8220;variables&#8221; are here called &#8220;registers&#8221;.  And I&#8217;m not the only one who <a HREF="http://www.cs.princeton.edu/~appel/papers/ssafun.ps" rel="nofollow">thinks this</a>.  In effect, modern compilers first convert your program to a functional program, and then compile and optimize that.</p>
<p>So it&#8217;s no surprise to me that Java is fitting the same pattern.  Because, at the end of the day, it&#8217;s about the computer being able to reason about the code.  For an optimization to be applied, the compiler has to determine two things about a particuler chunk of code: 1) can the optimization be profitably applied, i.e. is the resulting code faster/less memory intensive/smaller/better than before applying the optimization, and 2) can the optimization be safely applied, i.e. is the behavior of the program still the same after the optimization is applied as it was before?  Both of these require reasoning about the program.</p>
<p>There are other advantages to the computer being able to reason about the code, and not just humans.  For example, we can set the computer to finding bugs in our code, or at least proving that certain wide classes of bugs don&#8217;t exist.  And the more the computer can figure out on it&#8217;s own, the less we humans have to tell it.  For example, what type a particular variable or expression has.  Functional programming languages are consistently and repeatedly demonstrated to be among the most productive languages, in large part because so much of the specifics are left up to the computer.</p>
<p>So it&#8217;s no surprise to me that a more functional style of Java programming is more advantageous.  The trend has been obvious for decades, the question is when will the industry wake up to this fact.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
