<?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>The Shyam &#187; javascript</title>
	<atom:link href="http://theshyam.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://theshyam.com</link>
	<description>Ramblings from the Real Shyam; You know, unlike those other fake Shyams!</description>
	<lastBuildDate>Sun, 08 Jan 2012 13:07:53 +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>APIs and what not to do</title>
		<link>http://theshyam.com/2010/04/apis-and-what-not-to-do/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=apis-and-what-not-to-do</link>
		<comments>http://theshyam.com/2010/04/apis-and-what-not-to-do/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 20:41:57 +0000</pubDate>
		<dc:creator>shyam</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://theshyam.com/?p=301</guid>
		<description><![CDATA[APIs seem to be like opinions. Everyone has one, and no two people have the same concept of what constitutes a good one. An API is supposed to be an interface that is exposed for other programs or programmers to use to interact with your code. Except, each API, like an individual, is unique with [...]]]></description>
			<content:encoded><![CDATA[<p>APIs seem to be like opinions. Everyone has one, and no two people have the same concept of what constitutes a good one. An API is supposed to be an interface that is exposed for other programs or programmers to use to interact with your code. Except, each API, like an individual, is unique with its own flaws and niceties. A great API is one which reduces the amount of code you have to write when you use it. I personally feel amazing if I can get something done with minimal code. That just screams &#8220;GOOD API&#8221; to me.</p>
<p>On the other hand, a bad API leaves you feeling dirty, unclean even, as if you are committing grave sins against nature even by just using it. Here are a few common mistakes which end up leaving that bad taste in your <span style="font-size: 13.3333px;">mouth (with examples, of course!) :</span></p>
<p><strong>Bad APIs</strong></p>
<p>These are the worst offenders, the APIs which are supposedly there to make your life easier, but just end up making it more work to use it than rewriting it from scratch. I faced one of the bigger offenders of this one recently when I was working with GWT. I was trying to create a tree structure to represent a navigation hierarchy when it dawned on me.</p>
<p>A GWT <a href="http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/user/client/ui/Tree.html">Tree</a> is created by creating a Tree object, and then creating a tree item for each node. To append children to each node, you create further tree items and add whatever text or elements you want to it. So to summarize, even if I have a data structure to represent my tree (which in most cases, I do), I will have to traverse it manually, create tree items, tell each one how to render itself and then append it to the correct items. Yuck.</p>
<p>Now consider how JFace creates a <a href="http://www.eclipse.org/articles/Article-TreeViewer/TreeViewerArticle.htm">Tree</a> (which I consider much more powerful and a nicer API altogether). You create a TreeViewer, set its data source / input. Then, you set a content provider which knows how to traverse your data object and get children / parents. You can also set a LabelProvider which tells it how to render its data elements. End result? Nice clean code that I actually feel satisfied about.</p>
<p>Most of these are the end result of rushed / not well thought out design. Having a concrete use case prior to designing it should have been enough to scream out &#8220;Its ugly!!!&#8221;. Suggestion to prevent this : write a test / use case for anything you start designing, so you can get a feel for how it feels in action. That should help you avoid a lot of these.</p>
<p><strong>Not fully thought out APIs</strong></p>
<p>This one is similar to the previous one, but I think it deserves section and example of its own. This happens when you almost nail the API, but fail to consider some common uses of the API. The biggest offender of this one I believe is the Java List API.</p>
<p>The two most common use cases I have in Java when I work with lists are<br />
<span style="font-size: 13.3333px;">1.) Iterating through them to perform some operation and<br />
2.) Filtering the list to get a subset</span></p>
<p><span style="font-size: 13.3333px;">The second operation is so common that I get annoyed now that I have to create an empty list, iterate through each one using a for each and conditionally add elements to the new list. Now I realize that Java doesn&#8217;t make it easy to pass in functions (check my older <a href="http://theshyam.com/2009/11/what-i-miss-in-java/">article</a> about this) as arguments, but what I really really want here is the ability to do myList.filter(predicate) where predicate is a predicate function I decide, which returns the filtered list with elements matching the predicate.</span></p>
<p><span style="font-size: 13.3333px;">There are many more such common operations missing on the List interface, but this is the most egregious one I believe. Javascript also gets this wrong, but <a href="http://documentcloud.github.com/underscore/">underscore</a>, a JS library adds a lot of this which makes working with lists and collections a dream. </span></p>
<p><strong>Misnamed APIs and methods</strong></p>
<p>How many times have you called a method, only to realize that it didn&#8217;t really do what you thought it did? Or look for a method XYZ, only to realize later that it had been named YXZ instead. Raise your hands if you have experienced this. For some reason, an apple for someone almost always turns out to be an orange for someone else.</p>
<p>I&#8217;ll switch to bashing on JS for this one, <a href="http://documentcloud.github.com/underscore/">underscore</a> in particular. For all the amazing methods that underscore provides in JS, they really have a problem with naming. I ended up looking for a collection.contains method, and ended up finding only indexOf, so I initially assumed that they didn&#8217;t have it. I mean, if I look for contains, at best, I will also look for has, hasKey. Browsing through the list of method names, I might have even accepted includes (though it would not have been my first choice). But never in all my life would I have expected it to be include (Yes, that is include, as in singular!). People, what were you thinking????</p>
<p><strong>Liars</strong></p>
<p>The final set of APIs which can annoy (but are easily worked around, just like the previous section) are APIs which lie. These include APIs which don&#8217;t do what the function name suggests it does (no obvious example from the open source land comes to mind, thankfully). The other kind is one which is not done with work even after the object is created. Most times, it is the case of a lurking init / initialize method. And if you ever see an interface called Initializable, run in the opposite direction.</p>
]]></content:encoded>
			<wfw:commentRss>http://theshyam.com/2010/04/apis-and-what-not-to-do/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Is Strong Typing really needed?</title>
		<link>http://theshyam.com/2010/03/is-strong-typing-really-needed/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=is-strong-typing-really-needed</link>
		<comments>http://theshyam.com/2010/03/is-strong-typing-really-needed/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 19:36:40 +0000</pubDate>
		<dc:creator>shyam</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[dynamic languages]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://theshyam.com/?p=282</guid>
		<description><![CDATA[This is something I have been struggling with for the last few months. I have had people argue ardently that all Strong typing is good for is false comfort and lots of unneeded typing. But I was strong. I was undeterred. I dismissed this as the crazy rants of those JS developers, those dynamic language [...]]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste">This is something I have been struggling with for the last few months. I have had people argue ardently that all Strong typing is good for is false comfort and lots of unneeded typing. But I was strong. I was undeterred. I dismissed this as the crazy rants of those JS developers, those dynamic language people who believe that obfuscation and compactness is everything, even at the cost of maintainability. I mean, how could a language where you didn&#8217;t even know what was getting passed in in any ways better than something where the APIs are explicit and stop you from making mistakes. A dynamic language could work for a single developer, but definitely not for a team. That was my whole hearted conclusion.</div>
<div id="_mcePaste">Now, I&#8217;m not so sure anymore. Its been 3 weeks since our team made the whole hearted switch. Has it been roses and sunshine? No. But it hasn&#8217;t been as bad as I expected it to be. And there are a few reasons for that. But before that, I&#8217;ll lay down the pros and cons the way I see them from my (assuredly very limited) experience :</div>
<p></p>
<div id="_mcePaste"><strong>Benefits of Strong Typing :</strong></div>
<div>
<ol>
<li><strong>Errors / Warnings in your editor</strong><br />
Simply put, this might just be the single most greatest benefit of strong typing, and the single reason why most java developers (a lot rightly so) will never even consider leaving the safety of strong typing. While compilation support doesn&#8217;t necessarily go hand in hand with strong typing, most people tend to associate Java with it, so lets run with that. Simply put, with Strong typing, your editor can (and should, I mean, if you are not going to get immd. feedback, what&#8217;s the point?) give you immediate feedback when you messed something up. Whether this be using the wrong variable name or trying to call a method that either does not exist or with the wrong parameters. Or if you are trying to use the wrong type of object.</p>
<p>To a Java developer, an IDE like Eclipse or IntelliJ is godsend, as it tells you what is wrong in your world and lets you jump to them, gives you suggestions and autofixes and generally makes your life as painless as it can. And it is brilliant, I can tell you that.</p>
<p>In Javascript (or any other dynamic language), everything is fine and dandy for the first 100 lines. After that, it becomes scarily unmanageable. The only way around this that I have found so far is to be super paranoid and write tests for every single line of code. If you can&#8217;t do that, stay far far away.</li>
<li><strong>Generics (but this is also a negative, in my opinion, which I&#8217;ll get to below)</strong><br />
The idea behind generics is that gives developers some assurances about the types in a collection (or whatever it is you are genericizing). That way, all operations are type safe, without having to convert to and from different types. And you are assured that you will not be surprised suddenly by a different type of object popping up when you least expect it. But there are a lot of issues with them that I&#8217;ll cover in the second section.</li>
<li><strong>Ability to follow a chain and figure out what type of object is required at each step</strong><br />
Now this is something I definitely miss in languages like Javascript and Python. The fact that I can trace (in my IDE, note that part) what the type of each variable / method call in an expression chain is simply amazing, especially when you are working with a new codebase. You never have to wonder what the parameter types of the method you are calling are. You don&#8217;t have to wonder what methods are available or visible. You just know this information (Again, assuming you are using an IDE. If not, god help you)</li>
<li><strong>Refactoring</strong>
<p>The biggest advantage of Strong typing though, in my opinion, is the ability to create IDEs which make refactoring a breeze. Renaming a method / variable? Trivial. Moving or extracting a method? Simple key combination. Stuff which can be extremely tedious and mind numbing are accomplished in a matter of minutes. (Want to know more about these shortcuts? Check out Eclipse shortcuts). This is simply not possible with languages like Python and Javascript.</li>
</ol>
</div>
<div id="_mcePaste"><strong>Disadvantages of Strong typing :</strong></div>
<div>
<ol>
<li><strong>More concise and precise, less typing</strong><br />
Dynamic languages do tend to be more dense, and it is much easier to accomplish in 10 lines what can easily take 50-100 in a language like Java, which is especially verbose. Consider trying to pass in a chunk of code to be executed at the end of a function in both Java and javascript (this is pretty common in web apps and task runners)<br />
<em>Java :</em></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">interface</span> Function <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; T execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// Optional parameters is not easy here :(</span><br />
<span style="color: #009900;">&#125;</span><br />
taskRunner.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>taskArgument, <span style="color: #000000; font-weight: bold;">new</span> Function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Success&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p><em>Javascript</em>:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">taskRunner.<span style="color: #660066;">execute</span><span style="color: #009900;">&#40;</span>params<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>response<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Success&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
</li>
<li><strong>No badly implemented generics</strong><br />
This is mostly Java&#8217;s fault of getting generics pretty badly wrong. The idea behind generics is sound, its the implementation that is horribly broken. Here are a few things which are wrong with it :<br />
<em>Type erasure</em> : This basically involves the fact that at runtime, there is no way to differentiate between say, a List&lt;String&gt; and a List&lt;Integer&gt; If you never work with reflection or Guice, then this might not be a problem. But it also is a pain with deeply nested generics and wildcards. I have seen compiling code which blows up at runtime because it cannot differentiate between a Provider&lt;? extends Repository&gt; and Provider&lt;? extends Resource&gt; and neither Resource nor Repository have anything in common. Crazy&#8230;.</p>
<p><em>Verbosity</em> : Map&lt;String, List&lt;String&gt;&gt; myMap = new HashMap&lt;String, List&lt;String&gt;&gt;();. Enuff said.</p>
<p><em>Guice &amp; Reflection </em>: Generics and java.lang.reflect just don&#8217;t mix. They just don&#8217;t. Type erasure blows away all type information, so you are bound to be using stuff like new Entity&lt;?&gt; which totally defeats the purpose. And don&#8217;t get me started on Guice. In guice, normal bindings (non generic classes) look as follows :</p>
<p><em>bind(MyInterface.class).toInstance(instance);</em></p>
<p>With Generics involved, they now look as follows :</p>
<p><em>bind(new TypeLiteral&lt;MyInterface&lt;String&gt;&gt;(){}).toInstance(instance);</em></p>
<p>What the heck just happened there???</li>
<li><strong>Closures / Functions :</strong><br />
Closures are a form of anonymous inner functions which can have an environment of their own, including variables bound to the scope of the function. The inner function has access to the local variables of the outer scope and can change state. But what it does allow is creating functions, as callbacks or for performing some quick little task in a repeated fashion, easily and quickly and pretty darn cheaply.Java has had a few proposals to add it (http://javac.info/) but it has not passed the review committee yet. And probably won&#8217;t for the next few years. So till then, in Java, you are stuck creating interfaces, creating an implementation of it at runtime, passing in variables you need access to in the constructor or through some other mechanism, and generally be in a lot of pain. Thanks, but no thanks.</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://theshyam.com/2010/03/is-strong-typing-really-needed/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Super fast JS Testing</title>
		<link>http://theshyam.com/2009/08/super-fast-js-testing/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=super-fast-js-testing</link>
		<comments>http://theshyam.com/2009/08/super-fast-js-testing/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 16:42:43 +0000</pubDate>
		<dc:creator>shyam</dc:creator>
				<category><![CDATA[Technical]]></category>
		<category><![CDATA[Tool]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[jstestdriver]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://theshyam.com/?p=164</guid>
		<description><![CDATA[Before I jump into how exactly you can perform super fast and easy JS testing, let me give you some background on the problem. Javascript is a finicky language (Some people even hesitate to call it a language). And it can easily grow and become a horrible and complicated beast, incapable of being tamed once [...]]]></description>
			<content:encoded><![CDATA[<p>Before I jump into how exactly you can perform super fast and easy JS testing, let me give you some background on the problem.</p>
<p>Javascript is a finicky language (Some people even hesitate to call it a language). And it can easily grow and become a horrible and complicated beast, incapable of being tamed once let loose. And testing it is a nightmare. Once you have decided on a framework (of which there are a dime a dozen), you then have to set it up to run just right. You need to set it up to actually run your tests. Then you have to figure out how to run it in a continuous integration environment. Maybe even run it in headless mode. And everyone solves it in their own ways.</p>
<p>But the biggest problem I have with most of these frameworks is that executing the tests usually requires a context switch. By that, I mean to run a JSUnit test, you end up usually having to open the browser, browse to a particular url or html page which then runs the test. Then you have to look at the results there, and then come back to your editor to either proceed further or fix your tests. Works, but really slows down development.</p>
<p>In java, all it takes is to click the run button in your IDE to run your tests. You get instant feedback, a green / red bar and details on which tests passed and failed and at what line. No context switch, you can get it to run at every save, and proceed on your merry way. Till now, this was not possible with Javascript.</p>
<p>But now, we have <a href="http://code.google.com/p/js-test-driver/" target="_blank">JS Test Driver</a>. My colleagues Jeremie and <a href="http://misko.hevery.com" target="_blank">Misko</a> ended up running into some of the issues I outlined above, and decided that going along with the flow was simply unacceptable. So they created a JS Testing framework which solves these very things. You can capture any browser on any machine, and when you tell it to run tests, it will go ahead and execute them on all these browsers and return you the results in your client. And its blazing fast. I am talking milliseconds to run 100 odd tests. And you can tell it to rerun your tests at each save. All within the comforts of your IDE. And over the last three weeks, I have been working on the eclipse plugin for JS Test Driver, and its now at the point where its in a decent working condition.</p>
<div id="attachment_165" class="wp-caption aligncenter" style="width: 438px"><a href="http://theshyam.com/wp-content/uploads/2009/08/JS-Test-Driver-Plugin.png"><img class="size-full wp-image-165" title="JS Test Driver Plugin" src="http://theshyam.com/wp-content/uploads/2009/08/JS-Test-Driver-Plugin.png" alt="The plugin in action" width="428" height="478" /></a><p class="wp-caption-text">The plugin in action</p></div>
<p>The plugin allows you to, from within Eclipse, start the JS Test Driver server, capture some browsers, and then run your tests. You get pretty icons telling you what browsers were captured, the state of the server, the state of the tests. It allows you to filter and show only failures, rerun your last launch configuration, even setup the paths to your browsers so you can launch it all from the safety of eclipse. And as you can see, its super fast. <strong><em>Some 100 odd tests in less than 10 ms</em></strong>. If thats not fast, I don&#8217;t know what is.</p>
<p>For more details on JS Test Driver, visit its <a href="http://code.google.com/p/js-test-driver/" target="_blank">Google Code</a> website and see how you can use it in your next project and even integrate it into a continuous integration. Misko talks a little bit more about the motivations behind writing it on his <a href="http://misko.hevery.com/2009/05/22/yet-another-javascript-testing-framework/" target="_blank">Yet Another JS Testing Framework</a> post. To try out the plugin for yourselves, go add the following update site to eclipse : <strong>http://js-test-driver.googlecode.com/svn/update/</strong>. For all you IntelliJ fanatics, there is something similar in the works.</p>
]]></content:encoded>
			<wfw:commentRss>http://theshyam.com/2009/08/super-fast-js-testing/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

