<?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>CertPal &#187; bug</title>
	<atom:link href="http://www.certpal.com/blogs/tag/bug/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.certpal.com/blogs</link>
	<description>Technology and certifications</description>
	<lastBuildDate>Mon, 18 Jul 2011 06:48:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>The 5 minute fix</title>
		<link>http://www.certpal.com/blogs/2010/08/the-5-minute-fix/</link>
		<comments>http://www.certpal.com/blogs/2010/08/the-5-minute-fix/#comments</comments>
		<pubDate>Sat, 14 Aug 2010 01:06:05 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[expectation]]></category>
		<category><![CDATA[fix]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=862</guid>
		<description><![CDATA[A developer's perspective on setting the right expectations for any work that you do. Sometimes that is more important than the work itself.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F08%2Fthe-5-minute-fix%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F08%2Fthe-5-minute-fix%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.certpal.com/blogs/wp-content/uploads/bomb_fuse.png"><img class="alignright size-full wp-image-825" title="bomb_fuse" src="http://www.certpal.com/blogs/wp-content/uploads/bomb_fuse.png" alt="" width="128" height="128" /></a>It was a normal day. The sun rose, birds were singing and everything seemed to be going fine until Kaboom ! A bug appeared in the live environment. Things turned downhill from there that day. But we are more interested in how things got fixed so lets focus on that.</p>
<p>An investigation of the code revealed that the bug had crept in because of a minor mistake. A variable was referring to the wrong column index on a table. &#8216;Hmmm&#8230; simple fix&#8217; I thought. I told my manager it would not take more than 5 minutes to do. &#8216;Its a one line fix&#8217;. Mistake.</p>
<p>There were loads of other things to do after fixing this bug. This is what I ended up doing over the next 4 hours.</p>
<ul>
<li> Analyze if any other code flow would explode.</li>
<li> Code the fix &#8211; This only took 5 minutes as expected.</li>
<li> Move fix to DEV</li>
<li> DEV server crashed. Great. Fix this and get DEV up and running.</li>
<li> Test fix</li>
<li> Export the old data before the fix</li>
<li> Export the new data after the fix</li>
<li> Compare new data with old data and verify the result for each column (Wrote a small program to do this).</li>
<li> Move fix to PROD</li>
<li> Close the bugzilla bugs and fill in a lot of bug details</li>
<li> Inform PROD users about the fix</li>
</ul>
<p>So ! There we are. All done. Loose ends tied. But at the end of the day, I had not set the right expectations when I went about fixing this problem. When you say things like &#8216;It will be done in 5 minutes&#8217; and &#8216;It is a one line fix&#8217;, you need to remember that to an end user / manager that might sound something like &#8216;It will start working again after 5 minutes&#8217;. But that is not the case. Well&#8230; a manager with a technical background would be more understanding but that is not my point.</p>
<p>Sometimes there are things you cannot put down in writing when you provide an estimated time to fix stuff. I find it is always better to say &#8216;Sometime by Tuesday noon&#8217; instead of &#8216;One line code change / 5 minutes&#8217;. It sets the right expectation and you don&#8217;t have people asking you &#8216;Its not done yet !?&#8217;. Explaining your work is sometimes more important than the work itself.</p>
<p><script type="text/javascript">// <![CDATA[
var dzone_url = 'http://www.certpal.com/blogs/2010/08/the-5-minute-fix/';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_title = 'The 5 minute fix';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_blurb = 'A developer's perspective on setting the right expectations for any work that you do. Sometimes that is more important than the work itself.';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_style = '2';
// ]]&gt;</script><br />
<script src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F08%2Fthe-5-minute-fix%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F08%2Fthe-5-minute-fix%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2010/08/the-5-minute-fix/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to improve your programming skills</title>
		<link>http://www.certpal.com/blogs/2010/06/how-to-improve-your-programming-skills/</link>
		<comments>http://www.certpal.com/blogs/2010/06/how-to-improve-your-programming-skills/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 16:35:55 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=732</guid>
		<description><![CDATA[Everyone introduces bugs into their code, whether they like it or not. Here are a few ways through which you can reduce the number of bugs in your code. Or at least, try ;)]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F06%2Fhow-to-improve-your-programming-skills%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F06%2Fhow-to-improve-your-programming-skills%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Ever visit sites like <a href="http://www.topcoder.com/tc/" target="_blank">topcoder.com</a> and solve a problem or two ? No ? You should and do so regularly. It will help find tune your programming / design skills. This is because solving the problems requires going though the following thought process</p>
<ol>
<li>Understanding the problem.</li>
<li>Thinking up a solution that covers all use cases.</li>
<li>Executing your thought process through code.</li>
<li>Testing.</li>
</ol>
<p>Now as simple as that sounds, it is not so easy to get right. Especially when you are competing with thousands of fellow coders for time. And when time is a constrain you usually select the first solution that comes to your head, which might not necessarily be the best. So there is that risk of your code timing out even if it is following the right logic. I would love to delve into a problem right about now and throw in some examples and illustration, but you get the point.</p>
<p>When the thought process that we follow is not well defined and we lose focus, it often leads to buggy code. Don&#8217;t believe me ? Here are some bugs I caught in a code base on a recent project.</p>
<p><a href="http://www.certpal.com/blogs/2010/03/bugs-the-most-common-kind/" target="_blank">Bug 1</a></p>
<p><a href="http://www.certpal.com/blogs/2010/04/yet-another-bug/" target="_blank">Bug 2</a></p>
<p><a href="http://www.certpal.com/blogs/2010/06/more-bugs/" target="_blank">Bug 3</a></p>
<p>There were more bugs identified after development than what is listed here. But what I wanted to highlight was that the problem was introduced into the system due to lack of focus and clarity of thought. Well, more often than not anyway. Other times the bug sneaks into the system because the number of test cases to test a method were not sufficient. </p>
<p>Have a look at this google research <a href="http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html" target="_blank">blog post about broken binary search and merge sort algorithms</a> for example. They pretty much have the same bug as the <a href="http://www.certpal.com/blogs/2010/04/yet-another-bug/" target="_blank">date calculation bug</a> that I highlighted in a different post. We are all prone to creating bugs, but we can minimize the number of ones we introduce into the system.</p>
<p>So what have I learned from introducing bugs so far ?</p>
<ol>
<li>As long as you keep your skills sharp and practice solving problems, the number of bugs you introduce will reduce. I try to visit sites like <a href="http://www.topcoder.com/tc/" target="_blank">topcoder.com</a> or <a href="http://codingbat.com/" target="_blank">codingbat.com</a> whenever I find the time.</li>
<li>Learning from other&#8217;s mistakes and best practices can save you tons of time.</li>
<li>Try not to cram in your work within X hours. This tends to focus the effort on finishing the code on time instead of writing it neat and bug free.</li>
</ol>
<p>As always its easier said than done. But hey, its never too late eh ? As for finding the bugs in the first place&#8230;</p>
<ol>
<li>Use a tool like <a href="http://findbugs.sourceforge.net/" target="_self">FindBugs</a> after you are done coding. You will be surprised at how many problems turn up.</li>
<li>Try to maintain a code coverage of at least 80 %. Tools like <a href="http://emma.sourceforge.net/" target="_blank">Emma</a> can help you track this.</li>
</ol>
<p><script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2010/06/how-to-improve-your-programming-skills/';</script><br />
<script type="text/javascript">var dzone_title = 'How to improve your programming skills';</script><br />
<script type="text/javascript">var dzone_blurb = 'Everyone introduces bugs into their code, whether they like it or not. Here are a few ways through which you can reduce the number of bugs in your code. Or at least, try <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F06%2Fhow-to-improve-your-programming-skills%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F06%2Fhow-to-improve-your-programming-skills%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2010/06/how-to-improve-your-programming-skills/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>More bugs</title>
		<link>http://www.certpal.com/blogs/2010/06/more-bugs/</link>
		<comments>http://www.certpal.com/blogs/2010/06/more-bugs/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 09:02:32 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[bug]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=742</guid>
		<description><![CDATA[Another bug that I stumbled across while testing some code. Squashed this one after some analysis.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F06%2Fmore-bugs%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F06%2Fmore-bugs%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p style="text-align: center;"><a href="http://www.certpal.com/blogs/wp-content/uploads/bug.png"><img class="aligncenter size-full wp-image-637" style="border: 1px dashed blue;" title="bug" src="http://www.certpal.com/blogs/wp-content/uploads/bug.png" alt="" width="213" height="176" /></a></p>
<p>I ran into another sweet little bug that I thought I would share with you. This one cropped up after it passed a few tests. See if you can spot the problem. Without further delay here it is</p>
<p><strong>Problem:</strong></p>
<p>A record needs to be inserted with a new identity number. This number is obtained as the next maximum ID value from a set of elements in a XML file. The following code is supposed to obtain the next ID from the XML. Lets skip the XML related code since that just adds clutter. </p>
<p><strong>Code:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> max <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> DataType data <span style="color: #339933;">:</span> someListFromTheXml <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>max <span style="color: #339933;">&lt;</span> data.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
         max <span style="color: #339933;">=</span> data.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
s_logger.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; Max id: &quot;</span> <span style="color: #339933;">+</span> max<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">return</span> max<span style="color: #339933;">;</span></pre></div></div>

<p>See anything wrong with it ? Its got a pretty big flaw. The logic works only for odd number of elements. When the number of elements is even, it fails. Here&#8217;s an illustration</p>
<p><strong>Test cases:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Test
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">int</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> input1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> input2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> input3 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> input4 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        Test test <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        test.<span style="color: #006633;">go</span><span style="color: #009900;">&#40;</span>input1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        test.<span style="color: #006633;">go</span><span style="color: #009900;">&#40;</span>input2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        test.<span style="color: #006633;">go</span><span style="color: #009900;">&#40;</span>input3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        test.<span style="color: #006633;">go</span><span style="color: #009900;">&#40;</span>input4<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        test.<span style="color: #006633;">go</span><span style="color: #009900;">&#40;</span>input5<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> go<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> input<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">int</span> max<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> id<span style="color: #339933;">:</span>input<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>max <span style="color: #339933;">&lt;</span> id<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                max <span style="color: #339933;">=</span> id<span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; Max id: &quot;</span> <span style="color: #339933;">+</span> max<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here&#8217;s the output:</p>
<p>Max id: 2<br />
Max id: 2<br />
Max id: 4<br />
Max id: 4<br />
Max id: 6</p>
<p>The logic incorrectly assigns the max to id+1. Instead it should have stored the current max and returned max+1 at the end of the loop. Sigh. </p>
<p>In hind sight a Xpath expression would have done nicely here. Something like max(/Parent/child/@value)+1. Still that is no excuse for the faulty logic <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2010/06/more-bugs/';</script><br />
<script type="text/javascript">var dzone_title = 'More bugs';</script><br />
<script type="text/javascript">var dzone_blurb = 'Another bug that I stumbled across while testing some code. Squashed this one after some analysis.';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F06%2Fmore-bugs%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F06%2Fmore-bugs%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2010/06/more-bugs/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Yet another bug</title>
		<link>http://www.certpal.com/blogs/2010/04/yet-another-bug/</link>
		<comments>http://www.certpal.com/blogs/2010/04/yet-another-bug/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 05:23:37 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[bug]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=686</guid>
		<description><![CDATA[Yet annother bug caused by a silly mistake. Bugs like these are easy to catch although they are not so evident when they are introduced into the code.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F04%2Fyet-another-bug%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F04%2Fyet-another-bug%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.certpal.com/blogs/wp-content/uploads/bug.png"><img class="aligncenter" style="border: 1px dashed blue;" title="bug" src="http://www.certpal.com/blogs/wp-content/uploads/bug.png" alt="" width="213" height="176" /></a></p>
<p>I came across another silly little bug today. Take a look at the code below (assume &#8216;days&#8217; is a parameter and that this snippet is part of a larger function)</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Date</span> now <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">long</span> nowMillis <span style="color: #339933;">=</span> now.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Timestamp</span> nowTimestamp <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#40;</span>nowMillis<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">long</span> future <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3600</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">24</span> <span style="color: #339933;">*</span> days <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Timestamp</span> expiryTimestamp <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#40;</span>nowMillis <span style="color: #339933;">+</span> future<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>nowTimestamp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>expiryTimestamp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Can you tell what is wrong with it ? What the code intends to do is to set an expiry timestamp to an element &#8211; X days from today. A caller called this code snippet with the value 5. Well there is nothing wrong so far. Here is the output</p>
<p>2010-<strong>04</strong>-<strong>27</strong> 10:37:10.497<br />
2010-<strong>05</strong>-<strong>02</strong> 10:37:10.497</p>
<p>So then came along an element that needed an expiry of around 40 days. The caller calls with the value 40. Guess what the output is</p>
<p>2010-04-<strong>27</strong> 10:38:43.372<br />
2010-04-<span style="color: #ff0000;"><strong>17</strong></span> 17:35:56.076</p>
<p>Umm&#8230; so the expiry date is *before* the current date. Super. So what is going wrong ? The value of future in this case is -838967296. Although future is a long type, the data type that is being calculated to assign a value to it, is int. An int cannot take anything more than Integer.MAX_VALUE (2147483647).</p>
<p>If future had been calculated correctly, its value would be 3456000000. Instead what is being presented is the wrapped around value obtained after adding the remainder to Integer.MIN_VALUE.</p>
<p>I guess that just goes to show that if it aint broke you may still need to fix it later. Of course that depends on what your definition of &#8216;broke&#8217; is <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2010/04/yet-another-bug/';</script><br />
<script type="text/javascript">var dzone_title = 'Yet another bug';</script><br />
<script type="text/javascript">var dzone_blurb = 'Yet annother bug caused by a silly mistake. Bugs like these are easy to catch although they are not so evident when they are introduced into the code.';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F04%2Fyet-another-bug%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F04%2Fyet-another-bug%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2010/04/yet-another-bug/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Bugs &#8211; The most common kind</title>
		<link>http://www.certpal.com/blogs/2010/03/bugs-the-most-common-kind/</link>
		<comments>http://www.certpal.com/blogs/2010/03/bugs-the-most-common-kind/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 04:02:18 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=627</guid>
		<description><![CDATA[Bugs are introduced into code inevitably. Most bugs are introduced due to minor errors made in logic or errors introduced when code is copied and pasted into a location. This highlights the need for tools like FindBugs]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F03%2Fbugs-the-most-common-kind%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F03%2Fbugs-the-most-common-kind%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p style="text-align: center;"><a href="http://www.certpal.com/blogs/wp-content/uploads/bug.png"><img class="aligncenter size-full wp-image-637" style="border: 1px dashed #4665FF;" title="bug" src="http://www.certpal.com/blogs/wp-content/uploads/bug.png" alt="" width="213" height="176" /></a></p>
<p style="text-align: center;">
<p style="text-align: left;">I was testing an application today and came across a bug. A screen had some CRUD operations on a resource. The problem was, whatever I did, the system would perform the operation and tell me that the resource already existed. This left me scratching my head for quite a while. I add a resource and it says it already exists and then adds it. Delete and update also do the same.</p>
<p>So the debugging process started and I sat down with eclipse to get to the root of the problem. I verified that indeed the CRUD operations were reflecting in the persistence store. Then I came across these magical lines of code</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> S_SOMETHING_EXISTS <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;something.already.exists&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> S_SOMETHING_ADD_SUCCESS <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;something.already.exists&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> S_SOMETHING_DELETE_FAIL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;something.already.exists&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> S_SOMETHING_DELETE_SUCCESS <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;something.already.exists&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This left me smacking my head. It should have been</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> S_SOMETHING_EXISTS <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;something.already.exists&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> S_SOMETHING_ADD_SUCCESS <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;something.add.success&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> S_SOMETHING_DELETE_FAIL <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;something.delete.failed&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> S_SOMETHING_DELETE_SUCCESS <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;something.delete.success&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The bug was introduced when I copied the first constant and pasted it three times. I forgot to change the keys <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_mrgreen.gif' alt=':mrgreen:' class='wp-smiley' /> . My point is, most of the bugs that I see introduced into the system are due to silly mistakes like the one above. This is where tools like FindBugs (which can be extended with your own rules) and CheckStyle and other bug finder / code review tools come in handy. A human code review is also a great tool. This little bug served as a reminder as to why we have such tools. To a developer this bug might seem small, but wait till an end user sees the message and says &#8216;What gives !&#8217;.</p>
<p><script type="text/javascript">// <![CDATA[
var dzone_url = 'http://www.certpal.com/blogs/2010/03/bugs-the-most-common-kind/';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_title = 'Bugs - The most common kind';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_blurb = 'Bugs are introduced into code inevitably. Most bugs are introduced due to minor errors made in logic or errors introduced when code is copied and pasted into a location. This highlights the need for tools like FindBugs';
// ]]&gt;</script><br />
<script type="text/javascript">// <![CDATA[
var dzone_style = '2';
// ]]&gt;</script><br />
<script src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F03%2Fbugs-the-most-common-kind%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2010%2F03%2Fbugs-the-most-common-kind%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2010/03/bugs-the-most-common-kind/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sun java certification pitfalls</title>
		<link>http://www.certpal.com/blogs/2009/08/sun-java-exam-pitfalls/</link>
		<comments>http://www.certpal.com/blogs/2009/08/sun-java-exam-pitfalls/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 10:46:32 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[Java certifications]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[drag and drop]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[pitfall]]></category>
		<category><![CDATA[review]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=39</guid>
		<description><![CDATA[Before you can go ahead and take the real Sun Java exams, you need to know some of the pitfalls that have plagued several test takers. Here are a few pointer Do not buy a voucher before you are prepared to take the exam: Often some candidates like to follow this approach hoping that the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fsun-java-exam-pitfalls%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fsun-java-exam-pitfalls%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Before you can go ahead and take the real Sun Java exams, you need to know some of the pitfalls that have plagued several test takers. Here are a few pointer</p>
<p><strong> Do not buy a voucher before you are prepared to take the exam: </strong></p>
<p>Often some candidates like to follow this approach hoping that the purchase of the voucher would force them to learn within a given time period. On the contrary this adds additional pressure to your preparation, since you need to study the objectives before the expiry date. Some candidates have lost money misinterpreting the expiry date on the voucher. Be wary of the <strong> MM/DD/YYYY Vs DD/MM/YYYY </strong> formats. Which one does your voucher display ?</p>
<p>When the candidates finally find that they have no more time to learn, they post a message on a forum that reads something like &#8216;I have voucher for exam X for a discount of Y%. Please call me to buy&#8217;. Remember, you are a candidate, not a voucher reseller <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong> Material made available to you inside the exam room: </strong></p>
<p>Prometric centers will not provide you with pen/paper/pencil at the time of the exam. You will be given a plastic sheet and a marker/felt pen. You can jot down on the plastic sheet with the pen.</p>
<p>Mobile phones, PDAs, and other object that would allow a candidate to cheat are not allowed. Also make sure that once you are ready, you book your exam slot in advance. This ensures that you dont spend anxious minutes waiting to buy one just before the exam.</p>
<p><strong> Beware of drag and drops: </strong></p>
<p>All Sun Java certification real exams run on a managed software. This software has a user interface where some questions require that you drag elements and drop them into their appropriate places. Beware though, once you answer a drag and drop question you <strong>cannot review</strong> its answer again. Which means if you do answer the Drag and drop question, when you try to review the answer next time the answer will be lost / reset. If you want to, you can jot down the drag and drop answer on the plastic sheet given to you and review it later.</p>
<p><strong> Code analysis: </strong></p>
<p>It is important to analyze the code presented to you even before you attempt to find the solution. Scan the entire class for well known problems before continuing your analysis</p>
<ul>
<li>Are non-static variables referred from a static context ?</li>
<li>Are there infinite loops ?</li>
<li>Is there a compile time error ?</li>
<li>Are method overridden correctly ?</li>
<li>etc etc&#8230;</li>
</ul>
<p>Once you know how to scan for such patterns, answering the question will become easier.</p>
<p><strong> Am I ready ?: </strong></p>
<p>So when do you that you are ready to take the exam ? Thats a tough one to answer. There are candidates that get 50% on the mock exam but pass the real exam with flying colors. Then there are the ones that pass mock exams and fail the real one. The latter group is rare. At the end of the day if you know what your weak points are and you are confident that you can strengthen them, go for it !.</p>
<p><a href="../../index.html">Back to home</a>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fsun-java-exam-pitfalls%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fsun-java-exam-pitfalls%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2009/08/sun-java-exam-pitfalls/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

