<?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; programming</title>
	<atom:link href="http://indiwiz.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://indiwiz.com</link>
	<description>Subhash&#039;s Tech Log</description>
	<lastBuildDate>Sun, 29 Apr 2012 15:11:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Writing HTML</title>
		<link>http://indiwiz.com/2009/04/21/writing-html/</link>
		<comments>http://indiwiz.com/2009/04/21/writing-html/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 05:46:02 +0000</pubDate>
		<dc:creator>Subhash Chandran</dc:creator>
				<category><![CDATA[Software Dev]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://indiwiz.com/?p=248</guid>
		<description><![CDATA[Programmers are often required to write HTML code. Recently, on reviewing such code, I found some glaring mistakes. Based on this experience, I have assembled some points which programmers should note when developing HTML. Version of HTML Before writing HTML, decide upon version compliance. HTML 4.01 and XHTML 1.0 are popular choices. Specify Version of [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p>Programmers are often required to write HTML code. Recently, on reviewing such code, I found some glaring mistakes. Based on this experience, I have assembled some points which programmers should note when developing HTML.</p>
<h3>Version of HTML</h3>
<p>Before writing HTML, decide upon version compliance. HTML 4.01 and XHTML 1.0 are popular choices.</p>
<h3>Specify Version of HTML as DOCTYPE</h3>
<p>For HTML 4.01 it is:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
</pre>
<p>For XHTML 1.0:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
</pre>
<p>For a detailed list, visit: <a href="http://webdesign.about.com/od/xhtml/a/aa011507.htm">http://webdesign.about.com/od/xhtml/a/aa011507.htm</a></p>
<h3>Don&#8217;t use deprecated elements like &lt;font&gt;</h3>
<p>&lt;font&gt; has been deprecated since version 4.01 of HTML.</p>
<h3>Use CSS for styling</h3>
<p>For styling purposes (specifying font, color, background-color, border, etc.) use only CSS. For example, for setting the background of a page, the earlier method is:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;body bgcolor=&quot;blue&quot;&gt;
</pre>
<p>This is better written as:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;body style=&quot;background: blue;&quot;&gt;
</pre>
<h3>Open/Close Elements</h3>
<p>Please ensure you open and close the HTML elements in proper order. Always have the discipline of closing open elements.</p>
<h3>Indentation</h3>
<p>HTML is also source code which is maintained by humans. Please respect yourself and the people who will be maintaining it later: write readable HTML with proper indentation.</p>
<h3>Validate HTML</h3>
<p>Use a proper <a href="http://validator.w3.org/">validation service</a> before publishing your HTML. You may also use tools like <tt>xmllint</tt> also to validate your HTML.</p>
<h3>Test in target browser</h3>
<p>All our development systems are Linux. We developers test our HTMLs in Firefox. But our clients use IE. Situations like these demand additional testing effort in IE.</p>
<div class="shr-publisher-248"></div><!-- Start Shareaholic LikeButtonSetBottom --><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://indiwiz.com/2009/04/21/writing-html/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Freedom from fear: The path towards failure, learning and innovation</title>
		<link>http://indiwiz.com/2009/01/24/freedom-from-fear-the-path-towards-failure-learning-and-innovation/</link>
		<comments>http://indiwiz.com/2009/01/24/freedom-from-fear-the-path-towards-failure-learning-and-innovation/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 15:55:46 +0000</pubDate>
		<dc:creator>Subhash Chandran</dc:creator>
				<category><![CDATA[Innovation]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://indiwiz.com/?p=170</guid>
		<description><![CDATA[I have seen this scenario many times: programmers fearing existing code, writing extra code just to accommodate new features without changing existing code. A similar attitude I see is, is manifest in projects dealing with big databases. People working in these projects don&#8217;t dare to question the design of the Database, and subject themselves to [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p>I have seen this scenario many times: programmers fearing existing code, writing extra code just to accommodate new features without changing existing code. A similar attitude I see is, is manifest in projects dealing with big databases. People working in these projects don&#8217;t dare to question the design of the Database, and subject themselves to finishing the tasks at hand to fit around the existing DB design. This is fear. Fear of:</p>
<ul>
<li>Uncertainty. They know existing code works. Refactoring existing might break some functionality.</li>
<li>Time. The time to refactor code cannot be correctly estimated.</li>
<li>Work. Of course, this is additional work.</li>
</ul>
<p>There may be other reasons (for example, greed. The programmer might want to gain a short-term acknowledgment of his speed of delivery). But the common occurrence is Fear. And we will try to understand the implication of this fear:</p>
<ul>
<li>Old code gets older.</li>
<li>Additional code to manage deficiencies in old code need to be created.</li>
<li>The application becomes monolithic and difficult to change.</li>
<li>This creates additional fear of change.</li>
</ul>
<p>The cost involved in maintaining such code is stupendous. And how do we avoid this cost? It is, from my experience, by following some good-old practices and tools:</p>
<ul>
<li>Unit tests: The importance of these cannot be exaggerated. When you have a Unit test suite covering your code, it is with more confidence you can refactor it, and more confidence with which change it.</li>
<li>Each developer gets his own play area: Sharing resources during development is also costly. This is the reason I encourage each developer to have his own database setup and development environment (whenever possible). This isolates the developer, and his fear of breaking the shared environment is nullified (which affects other team member&#8217;s productivity). He is more free to innovate and bring in new thoughts.</li>
<li>Developer&#8217;s own coding and commit space: I also encourage people to use distributed source control systems in place of things like svn. This allows the individual to have a independent commit space, where he can commit his work as and when he completes a unit of it, without worry of breaking someone&#8217;s build. Again, a source of greater freedom.</li>
</ul>
<p>I have seen the most unlikely people innovate by following these simple practices. So what is your take on this?</p>
<div class="shr-publisher-170"></div><!-- Start Shareaholic LikeButtonSetBottom --><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://indiwiz.com/2009/01/24/freedom-from-fear-the-path-towards-failure-learning-and-innovation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The next baby step in Haskell</title>
		<link>http://indiwiz.com/2009/01/24/the-next-baby-step-in-haskell/</link>
		<comments>http://indiwiz.com/2009/01/24/the-next-baby-step-in-haskell/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 07:38:05 +0000</pubDate>
		<dc:creator>Subhash Chandran</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://indiwiz.com/?p=165</guid>
		<description><![CDATA[One important parameter in learning a new language is the language&#8217;s approachability. I remember the difficulty I faced when learning Java. I was asked to know things like package, static function, etc. I was kept in the blank about these features till I reached the these trails in my learning. This is disappointing, and can [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p>One important parameter in learning a new language is the language&#8217;s approachability. I remember the difficulty I faced when learning Java. I was asked to know things like package, static function, etc. I was kept in the blank about these features till I reached the these trails in my learning. This is disappointing, and can intimidate learners from learning such language.</p>
<p>Haskell, just like Python (and other languages like Ruby, Perl, Groovy, etc.) provides a simple interactive shell called <tt>ghci</tt>. This allows one to immediately jump into Haskell, experimenting with its syntax without further ado.</p>
<div class="shr-publisher-165"></div><!-- Start Shareaholic LikeButtonSetBottom --><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://indiwiz.com/2009/01/24/the-next-baby-step-in-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Year Resolution: Language to learn</title>
		<link>http://indiwiz.com/2009/01/13/new-year-resolution-language-to-learn/</link>
		<comments>http://indiwiz.com/2009/01/13/new-year-resolution-language-to-learn/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 17:20:54 +0000</pubDate>
		<dc:creator>Subhash Chandran</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://indiwiz.com/?p=153</guid>
		<description><![CDATA[We are in the mid-January of 2009, and I have taken the decision to learn one of these languages this year: Haskell Erlang Clojure In the recent past I have been hearing some great stuff about these languages. Haskell and Erlang I have been attracted due to their powerful concurrency support. After reading Paul Graham&#8216;s [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p>We are in the mid-January of 2009, and I have taken the decision to learn <i>one</i> of these languages this year:</p>
<ol>
<li><a href="http://www.haskell.org/">Haskell</a></li>
<li><a href="http://erlang.org/">Erlang</a></li>
<li><a href="http://clojure.org/">Clojure</a></li>
</ol>
<p>In the recent past I have been hearing some great stuff about these languages. Haskell and Erlang I have been attracted due to their powerful concurrency support. After reading <a href="http://www.paulgraham.com/">Paul Graham</a>&#8216;s preface in <a href="http://www.paulgraham.com/onlisp.html">On Lisp</a>, I became attracted to Lisp (and Clojure). Haskell also has a JVM implementation, <a href="http://jaskell.codehaus.org/">Jaskell</a>. Coming from a Java background, I think this should be easy for me. Time will tell which one of these I will pick and learn!</p>
<div class="shr-publisher-153"></div><!-- Start Shareaholic LikeButtonSetBottom --><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://indiwiz.com/2009/01/13/new-year-resolution-language-to-learn/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning to Program</title>
		<link>http://indiwiz.com/2008/11/18/learning-to-program/</link>
		<comments>http://indiwiz.com/2008/11/18/learning-to-program/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 09:05:56 +0000</pubDate>
		<dc:creator>Subhash Chandran</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://indiwiz.wordpress.com/?p=13</guid>
		<description><![CDATA[My personal favorite language, of course is Java. But Java is difficult to learn. Consider the Hello World program in Java: Now consider the same program in other languages (Python, Perl, Ruby, Groovy): When learning Java as the first programming language, the learner is forced to understand, difficult to understand concepts like package, command line [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p>My personal favorite language, of course is Java. But Java is difficult to learn. Consider the Hello World program in Java:</p>
<pre class="brush: java; title: ; notranslate">
public class MyClass{
    public static void main(String[] arg){
        System.out.println(&quot;Hello World&quot;);
    }
}
</pre>
<p>Now consider the same program in other languages (Python, Perl, Ruby, Groovy):</p>
<pre class="brush: python; title: ; notranslate">
print &quot;Hello World&quot;
</pre>
<p>When learning Java as the first programming language, the learner is forced to understand, difficult to understand concepts like package, command line parameter, visibility, arrays, etc. in the very first program itself. This is not beginner friendly.</p>
<p><a href="http://www.catb.org/~esr/faqs/hacker-howto.html#skills1">Eric S. Raymond recommended Python</a> for beginners. But now a days it could be any of the new scripting languages: Ruby, Groovy, Scala, or of course Python itself. Each of these dynamic scripting languages have very minimal infrastructure code. So when writing a program to do something, the code exactly means that. Very beginner friendly.</p>
<h3>Java</h3>
<p>After being exposed to so many languages, I still love Java. Of course, I spend lot of time still writing infrastructure code instead of core business logic even now, many of these are changing in Java. For example, to read a user input from command line, long back ago we had to write:</p>
<p>Before Java 5</p>
<pre class="brush: java; title: ; notranslate">
try{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s = br.readLine();
    br.close();
}
catch(IOException ex){
    // ...
}
</pre>
<p>Java 5:</p>
<pre class="brush: java; title: ; notranslate">
Scanner in = new Scanner(System.in);
String s = in.nextLine();
in.close();
</pre>
<p>Now, in Java 6 this has been replaced with:</p>
<pre class="brush: java; title: ; notranslate">
String s = System.console().readLine(); // No IOException thrown
</pre>
<p>Things are getting more developer friendly day by day.</p>
<h3>Java Swing</h3>
<p>One of my favorite parts in Java is Swing programming. All over the years since I first touched Java, Swing has constantly made me learn new things. And I have also been awe inspired so often at the wonderful extensible design of the Swing API itself.</p>
<p>Using Swing APIs itself is a covert educating process. When I started writing event processing code, I was covertly introduced to Observer design pattern. There is thousands of such instances where I learned good OOP design approaches without any formal education. Another area where in normal Java coding we do not spend much time is concurrency. When writing any non-trivial Swing applications, you will have to think about concurrency!</p>
<p>Swing has constantly challenged me to think and design smart applications. This is an area which must be covered for learning Java.</p>
<h3>Enterprise Java</h3>
<p>Enterprise Java is one thing I work day-in and day-out. But the majority of the time I spend when writing Enterprise Java code is: figuring out how. The Enterprise Java platform takes care of the critical areas like resource pooling, transaction management, concurrency etc., so at the end of the day, the programmer is relieved of writing this same code again-and-again. But this ease comes with the cost of configuring the system for it. Also, is Enterprise Java a good learning platform for programming? Well, for programming not exactly. But for many practices, it is. Practices include: packaging, build, testing among others.</p>
<div class="shr-publisher-13"></div><!-- Start Shareaholic LikeButtonSetBottom --><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://indiwiz.com/2008/11/18/learning-to-program/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Interrupting a Thread in Java</title>
		<link>http://indiwiz.com/2008/11/18/interrupting-a-thread-in-java/</link>
		<comments>http://indiwiz.com/2008/11/18/interrupting-a-thread-in-java/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 08:52:48 +0000</pubDate>
		<dc:creator>Subhash Chandran</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[concurrent]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://indiwiz.wordpress.com/?p=10</guid>
		<description><![CDATA[Long long ago, when I was studying in College, my professors taught me to abruptly interrupt a running Thread using Thread.stop() method. Later this method was deprecated. Then in some article I read to interrupt a running thread, you have to write your custom logic: This seems to be a valid code, but there is [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p>Long long ago, when I was studying in College, my professors taught me to abruptly interrupt a running Thread using <tt>Thread.stop()</tt> method. Later this method was deprecated. Then in some article I read to interrupt a running thread, you have to write your custom logic:</p>
<pre class="brush: java; title: ; notranslate">
class MyThread extends Thread{

    private boolean isCancelled = false;

    public void run(){
        while(isCancelled != true){
            // do long running job
        }
    }

    public void cancel(){
        isCancelled = true;
    }
}
</pre>
<p>This seems to be a valid code, but there is an issue. If we are doing some blocking operation, or an IO operation inside the loop, then the cancel notification does not reach until that is finished. Well, so how do we solve this issue?</p>
<h3>Thread API</h3>
<p>The <tt>java.lang.Thread</tt> class has the following API methods:</p>
<pre class="brush: java; title: ; notranslate">
void interrupt();
boolean isInterrupted();
</pre>
<p>To signal a running Thread about our request for termination, we can call its <tt>interrupt()</tt> method. Unlike the old <tt>stop()</tt> method, this does not abruptly terminate the execution of the Thread. What is does:</p>
<table border="1">
<tr>
<th>When Thread is in Blocking mode </th>
<th>When Thread is in normal execution</th>
</tr>
<tr>
<td>Thread&#8217;s interrupt status will be cleared, and InterruptedException thrown.</td>
<td>Thread&#8217;s interrupt status will be set. </td>
</tr>
</table>
<p>A Thread is in blocking mode during following states:</p>
<ol>
<li>Thread blocked in invocation of <tt>wait(...)</tt> methods; or</li>
<li><tt>join(...)</tt> methods; or</li>
<li><tt>sleep(...)</tt> methods.</li>
</ol>
<p>Now my actual reason for exploring this concept was due to <a href="http://code.google.com/p/rest-client/issues/detail?id=44">an issue in RESTClient</a>. I had wanted to implement a Stop button just as in a normal browser. So my Thread was not blocked using any of the interruptable methods, but due to a socket IO. So how can I solve this?</p>
<pre class="brush: java; title: ; notranslate">
class MyThread extends Thread{

    public void run(){
        while(isInterrupted() != true){
            // do socket IO
        }
    }

}
</pre>
<p>But this has the same limitation as the first code I wrote. If the socket IO is in progress, this condition is not checked until the IO is done. So what finally is the solution? It is simple:</p>
<pre class="brush: java; title: ; notranslate">
class MyThread extends Thread{

    private Socket soc = ..;

    public void run(){
        // do socket IO
    }

    @Override
    public void interrupt(){
        if(soc != null){
            soc.close(); // wrap in try/catch
        }
        super.interrupt();
    }

}
</pre>
<p>When we close the Socket object, the IO will fail raising an IOException which we can gracefully handle.</p>
<div class="shr-publisher-10"></div><!-- Start Shareaholic LikeButtonSetBottom --><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://indiwiz.com/2008/11/18/interrupting-a-thread-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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: 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 [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><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; title: ; notranslate">
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; title: ; notranslate">
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; title: ; notranslate">
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-publisher-6"></div><!-- Start Shareaholic LikeButtonSetBottom --><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://indiwiz.com/2008/11/18/beautiful-closures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

