<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>indiWiz.com &#187; closure</title>
	<atom:link href="http://indiwiz.com/tag/closure/feed/" rel="self" type="application/rss+xml" />
	<link>http://indiwiz.com</link>
	<description>Subhash&#039;s Tech Log</description>
	<lastBuildDate>Wed, 03 Mar 2010 13:24:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Beautiful Closures</title>
		<link>http://indiwiz.com/2008/11/18/beautiful-closures/</link>
		<comments>http://indiwiz.com/2008/11/18/beautiful-closures/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 08:27:54 +0000</pubDate>
		<dc:creator>Subhash Chandran</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://indiwiz.wordpress.com/?p=6</guid>
		<description><![CDATA[One of the irritating things when writing code in Java is when managing files or database resources. We have to write so much redundant code: File f = new File(&#34;/etc/passwd&#34;); BufferedReader br = null; try{ br = new BufferedReader(new FileReader(f)); String str; while((str=br.readLine())!=null){ System.out.println(str); } } catch(IOException ex){ ... } finally{ if(br != null){ try{ [...]]]></description>
			<content:encoded><![CDATA[<p>One of the irritating things when writing code in Java is when managing files or database resources. We have to write so much redundant code:</p>
<pre class="brush: java;">
File f = new File(&quot;/etc/passwd&quot;);
BufferedReader br = null;
try{
  br = new BufferedReader(new FileReader(f));
  String str;
  while((str=br.readLine())!=null){
    System.out.println(str);
  }
}
catch(IOException ex){
  ...
}
finally{
  if(br != null){
    try{
      br.close();
    }
    catch(IOException ex){
      ...
    }
  }
}
</pre>
<p>So much redundancy is ground for human errors. Issues like these are beautifully solved using programming concept called Closures. For example, in the newly released 2.6 version of Python, the same would be written as:</p>
<pre class="brush: python;">
with open('/etc/passwd', 'r') as f:
    for line in f:
        print line
</pre>
<p>In the above code, even when an exception is raised during file read, the file is gracefully closed when the control exits with block. A similar example in Groovy:</p>
<pre class="brush: java;">
File f = new File(&quot;/etc/passwd&quot;)
f.eachLine{
  line -&gt;
  println line
}
</pre>
<p>Martin Fowler has written a short introduction to <a href="http://martinfowler.com/bliki/Closure.html">Closures</a>.</p>


<div class="shr-bookmarks shr-bookmarks-expand">
<ul class="socials">
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://indiwiz.com/2008/11/18/beautiful-closures/&amp;title=Beautiful+Closures" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://indiwiz.com/2008/11/18/beautiful-closures/&amp;title=Beautiful+Closures" rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://indiwiz.com/2008/11/18/beautiful-closures/&amp;title=Beautiful+Closures&amp;desc=One%20of%20the%20irritating%20things%20when%20writing%20code%20in%20Java%20is%20when%20managing%20files%20or%20database%20resources.%20We%20have%20to%20write%20so%20much%20redundant%20code%3A%0A%0A%5Bsourcecode%20language%3D%27java%27%5D%0AFile%20f%20%3D%20new%20File%28%22%2Fetc%2Fpasswd%22%29%3B%0ABufferedReader%20br%20%3D%20null%3B%0Atry%7B%0A%20%20br%20%3D%20new%20BufferedReader%28new%20FileReader%28f%29%29%3B%0A%20%20String%20str%3B%0A%20%20w" rel="nofollow" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://indiwiz.com/2008/11/18/beautiful-closures/&amp;t=Beautiful+Closures" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://indiwiz.com/2008/11/18/beautiful-closures/&amp;title=Beautiful+Closures&amp;summary=One%20of%20the%20irritating%20things%20when%20writing%20code%20in%20Java%20is%20when%20managing%20files%20or%20database%20resources.%20We%20have%20to%20write%20so%20much%20redundant%20code%3A%0A%0A%5Bsourcecode%20language%3D%27java%27%5D%0AFile%20f%20%3D%20new%20File%28%22%2Fetc%2Fpasswd%22%29%3B%0ABufferedReader%20br%20%3D%20null%3B%0Atry%7B%0A%20%20br%20%3D%20new%20BufferedReader%28new%20FileReader%28f%29%29%3B%0A%20%20String%20str%3B%0A%20%20w&amp;source=indiWiz.com" rel="nofollow" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://indiwiz.com/2008/11/18/beautiful-closures/&amp;title=Beautiful+Closures" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://indiwiz.com/2008/11/18/beautiful-closures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
