<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-484485182208361900</id><updated>2011-04-21T13:44:37.772-07:00</updated><category term='Coding'/><category term='JMock'/><category term='Java'/><category term='Programming'/><category term='Testing'/><title type='text'>Untitled</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://bytingme.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/484485182208361900/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://bytingme.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Vivek</name><uri>http://www.blogger.com/profile/12450683675202721918</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-484485182208361900.post-2420961185187478769</id><published>2009-01-10T04:08:00.000-08:00</published><updated>2009-01-10T04:09:25.196-08:00</updated><title type='text'>Shifting domains</title><content type='html'>I am starting to blog on bytingme.wordpress.com. Blogspot sucks, your UI sucks and so does this blog :).Meet you there. Bye! :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/484485182208361900-2420961185187478769?l=bytingme.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bytingme.blogspot.com/feeds/2420961185187478769/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=484485182208361900&amp;postID=2420961185187478769' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/484485182208361900/posts/default/2420961185187478769'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/484485182208361900/posts/default/2420961185187478769'/><link rel='alternate' type='text/html' href='http://bytingme.blogspot.com/2009/01/shifting-domains.html' title='Shifting domains'/><author><name>Vivek</name><uri>http://www.blogger.com/profile/12450683675202721918</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-484485182208361900.post-1098866283345510235</id><published>2008-08-03T05:09:00.000-07:00</published><updated>2008-08-03T06:24:03.797-07:00</updated><title type='text'>Sucky fonts in Ubuntu :(</title><content type='html'>This has always been pricking me for a long time. Whenever, I try to code in gedit, the fonts are so so sucky!!. I wanted a nice environment for coding, so my search for non-sucky ubuntu fonts started :)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Is there clear-type fonts in Linux ?&lt;/span&gt;&lt;br /&gt;Yeah, very much :). All you need to do is,    &lt;span style="font-style: italic;"&gt;System-&gt;Preferences-&gt;Appearance-&gt;Fonts-&gt;Subpixel smoothing (LCD's)&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;You are all set to experience a sexy font style :-). Guess, cleartype fonts were patented/found my Microsoft, thank you M$ :)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Cleartype is good, but I really want those Calibri,Constantia fonts ??&lt;/span&gt;&lt;br /&gt;Oh..yeah, Ubuntu can get you that too :). I found this neat scrip &lt;a href="http://plasmasturm.org/code/vistafonts-installer/vistafonts-installer"&gt;here&lt;/a&gt;. Just copy it, save it in your home folder as 'fonts-installer.sh', and say "sh fonts-installer.sh" on your terminal. It works :-).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Nooooooo, it doesn't :(. Tells me&lt;/span&gt; &lt;span style="font-style: italic;"&gt;"There is no .fonts directory in your home.&lt;/span&gt;&lt;span style="font-style: italic;"&gt;Is fontconfig set up for privately installed fonts?&lt;/span&gt;"&lt;br /&gt;&lt;br /&gt;Oh..ok, nothing to worry, when Synaptic is there :).&lt;br /&gt;&lt;br /&gt;Step 1: System-&gt;Administration-&gt;SynapticPackageManger.&lt;br /&gt;Step 2: Look for the package "fontconfig" and install"&lt;br /&gt;Step 3: Create a directory named ".fonts" in your home folder.&lt;br /&gt;&lt;br /&gt;This will definitely work!!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Uh!!.. you and ur linux :P&lt;/span&gt;. &lt;span style="font-style: italic;"&gt;It says "curl: not found"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Hmmm..Even I am wondering. Ideally, the code shouldn't throw that error, since it checks for &lt;span style="font-style: italic;"&gt;wget&lt;/span&gt;,&lt;span style="font-style: italic;"&gt;fetch&lt;/span&gt; commands on your system. Even if one exists, it should work!.&lt;br /&gt;&lt;br /&gt;Anyways, curl is a client to get files from servers using HTTP/FTP protocol. Why fear, when synaptic is here :-). Follow the steps that I have outlined in the previous comment and install curl. Cmon, now it should work!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Arggggghhhhhhhh!!!!. Chuck it, I will code in Dev-C++. I am happy. Goodbye to your gedit and your bloddy Linux!!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Hey...Chill..what is the error now ?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;trap: 47: SIGINT: bad trap&lt;/span&gt;!!!!!&lt;br /&gt;&lt;br /&gt;Could you please do this ? Just comment out line number 47. Insert # before it. If you are super frust, you can go ahead and delete it. Now, run "sh fonts-installer.sh". It should work.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Wow! wow! wow!. Yaaaaaaaaayyyyyyyy, am gonna use gedit now. It definitely rocks. Thanks a ton :-)&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/484485182208361900-1098866283345510235?l=bytingme.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bytingme.blogspot.com/feeds/1098866283345510235/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=484485182208361900&amp;postID=1098866283345510235' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/484485182208361900/posts/default/1098866283345510235'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/484485182208361900/posts/default/1098866283345510235'/><link rel='alternate' type='text/html' href='http://bytingme.blogspot.com/2008/08/sucky-fonts-in-ubuntu.html' title='Sucky fonts in Ubuntu :('/><author><name>Vivek</name><uri>http://www.blogger.com/profile/12450683675202721918</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-484485182208361900.post-8663840098615487813</id><published>2008-08-01T11:42:00.001-07:00</published><updated>2008-08-01T12:21:30.894-07:00</updated><title type='text'>Bookmarking favourites</title><content type='html'>I just found this simple technique of bookmarking your pages. I will talk wrt to Firefox, even though it can be extended to any browser. There are a number of sites like Php help, Java doc which provide an explanation of the API.&lt;br /&gt;&lt;br /&gt;How nice it would be if you were to just type "php array"  and the array documentation in Php comes up ? :-). Well it can be done. :-)&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Step 1: Create a new Bookmark (fairly simple to anyone who uses a browser)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Step 2: Right click on the bookmark and go to Properties&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Step 3: Give the location: http://php.net/%s&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Step 4: Set the keyword to php&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Step 5: On your location bar, say "php array", for that matter any function :-), the doc comes up :D&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Am trying to do a generic method for JavaDoc also. However the Javadoc provided by Sun contains webaddress like http://blah...../api/&lt;package-name&gt;/&lt;class-name&gt;. I am yet to figure out, how to have 2 different arguments passed to the command.&lt;br /&gt;Anyways, for now be happy with this :-)&lt;br /&gt;&lt;br /&gt;There is another cool functionality that you can add to this. Ever wondered, what your search results would be in Yahoo!, Google, etc..&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Step 1: Create a new Bookmark&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Step 2: Right click on the bookmark -&gt; Properties tab&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Step 3: Set location to javascript:void(window.open('http://www.google.com/search?q=%s'));void(window.open('http://www.search.yahoo.com/search?p=%s'))&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Step 4: Set keyword to search&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Step 5: Say "search vivek" on your location bar&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Up comes 2 windows, one Google result and the other Yahoo! :D&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I have another one too.&lt;br /&gt;Bookmark location: http://en.wiktionary.org/wiki/%s&lt;br /&gt;Try setting this up :-)&lt;br /&gt;&lt;br /&gt;Good Luck, have fun with bookmarks :D&lt;/class-name&gt;&lt;/package-name&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/484485182208361900-8663840098615487813?l=bytingme.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bytingme.blogspot.com/feeds/8663840098615487813/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=484485182208361900&amp;postID=8663840098615487813' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/484485182208361900/posts/default/8663840098615487813'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/484485182208361900/posts/default/8663840098615487813'/><link rel='alternate' type='text/html' href='http://bytingme.blogspot.com/2008/08/bookmarking-favourites.html' title='Bookmarking favourites'/><author><name>Vivek</name><uri>http://www.blogger.com/profile/12450683675202721918</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-484485182208361900.post-330451174839691940</id><published>2008-07-30T11:43:00.000-07:00</published><updated>2008-07-30T12:30:28.033-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='Testing'/><category scheme='http://www.blogger.com/atom/ns#' term='Coding'/><category scheme='http://www.blogger.com/atom/ns#' term='JMock'/><title type='text'>Have you tested your code ?</title><content type='html'>As I work daily, I am beginning to understand the difference between a programmer and a Development Engineer. How many times would you have written a test module for the API that you just coded ? AFAIK, it only used to be a manual testing to check whether everything is working fine. Let me take you to a new domain...lets call it the &lt;span style="font-style: italic;"&gt;'TestDomain'&lt;/span&gt; :-)&lt;br /&gt;&lt;br /&gt;In a typical S/W company, you tend to write code that satisfies some business logic. It's no longer finding out the shortest path or the MST :-). The code might involve retrieving values from databases, usage of web services, etc. Hence, each module needs to be tested. To our rescue, comes a nice neat package "JMock" !Astounded by its power, I just love writing test cases now :-).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;So, what is this JMock all about ? &lt;/span&gt;&lt;br /&gt;JMock is a library that allows you test Java code with the help of mock objects.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Mock object ? Enlighten me pl.&lt;/span&gt;&lt;br /&gt;Hmmm.... Say you need to test a module, &lt;span style="font-style: italic;"&gt;Module 1&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: center;"&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-style: italic; color: rgb(255, 0, 0);"&gt;                                                        Module 1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;                                &lt;br /&gt;                     &lt;span style="font-family:arial;"&gt;class Module1{      &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;                                                             Webservice service; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);font-family:arial;" &gt;            String ipAddr;&lt;br /&gt;           public setService(Webservice serv){&lt;br /&gt;                service = serv;&lt;br /&gt;                                                         }&lt;br /&gt;&lt;br /&gt;            public String getIpAddr(){&lt;br /&gt;                return ipAddr;&lt;br /&gt;                  }&lt;br /&gt;&lt;br /&gt;            public void function(){&lt;br /&gt;                ipAddr = service.getIPAddr();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);font-family:arial;" &gt;                                &lt;br /&gt;                if( ipAddr == blah...blah... ){&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);font-family:arial;" &gt;&lt;br /&gt;                     set ipAddr value = "val";&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);font-family:arial;" &gt;&lt;br /&gt;                } &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);font-family:arial;" &gt;else {&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="color: rgb(255, 0, 0);font-family:arial;" &gt;                          throw Exception&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);font-family:arial;" &gt;                                                                             } &lt;/span&gt;&lt;br /&gt;                                               &lt;span style="color: rgb(255, 0, 0);font-family:arial;" &gt;}   &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);font-family:arial;" &gt;                                   }&lt;/span&gt;&lt;br /&gt;There are 2 ways to test this module.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Initializing the service and checking whether the ipAddr value has been set or an exception is thrown&lt;/li&gt;&lt;li&gt;Mocking the service&lt;/li&gt;&lt;/ul&gt;We are here to discuss the 2nd point. Instead of initializing the service and making the call heavy, instead you can mock the service object, (i.e) JMock allows you to create a "mock" WebService object which to the Java code will look like a WebService object.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Still hazy&lt;/span&gt; :(&lt;br /&gt;Basically, instead of hitting the service, you try to define how the service object will work given a set of parameters. To put it crudely, it essentially means, you are trying to override the WebService functions when passed with a specific set of parameters.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Nice :-), Eg?&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;span style="color: rgb(255, 0, 0);font-family:times new roman;font-size:100%;"  &gt;import org.jmock.Expectations;&lt;br /&gt;import org.jmock.Mockery;&lt;br /&gt;import org.jmock.integration.junit4.JMock;&lt;br /&gt;import org.jmock.integration.junit4.JUnit4Mockery;&lt;br /&gt;&lt;br /&gt;@RunWith(JMock.class)&lt;br /&gt;class PublisherTest {&lt;br /&gt;Mockery context = new JUnit4Mockery();&lt;br /&gt;&lt;br /&gt;/* I am mocking the webservice object here */&lt;br /&gt;&lt;br /&gt;final WebService webservice = context.mock(WebService.class )&lt;br /&gt;&lt;br /&gt;/* Don't panic! Its simple :-) All I am asking it to do is, when the webservice object&lt;br /&gt;calls the getIpAddr() function, DO NOT call the service, instead return the value I have specified */&lt;br /&gt;&lt;br /&gt;context.checking(new Expectations() {{&lt;br /&gt;one (webservice).getIpAddr();&lt;br /&gt;will(returnValue("127.0.0.1");&lt;br /&gt;}});&lt;br /&gt;&lt;br /&gt;Module1 moduleObject = new Module1();&lt;br /&gt;&lt;br /&gt;/* Set the service used to be the mock object :-) */&lt;br /&gt;moduleObject.setService(webservice);&lt;br /&gt;&lt;br /&gt;/* When I invoke this, the original service isn't called, the mock object takes over*/&lt;br /&gt;moduleObject.function();&lt;br /&gt;&lt;br /&gt;/* Assert statments follow here */&lt;br /&gt;Assert.assertNotNull(moduleObject.getIpAddr());&lt;br /&gt;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;pre style="font-style: italic; color: rgb(255, 0, 0);" class="Java Source"&gt;src: &lt;a href="http://jmock.org/"&gt;jmock.org&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div style="text-align: left;"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(0, 0, 0);font-family:courier new;" &gt;Well JMock is a huuuuuuge library with so many functions and you can keep playing around with it. &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(0, 0, 0);font-family:courier new;" &gt;With this knowledge, you can go through the &lt;/span&gt;&lt;a style="font-family: courier new; color: rgb(51, 102, 255);" href="http://www.jmock.org/cheat-sheet.html"&gt;cheatsheet&lt;/a&gt;&lt;span style="color: rgb(0, 0, 0);font-family:courier new;" &gt; and get greater power. And ofcourse, &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(0, 0, 0);font-family:courier new;" &gt;I am always right here to help you out with anything related to JMock :-), anything :-)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:monospace;"&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;div style="text-align: left;"&gt;&lt;span style="font-size:130%;"&gt;&lt;span style="color: rgb(0, 0, 0);font-family:monospace;" &gt;&lt;span style="font-style: italic;"&gt;Btw, I forgot to tell you something, NatPryce replies to mail like a lightning! Till date, he has sent me about 10 mails!! &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Who is NatPryce ?&lt;/span&gt;&lt;br /&gt;Go figure it out :P&lt;br /&gt;&lt;br /&gt;PS: Could someone pl. tell me how to indent code snippets! The blogspot "Edit Post" tab is a piece of shit!!&lt;br /&gt;&lt;span style="font-family:monospace;"&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;pre style="font-style: italic; color: rgb(255, 0, 0);" class="Java Source"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/484485182208361900-330451174839691940?l=bytingme.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bytingme.blogspot.com/feeds/330451174839691940/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=484485182208361900&amp;postID=330451174839691940' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/484485182208361900/posts/default/330451174839691940'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/484485182208361900/posts/default/330451174839691940'/><link rel='alternate' type='text/html' href='http://bytingme.blogspot.com/2008/07/have-you-tested-your-code.html' title='Have you tested your code ?'/><author><name>Vivek</name><uri>http://www.blogger.com/profile/12450683675202721918</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-484485182208361900.post-7022976391954669642</id><published>2008-07-29T12:06:00.000-07:00</published><updated>2008-07-30T11:42:16.698-07:00</updated><title type='text'>Java vs C++</title><content type='html'>Life at Amazon.com has been pretty good except for a few intricately hidden bugs which at times are difficult to swat! Here's one nice bug, oops! (no pun intended) I wouldn't call it a bug. Fully geared up for the task given to me this morning, the keyboard had a bad day until I had to do a swap...&lt;br /&gt;&lt;br /&gt;Well, what's there in a swap after all ? Just out of college, you do tend to show a lot of gimmicks, don't you ? I too, am a faithful follower of the policy and wrote&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;div style="text-align: center;"&gt;                               &lt;span style="color: rgb(51, 102, 255);"&gt;x^=(y^=(x^=y));&lt;/span&gt; where x,y are integers.&lt;br /&gt;&lt;/div&gt;Now, compile successful, build successful, Yaaaaaaaaay!!! - it's a 1000 line code, so the celebration kind of justified :-). But wrong answer :-(&lt;br /&gt;So, I get down to the debugging stage, I do a&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;div style="text-align: center;"&gt;                               &lt;span style="color: rgb(255, 0, 0);"&gt;&lt;br /&gt;while( 1 ) { debug; }&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;After spending a lot of time (no way, am going to tell you how much I spent :P), I realised, there was a bug in my swap code!!. Can you imagine ? It was such a common question, almost appeared in every single CS written paper. But, this is Java dear :-)&lt;br /&gt;&lt;br /&gt;Googling Java specifications lead me to a new finding:&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt;"A compound assignment expression of the form &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;E1&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt; &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;op&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt;= &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;E2&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt; is equivalent to &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;E1&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt;  &lt;/span&gt;&lt;code style="font-style: italic; color: rgb(0, 0, 102);"&gt;=&lt;/code&gt;  &lt;code style="font-style: italic; color: rgb(0, 0, 102);"&gt;(&lt;/code&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;T&lt;/i&gt;&lt;code style="font-style: italic; color: rgb(0, 0, 102);"&gt;)((&lt;/code&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;E1&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt;) &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;op&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt; &lt;/span&gt;&lt;code style="font-style: italic; color: rgb(0, 0, 102);"&gt;(&lt;/code&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;E2&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt;)), where &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;T&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt; is the type of &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;E1&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt;, except that &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;E1&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt; is evaluated only once"            &lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Hence, whatever assignment happens in (x^=y) has almost nil effect w.r.t this statement!!!! This one-liner won't work&lt;br /&gt;&lt;br /&gt;To be more precise, C++ rule states&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt;"A compound assignment expression of the form &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;E1&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt; &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;op&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt;= &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;E2&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt; is equivalent to &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;E1&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt;  &lt;/span&gt;&lt;code style="font-style: italic; color: rgb(0, 0, 102);"&gt;=&lt;/code&gt;  &lt;code style="font-style: italic; color: rgb(0, 0, 102);"&gt;(&lt;/code&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;T&lt;/i&gt;&lt;code style="font-style: italic; color: rgb(0, 0, 102);"&gt;)((&lt;/code&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;E1&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt;) &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;op&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt; &lt;/span&gt;&lt;code style="font-style: italic; color: rgb(0, 0, 102);"&gt;(&lt;/code&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;E2&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt;)), where &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;T&lt;/i&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt; is the type of &lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;E1"&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;span style="color: rgb(0, 0, 102); font-style: italic;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Step 1:&lt;/span&gt;&lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="font-style: italic;"&gt; x = x^y;&lt;br /&gt;Step 2: y = y ^ ( x^y) = x;&lt;br /&gt;Step 3: x = ('x' computed from Step1) ^ (y computed from Step2 ) =&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                &lt;span style="font-style: italic;"&gt;= (x ^y&lt;/span&gt;)^x&lt;br /&gt;               &lt;span style="font-style: italic;"&gt;=y&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;PS: I could have definitely explained it better using opcodes, but I want to keep it as simple as possible. As long as the meaning is conveyed, I am happy :-)&lt;br /&gt;&lt;br /&gt;Let's see what Java does&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 102); font-style: italic;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Step 1:&lt;/span&gt;&lt;/span&gt;&lt;i style="font-style: italic; color: rgb(0, 0, 102);"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="font-style: italic;"&gt; x = x^y;&lt;br /&gt;Step 2: y = y ^ ( x^y) = x;&lt;br /&gt;Step 3: x = (x) ^ (y computed from Step2 ) =&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                &lt;span style="font-style: italic;"&gt;= x&lt;/span&gt;^x&lt;br /&gt;               &lt;span style="font-style: italic;"&gt;= 0&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;This result is attributing purely towards the wicked statement at the end :-)&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;At the EOD, I said "Thank God, there are no #defines in Java, God knows how many gimmicks I would have shown with that and how many screeching bugs would have been inside!!"&lt;br /&gt;&lt;br /&gt;I want to look at the bytecodes generated, googling how to look at it now:-). My 1st lesson, well learnt :D&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 102);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;             &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/484485182208361900-7022976391954669642?l=bytingme.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bytingme.blogspot.com/feeds/7022976391954669642/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=484485182208361900&amp;postID=7022976391954669642' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/484485182208361900/posts/default/7022976391954669642'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/484485182208361900/posts/default/7022976391954669642'/><link rel='alternate' type='text/html' href='http://bytingme.blogspot.com/2008/07/java-vs-c.html' title='Java vs C++'/><author><name>Vivek</name><uri>http://www.blogger.com/profile/12450683675202721918</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry></feed>
