on Jan 5th, 2010Extending jQuery validation plugin – custom validation

JQuery validation plugin has saved me hours of development time on projects. It contains definitions for most validation tasks that you would need, including AJAX based validations. But if you have to do anything more than the ordinary, you have extend the library yourself. Here’s an example of an extension I wrote :

Functionality : I want to capture the twitter handle of a user on a form. Most users will enthusiastically enter the entire URL like http://twitter.com/itsmeritesh, some will leave out the http:// and enter the rest. I wanted to validate that the username was a single word, didn’t contain parts of a URL (mainly slashes) and didn’t mind it being empty.

 // HTML Code
// <form name="myform"> 
// <input type="text" id="twitterUrl"> <label for="twitterUrl" />
 
function isEmpty(Val)
	{
	    if(Val.length==0) return true;
	      for(var i=0; i< Val.length; i++)
	      {
	            if( " \t\n".indexOf( Val.charAt(i)) == -1 ) 
	            return false;
	      }
	       return true;
	}
 
 jQuery.validator.addMethod("handleOnly", function(value) {
		 if(isEmpty(value)) return true;
 
		 if ((value.split(" ").length == 1) && (value.search('/')==-1))
			  return true;
		 else
			 return false;	  
	}, "Please specify only one word");
 
$(document).ready(function(){
    $("#myform").validate({
    	rules: {		
    		twitterUrl : {  handleOnly: true }
                 }
      messages: {
		twitterUrl: { handleOnly : "Only your twitter handle " }
               }
         }); 
  });

handleOnly is a new extension written by using the jQuery.validator.addMethod() method. The rule then specifies whether the return from the function must be true or false, in my case its true. The message is displayed when the condition is not met by the value.

on Dec 19th, 2009Bookmarklets are as bad as software on a CD

Bookmarklets are funny pieces of software. Even though its primarily used in the web browser, mainly to perform small nifty tasks, its one resource that you have no control over after its deployed. Unlike Mozilla addon’s that prompt for an update, general websites which update when refreshed, bookmarklets are almost as archaic as delivering software over a CD. Lets take an example of a badly designed bookmarklet: The example below is the Press This bookmarklet distributed by wordpress initially. ( I have changed some of the values of course)

1
2
3
4
5
6
7
8
javascript:
   if(navigator.userAgent.indexOf('Safari')%20&gt;=%200)
    {
         Q=getSelection();
   }else{
    Q=document.selection?document.selection.createRange().text:document.getSelection();
  }
   location.href='http://wordpress.php?username=blah&amp;text='+encodeURIComponent(Q)+'&amp;popupurl='+encodeURIComponent(location.href);

You will see that this is calling a RESTful implementation of some service with query parameters and the whole code is stored and rendered from inside the bookmarklet. Lets leave the security aspects of this implementation aside, that will require another post in itself. Such a bookmark is a nightmare waiting to happen. Imagine the trouble you would face if you changed the implementation tomorrow. You will have to write to ensure backward compatibility or take the user to a page where he/she can upgrade to the newest version of the bookmarklet. This is a bad way of delivering software.

Ideally, the bookmarklet in accordance with a shortcut/bookmark must mainly be a pointer to the actual web resource. Even for trivial bookmarklets, make sure you follow this design principle because you might want to do something different or more advanced later on. I am sure you have come across situations where you realized the bookmarklet you wrote didn’t work on a browser like Opera or Safari. It may be too late to correct it later on if people have already installed it on their browsers.

Lets now take a bookmarklet that loads the code to be executed from a script on the web. This is now the preferred means of deploying bookmarklets as it puts the control back into the hands of the web developer. This example is from the Friendfeed bookmarklet that lets you share things on FriendFeed.

1
2
3
4
5
6
7
 javascript:void((function()
  {
    var%20e=document.createElement('script');
    e.setAttribute('type','text/javascript');
    e.setAttribute('src','http://someserver.com/bookmarkletcode.js');
   document.body.appendChild(e)
  })())

All this bookmarklet does is to append a new script tag in your html page and assigns the source to the bookmarklet source. The difference now is that there is no code on the client side that is tied to an implementation. You can change the code in the bookmarkletcode.js as and when you feel necessary and be assured that the people using your bookmarklet will always use the latest version.

on Aug 7th, 2009Whats common between Free online ads and Open source

Disclaimer: I am not an Open Source hater. In fact, I am quite the opposite. I believe that for a innovative software marketplace to exist, there must be an equal mix of open and closed source softwares. This also borders on a project that I did on multiagent systems.

There are claims that Open Source will one day completely displace closed software. Same goes with free online advertising, sometimes also referred to as ad exchanges, where a person places an ad on his/her site and in turn gets his/her ad placed on somebody else’s site. There are claims that free online advertising will one day displace Google’s monopolistic hold on online advertising. Frankly I think most of these claims are exaggerated and baseless to a certain extent.

Am I the only one who has a problem with transactions that do not obey the traditional laws of Economics? The market, or as Adam Smith would say – the foundations of human society,  runs on the buy-sell model. When something is free, it means other non quantifiable commodities get traded, like goodwill and trust.  In such circumstances fairness is the biggest casualty. But is there really a model which can ensure fairness in these situations ?

Yes, there are plenty of trust based networks at play in our real life, but they all reach a saturation point. A typical game theoretic scenario, the person to defect first gets a very high payoff ; In a network, everyone else suffers. People will cheat, one way or another. Like I mentioned before, fairness becomes a higly debatable issue. Though, the categorical imperative bestows humans with decision making powers, it doesn’t state the thresholding for these decisions. The only final outcome of these networks is that one person is going to hit it big and make the others suffer.

Naturally the follow up question will be:  how does Open source survive? I explain this using results from game theory. In any homogeneous population, ie either a defecting or a co-operating , a small group of people,  co-operating with each other while defecting against the general consensus , always has the ability to hold their own and, in some cases, even win against the entire population. Examples of this are plenty in real life,  for ex: corruption or the mafia. So, as long as that small bunch of people stay loyal to each other, such networks will continue. Open Source, is slightly a more complex beast than I described it to be, but it broadly falls into the category I mentioned.

Incentive mechanisms like reputation points, virtual currency etc usually try and emulate a real world materialistic economy, but they are limited in their reach and appeal. Eventually, people loose the initiative to compete for these non materialistic resources. Besides, in a medium such as the internet, where identity itself is questionable, how will people trust each other. Has anyone ever wondered if those mails, which inform you of the billions you have won, were ever real? I guess not.

In conclusion,  I ask, is there some mechanism design or a strategy that can ensure fairness (provided its defined accurately) whilst ensuring the continuing growth of such trust based networks ?

on Jul 24th, 2009One Decade of Programming

Sometime around July 1999 was the time I wrote my first “Hello World” program.  Yes, its been 10 years since I started programming, and I dont mean L R L T of Logo. In fact, I wrote some BASIC code as early as 1995-96, but I will skip that for reasons beyond the scope of this post.

I remember liking programming for lots of reasons, but one which I particularly remember. Unlike the other subjects in school, like math or physics,  programming had no boundaries or a legacy to deal with. By legacy, I mean there were no equations, constraints and I didn’t have to reciprocate what some mathematician proved 400 years ago. I have always enjoyed freedom and programming gave me that freedom to express myself.  I guess, I can claim that most good programmers have taken up programming because it lets them play God or be creationists of some complex entity.

Anyway, there are a lot of interesting and fun facts through these ten years and I shall pen some down.

  1. BASIC was my first programming language. PASCAL was probably my first compiled language.
  2. I wrote my first sorting routine ( an act I call, truly understanding the programming abstraction) sometime during Jan 2000.
  3. I got my first computer in 2001. AMD Athlon 1.7 GHz with 256 MB Ram and 40 Gb harddisk . “OMG 40GB” , was my friends reaction.
  4. I disliked C early on, due to my PASCAL roots, but grew fond of it later on when I completed my first large scale project ( A linux text editor, which I proudly wanted to call, “Better than VI“). This is also where I wrote my first recursive functionality which resulted in people actually wanting to read and understand my code.
  5. My first exposure to Linux and OSS was in 2002, I think it was RedHat linux 7.1 with running Xwindows.
  6. I almost lost my entire project due to a floppy malfunction. USB’s were expensive and uncommon then. CD writers expensive.
  7. I learnt about data structures in 2002.  My lecturer, one of the few good ones, was kind enough to teach us the intuition behind data structures and write the implementations ourselves. Our other lecturers taught programming from a book and expected us to replicate the programs written in the book.
  8. I would like to thank  my data structures lecturer who spent hours helping me debug and correct programs. It was probably the first time I looked at code from an outside perspective and yes, I realized the importance of indenting code then and there. I have never not indented my code again. (Remember we are still in 2002).
  9. I learnt about OO during christmas vacations of 2002. Loved it. Loved the paradigm shift in the way I thought about programs
  10. My first OO program was one class with 50+ methods in it (Yup, there was nothing OO about it, but its a start)
  11. My first large scale OO project was building a Paint like utility using C++. I am still proud of this work because I learnt about programming abstractions and class reponsibilities here. I still remember writing my first button class which was fully reponsible for itself.
  12. I learnt PERL in 2004 and wrote my first web application. An online messaging, calendar and collaboration utility for friends and corporates. ( yes, this was my first social app). When I saw Myspace very soon after that ( MySpace was still pretty bare in terms of features then) the only difference I saw was that other people could see who my contacts(friends) were and network with them.
  13. When I first discovered databases (2003), I thought, “wow, somebody made my file I/O’s really easy”. It was also an amazement because I had thought of a unified file writing and reading mechanism to solve all my I/O woes. Problem was getting it to work with many languages. I couldn’t get my head around it and thats when I learnt about databases. Imagine my surprise and rejoice. The first database I used was Oracle and later MySQL ( it wasn’t even relational then). My database project earned me 100/100 in my finals.
  14. Compiler Design was one of my favorite subjects. I loved the fact that I could understand how they build programming languagues. Lex Yacc was probably the best thing that ever happened to programming. Prateeksha and I wrote the specifications for a shift reduce parser for C++. We used an A1 sized sheet of paper to write down the shift reduce matrix. After two full days writing the Shift Reduce rules, we realized we had messed up somewhere.
  15. I wrote a prototype of my CASE tool in 2004.  My proud entry to the world of .NET. I had my official copy of Visual Studio.NET 2003 and .NET 1.1 . This CASE tool would eventually become my fnal semester project and also enter Microsoft’s Imagine Cup. We reached the Nationals for the competition. I called it Dzine.
  16. Even though I had learnt java in 2004, I didnt use J2EE till late 2005. when I started writing simple J2EE apps. I also figured that javascript, that language I had used to validate my controls in HTML (in 2004) , had suddenly taken the world by storm. Everybody was talking about it. Web 2.0.
  17. I sat for two whole days to figure out how yahoo mail was autocompleting email addresses I typed. After two days and a whole lot of searching, I learnt about AJAX. I was already on Gmail by then and didnt really understand the underlying plumbing that was holding the application together.
  18. Within the next 4 months I put AJAX and J2EE together to work on Samparkh with Prateeksha. I wrote an online chat application using AJAX ( inspired by Meebo). Remember this was a time when firebug wasn’t around and I used a tool called Venkman, which I am sure many of you haven’t even heard of.
  19. Then, Grad school happened and so did Microsoft, and the list of wonderful projects that I did during the two years I spent there.
  20. Special mention to BigKahuna, which took almost two years to perfect and won the Google Product Engineering Competition 2009.

Am I a good programmer? I dont know. But, I will continue to remain a programmer. Most of my friends know that I dont take sides. By sides, I mean OSS v/s Closed source, Linux vs Windows , Google vs Yahoo, and this is because of the vast and varied experiences I have had with all these different entities during programming. They all have a special place in my heart and I cannot choose one over the other. Programming is changing fast, and all I can hope for is that that I dont wear out of ideas or skills to call myself a programmer in the years to come.

A list of all the wonderful projects that I have pursued over the years is available here.

on Jun 15th, 2009State of higher education in India with a focus on Computer Science

I came back from attending a session which spoke about the state of graduate education in India and here is the summary:

  • Just over 450,000 students in India graduate with an Engineering degree
  • 150,000 students amongst them with a degree in either Computer Science or Information Technology.
  • There are about 1500 Engineering colleges in India.
  • Many of these colleges don’t even have a full professor on their rolls.
  • Currently there are about 750 students pursuing a Phd in 15 of the most reputed institutions in the country which means that, about 80 to 90 students graduate with a Phd from one of the 15 reputed institutions in India.
  • The 15 reputed institutions include the IIT’s, NIT’s, two of the IIIT’s (Hyderabad and Bangalore) and some autonomous institutions like BITS and Vellore.
  • The percentage of students who take up graduate education after their engineering in India is drastically low.
  • About a quarter of the students who secure Phd’s from universities from the US are Indians.
  • Students of Indian and Chinese origin make up half the graduate schools students in America.
  • Most people who secure their Phd’s from universities in India either join small and focused research groups in IT companies or take up faculty positions.
  • This year the amount of students applying for graduate education has increased dramatically, which only is reassuring evidence that graduate education is seen as a substitute for jobs and not as something of value.
  • A couple of IIT’s got about 700 applications for masters and phd positions.

Apart from all this the research output in India is not very high. Groups doing theory are considered to be doing some of the state of the art research, the other departments are not very highly regarded (I have a problem with this generalization, but we will keep that for another discussion). The researchers present in the discussion had plenty of points to contribute for the dismal state of higher education and some of the points mentioned were :

  • Lack of good, trained and motivated faculty members. This was attributed to the fact that salaries in academia were not on par with that of the industry. (pay commission’s revisions should do some good in this direction)
  • Lack of exposure to opportunities, challenges and rewards of research careers. ( this is true for colleges that are not very reputed, the quality of the faculty members are not up to the mark, which means they don’t have enough exposure … you get the point)
  • Societal pressures for securing jobs, that too through college placements, rather than pursuing something that the student really wants to do. A survey of the choices of the students during the engineering seat selection process will ascertain this fact. I even know of people who took up courses they had no interest in just because it was in a college where the placements were good.
  • Lack of funding for graduate students to attend conferences, workshops etc. ( though this was contested by a lot of people, I think , the problem lies in making the students aware of the funds that are available for such purposes )
  • Discrimination against the students who graduate from the IIT’s versus other institutions. (though strong alumni networks are not anything new, other colleges should target to strengthen their alumni networks and not work as silo’s )

This is where I found the IIIT’s (particularly Hyderabad and Bangalore) to be very innovative in their approach. They are situated in the heartland of what can be considered seat of innovation in India. Both of them have strong collaboration with the Indigenous and multinational companies based out of their respective cities and provide for a wonderful platform for students to explore a mix of both academic research and industry relevant parts of the information technology industry. Both IIIT-H and IIIT-Bangalore have achieved recognition for their quality in the industry and academia, and that too in good time. I am positive that in a few years time, these institutions will be deeply connected to the research and development communities of the information technology industry in India  and will contribute significantly to the intellectual output of the country.

disclaimer : the numbers mentioned in this post are thanks to Ashwani Sharma, part of the External Research Programs  team at Microsoft Research India.