Wednesday, February 25, 2009

How to handle a bomb threat ?

It was a day when the facilities team in the company I work for decided to release a handbook with useful information for employees and in order to coax employees to come in and get the book, a sweet treat was thrown in. The icing on the cake was definitely the last page of the book with instructions on how to handle a security threat like a bomb on premises. I am reproducing verbatim from the handbook.

[quote]

Bomb Threats

Stay calm and ask the following questions

1. Where is the bomb ?
2. When will it go off ?
3. Why has the bomb been placed there ?
4. By whom has the bomb been placed ?

[unquote]

I am not sure if this was intended to be a funny read but it nevertheless is. The first and probably only instruction I would have expected when there is a bomb on premises is "Run (or walk if you prefer) for your lives". I know this is probably not sound advice in crowded places but an office facility is not so much crowded. People are usually distributed across floors or buildings and it must be fairly straightforward to get them to leave without causing a stampede.
I am not sure me or any other sane person would just stay calm and wonder where the bomb is, who put it there or the intended motive. I wonder if someone actually gets paid to write such funny stuff for employee handbooks, it sure did lighten up the office environment for a while.

Friday, June 20, 2008

Old and dying ...

I used to have a friend, someone I have known pretty well for nearly 10 years now. I met him first when I was in high school. He was young, energetic and raring to go then. He was noticed wherever he went. He had a huge fan following. He was helpful to me in so many ways, he helped me to keep in touch with friends, he kept me updated with all the latest and greatest news, he even delivered love letters for me. 

He was cool, a rock-star of sorts, a trusted companion and meant a lot to me. There was a time when I simply couldn't have survived without him. 

Alas, great things are never meant to last. I ignored him of late, and so did a lot of my other friends. Because there was this new kid who was way cooler, more friendlier and infinitely more useful to me and my friends. We even wanted to join this new kid in his mission to take over the world. 

Selfish warts us ! We deserted our old friend and went behind the new and shiny kid. And we forgot about our good old friend. So many followed suit and our old friend was left alone and vulnerable. His strength reduced greatly, he lost all will to fight and was readying to kill himself. I heard last that he was searching for a place to lie down and die peacefully. 

I am writing this to thank my old friend for all that he has done, an obituary of sorts. He made life better for so many of us over the years, but has outgrown his usefulness. He is a crazy old cock now, trying to survive and kill himself at the same time. Senility does that to you sometimes. 

Well, my old friend. The time has come for us to bid our final good bye. I hope you have a peaceful death. I hope this man who has been making life miserable for you will leave you alone at least in your deathbed.

You always loved purple didn't you, we promise to place a wreath of purple flowers on your coffin. 

Goodbye Yahoo ! You will be missed !

Monday, April 21, 2008

Native American Wisdom

A Native American elder once described his own inner struggles in this manner:

"Inside of me there are two dogs. One of the dogs is mean and evil. The other dog is good. The mean dog fights the good dog all the time."

When asked which dog wins, he reflected for a moment and replied, "The one I feed the most."

Monday, March 17, 2008

Interesting tech interview questions

I've given numerous tech interviews in the span of the last 4 years or so and I thought I could take time to share some question from a few of them. Of course, I don't intend this writeup to be a ready reckoner for interviews, you might be better off going here. I rather wanted to share some good and some not so good questions I have been asked in tech interviews just to give an idea of the staggering variety and yet the common undercurrent in these questions.

I must admit that though I love interesting questions, I generally hate questions that rely on quirks or little known facts in a language or technology. These questions are binary in nature, you either know the answer (because you have read it before) or you don't know. These binary questions are pretty difficult to figure out by thinking hard. They also don't allow a discussion around the question, which is the whole intent of the interview: "to get the candidate talking on technical things and figure out how good he/she is". Joel has an excellent article on this here.

Now, on to the questions.

1. How would you implement your word count program in C++ ? (this was in a phone interview).

Fair enough, I got going about how I would implement it only to be stopped by the interviewer abruptly. He didn't want to know about my logic, he just wanted the code. I started off rattling that I would parse my input string only to be interrupted with "But you don't have a main() yet, so things are not going to work". I started to say that main() would call my wordcount() method only to be interrupted again with my code missing preprocessor directives and a return type for main(). By this point, I was a bit flummoxed because he was expecting me to recite code over the phone. This continued for the next 15 minutes where he pointed out every missing semi colon and brace. At the end of it all, he started comparing my code with a solution he had handy and emphatically remarked that I had made 16 deviations from what was optimal code. And most of these deviations were in the parameters passed and the data types used. What a dork !

The basic idea behind this question is to identify words in a sentence(s). This can be done by identifying word separators and incrementing the word count whenever we encounter a separator. The usual word separators (the characters that separate one word from another) are ' ' (whitespace), comma, period, semicolon and newline.


2. How would you print out something before entering main() in C++ ?

Now, this seems to be a famous interview question yet binary in nature. If you know the fact that global class objects get initialized before main gets called or global variables who initialize from a function return value will be called before main, you would be able to answer this question. I didn't know the latter and was interested in knowing why someone would want to print something before main() gets called. The interviewer simply said "we prefer our programmers to work on requirements without asking too many questions". No wonder I decided not to talk to them ever after.

The usual ways to do this, declare a global variable that gets initialized from the return value of a function and execute your print within the function.

int a = foo();
int main()
{
...
}

int foo()
{
std::cout << "printing before main"; return 42; }


Global class objects will be constructed before main gets called and hence, putting a print statement in the constructor of the object will achieve the same results.

class A
{
A()
{
cout << "Printing before main"; } };



3. What is Coercion by Member Templates in C++ ?

Coercion by what ? I could only take a guess because this seems to be a C++ idiom I am not aware of. This was followed by a question on Policy templates and Policy classes. Flummoxed again. The final nail in the wall was "What is RAII". I at least knew it stood for "Resource Acquisition Is Initialization". It's a technique used for ensuring clean-up for dynamically allocated storage and usually involves putting some code in the destructor that will be invoked always and will trigger clean-up. I had some follow up questions on problems with RAII and I couldn't think of any other than C++ error throwing mechanism that could prevent the clean-up code from getting called. That apparently was not the right answer because the interviewer shook his head vigorously.


4. How would you sort 1 million integers with only 2 MB memory and no storage. You are allowed to output results to a display. Optimize the solution for space first and then time.


Cool. This is an interesting question in many ways because there is no one line answer and no truly best solution. Hence, it leaves a lot of room to discuss CS concepts. I am not going to try and answer this question here since it's too involved but I would like to point out that choosing the correct data structure, choosing the correct (sorting) algorithm and applying data partitioning and slicing techniques would be the general direction to take.


5. What is lazy evaluation in C++


It's a technique that is alternately referred to as "construct on first use". It involves using a local static and putting that local static into a function. eg. If I want to call a method JumpAround() with a "foo" object inside another struct/class and if the foo object is not initialized before calling the method, it could lead to strange errors.

To fix this, we call the method JumpAround() like this

bar().JumpAround()

bar()
{
static foo obj;
return obj;
}


6. What is a virtual constructor in C++ ?

It is a feature that is not supported by the language itself. Let us examine why. The keyword "virtual" implements run time polymorphism or late binding. In an inheritance hierarchy, it is usual to call any class method using a base class pointer (or reference). Since a base class pointer has enough information to call any derived class method which is originally declared and/or defined in the base class, this idiom allows us to use a single pointer and call any member function in an inheritance hierarchy.

We fully expect function binding to happen, based on the "pointed to" object and not on the type of the pointer itself (which is merely a container). "virtual" merely ensures that this late binding or object type determination happens correctly. The keyword here is "type identification".

Based on the type of the object pointed to, we expect the base class pointer to call the method of that object. Since this whole implementation depends so heavily on the type of the object, we expect that the object is fully constructed and open to introspection and run time type identification (RTTI).

If we wanted a virtual constructor idiom, we are asking to call a constructor (which is also a member function) based on type determination of an object which is not fully constructed yet. Essentially we are trying to call a member function on an object that is still undergoing construction. This could lead to difficult errors and hence the language doesn't allow constructors to be virtual. (Destructors on the other hand can be virtual and are usually so when you have virtual functions in a class. "Thinking in C++" by Bruce Eckel available here has an excellent chapter on this, read it)


7. Does late binding work inside a constructor ? [OR] Can we call a virtual function inside a constructor ?

You can call a virtual function inside a constructor but you might not get expected behavior always. This is because the virtual mechanism breaks down inside a constructor and all function calls are mapped to member functions of the same class as the constructor. No late binding or type determination happens and this is precisely the reason why constructors themselves can't be virtual. Read the previous answer and also Eckel's chapter on "virtual" to understand this better.


8. Can we use "this" pointer inside a constructor or destructor ?

You can, but it is redundant. The compiler automatically makes all function calls and data member access using "this" unless you have used explicit scope resolution.


9. In an array of integers, how do you determine the longest continuous sequence with the greatest sum ? (Hint : Array could have negative integers as well)

I am not going to answer this since it seems to be a popular interview question and the answer is readily available if you know how to use a search engine.


10. Which operators cannot be overloaded in C++ ?

Direct member access operator .
De-reference pointer to class member operator .*
Scope resolution operator ::
Conditional operator ?:
Sizeof operator sizeof

Similarly, any of the new casting operators: static_cast<>, dynamic_cast<>, reinterpret_cast<> and const_cast<>, as well as the # and ## preprocessor tokens, may not be overloaded.

I would expect a lot of people to know which operators can't be overloaded but very few know why. The reason is this, most of these operators take a name instead of a value as an operand and the language will not allow names to be manipulated.

For eg,

A obj;
obj.myFunc();


obj.myFunc() calls myFunc() method on the obj object. The compiler usually converts the function call into something like this A::myFunc(obj&). You will notice that A:: refers to a name and not a type and there is no way this can be overloaded.

# cannot be overloaded since it is a preprocessor directive and the preprocessor is not aware of overloads.

Closing thoughts on operator overloading : Eckel calls operator overloading as syntactic sugar, a way to keep using the operators we are familiar with to perform their "usual tasks" on user defined types as well. I would personally avoid operator overloading and implement explicit functions that will perform the task in question because overloading an operator merely makes code look familiar but hides certain internal details which could cause difficult bugs.

eg.

A obj1, obj2, obj3;
obj3 = obj1 + obj2;


In order to make this work, I would need to overload the '+' operator to sum two A objects and '=' operator since copy construction is involved here (in case a bitwise copy will not suffice which is what a compiler generated copy constructor will provide)

Though the snippet above is neat, it hides the fact that instead of '+', you are calling a function that adds the two objects for you (yes, operator overloading is implemented as a function call)

I would rather do this.

obj3 = obj1.add(obj2);

where add() is defined as below

A A::add(obj&)
{
// do your object addition here, usually adding corresponding member data (though not always)
// return an A object
}

I am hoping to expand this article with more questions (and answers). You will notice that more than a few of these questions demand a good understanding of OOP concepts and their application in a language like C++. I usually find that understanding data structures solves half the problem and knowing the best algorithm to apply for the problem at hand is also useful.

Sunday, February 03, 2008

Microhoo

"Microhoo", that's what a Microsoft-Yahoo conglomerate might become. The web has been going crazy over Microsoft's bid for Yahoo. A lot of people believe it doesn't augur well for the world but the same people also believe it's a good thing for the Redmond giant. Here are two giants in their own respect, trying to see if they can get together to stop (or at least slow down) the Google juggernaut.

I think this is a very bad move by Microsoft. They have traditionally done poorly on the web[1] and have managed to keep their web offerings online by virtue of all that extra money from their desktop cash cows - Windows and Office. But times are changing and the world is moving away from a desktop-centric model. I, for one don't believe the web is set to replace the desktop anytime now. There are a bunch of things I would prefer doing offline no matter how compelling the online version is. Even though I would love my computing to happen on the cloud, the same cannot be said about my storage. I would still prefer a local (and private) storage area where the majority of my files reside.

Given that Microsoft don't get the web, it might seem logical for them to go buy a web giant and hope things would turn around. But that's exactly where the problem lies; "Microsoft don't get the Web". I would consider it an almost impossible task to do well on something I don't understand very well in the first place. Unless Microsoft understand what the web is all about, I doubt they can do well, no matter who they manage to buy. Yahoo will be a useful addition to Microsoft's arsenal but I doubt if a $44 billion purchase is warranted for what would merely be a useful addition.

Will Google sit up and take notice ? Sure. Will Microsoft's profits improve ? I guess so. Will it be game changing ? Nope, Sorry.

The areas that Microsoft would benefit almost immediately if they manage to acquire Yahoo would include online photo sharing, instant messaging, email and to a certain extent, groups. And photos is perhaps the only area that Microsoft could better Google outright if the Yahoo deal materializes. Yahoo Mail is certainly better than Hotmail and could supplant or improve Hotmail but it might not make a huge difference to the bottom-line other than a staggering increase in the number of users. However, GMail simply blows all competition away and I don't expect Yahoo to come anywhere close. Yahoo did try with their new Ajax email interface [2] but failed rather miserably.

Yahoo search is not so much better than Microsoft's and I would expect the latter to retain their Live Search as the primary search offering. Yahoo and Microsoft already allow cross pollination on their IM platforms and they might be able to give Google a run for its money in this segment (given that Google hasn't done much to improve their IM platform lately).

The name of the game is online advertising and that's what Microsoft is vying for. They predict a $80 Billion market by 2010 and want a sizable chunk of that pie. Yahoo's Panama has done rather poorly and Microsoft's online advertising offerings are not much better either. Probably, they are hoping that the aggregation of all good things from Panama, aQuantive and their own offerings will create an ad platform that could pose a serious challenge to Google's AdSense. I am not too sure.

There are a lot of areas where Microsoft and Yahoo offerings will clash and it's going to be one hell of a mess as some of these services might either be collapsed to form a monolith or dropped altogether. The net gain for Microsoft might be more users and a small increase in their net revenue (not counting the cost of the acquisition). This is where things start bothering me.

Microsoft has become this giant that cannot do anything cool on its own, it needs to buy someone to make it happen. Sadly, Yahoo is more or less on the same boat and haven't done anything path breaking for a while now. I fail to understand how such a conglomerate can pull off anything spectacular (and they better, because $44 B is not small money even if it's Microsoft in question). There are a whole class of problems that Microhoo will have to encounter and it looks unsurmountable at this time.

Microsoft and Yahoo are two entirely different cultures and Seattle can never level up to Sunnyvale when it comes to employee profiles. A cultural clash is bound to happen at Microhoo and it could turn nasty for quite a few people. Attracting and retaining young talent is another worrisome area. Yahoo can and does attract college students (who invariably are the ones who pull off a coup when it comes to web offerings) whereas Microsoft is believed by many to be a mature company (a nice way of saying it's more suited for older people). Not that Microsoft doesn't attract brilliant CS grads but they are few and far between. Most of the brilliant kids go off to start their own company or go to Mountain View.

Since Microsoft will end up being the dominant partner in Microhoo, they could enforce the "Microsoft Way" in day to day operations. This means a whole bunch of nice people in Yahoo who hate Microsoft might leave. Yahoo is built on a foundation of Open Source Software (BSD, PHP, MySQL and the works) and this is something not in agreement with Microsoft's anti OSS stand [3]. Microsoft said in its press statement that Microhoo could save upto $1 Billion in operation costs. I sincerely hope the guys at Redmond have a more solid reason to acquire Yahoo (it could take them 44 years to break even).

If Microsoft does decide to act a responsible parent to Yahoo and lets the latter run its show and ensures a steady flow of money, the collective intelligence of both the companies could come up with offerings that give Google some serious heat. But I seriously doubt if this would happen. Microsoft don't have a single profitable division outside Windows and Office. (I know about XBox, shut up about XBox, it's seriously subsidized by the surplus monies from other divisions). Perhaps, a serious misstep by Google if it coincides with a coup by Microhoo could tilt the balances which I seriously doubt will ever occur. Google is not invincible, they are just very very difficult to beat. And if someone does manage to beat Google, I can bet it's going to be someone new or unexpected, not the usual suspects Microsoft or Yahoo, simply because if these companies could beat Google, they already would have.


Disclaimer : The writer is not affiliated to Google, Microsoft or Yahoo at the time of this writing.

Notes :

1. I know about Live and also about how much it sucks and don't even get me talking about MSN.

2. GMail uses Ajax in a non obvious and intuitive way. Yahoo on the other hand really screwed it up badly, and used Ajax because they could. Their new web mail outlook clone simply sucks and hastened my complete migration to GMail.

3. I can't imagine Flickr on Windows servers or Yahoo Mail written in ASP.NET, I just can't no matter how hard I try.

Sunday, October 28, 2007

How to start a startup from nothing

Enough is enough.

I refuse to work to make Larry Ellison or Ivan Seidenberg any richer, I refuse to do mundane stuff day in and day out, I refuse to come down to a place called office and spend the requisite 8 hours every day just because I have to, I refuse to work on things that don't interest me, I refuse to be guided by PHB's (Point Haired Bosses) anymore, I refuse to attend meetings that I don't have to, I refuse to convince my superiors about the need for a good development infrastructure to turn out good work.

I want to start a tech startup of my own, I have some ideas and know a couple of friends who think alike who would chip in. I am broke (well, nearly) and that's perhaps the only reason that keeps me stuck to my full-time day job. Also, am in India (Bangalore/Chennai to be specific) which is not exactly a startup hub even though a lot of startups are trying to make it big in India these days. Still, it's not Bay Area with an air that is friendly to startups and a bunch of like minded people.

I am clueless on where to start. I do have some ideas but I tend to believe the execution counts much more than ideas ever will, especially so in case of a startup. What will readers suggest ?

Should I approach a VC with a prototype and a business plan or should I apply to Y Combinator and keep my fingers crossed. Do VC's in the Bay Area (and US in general) fund ventures outside the continent ? I know Y Combinator wants their startups to relocate to the valley but for someone sitting in India, this is a proper pain because the way US Immigration laws are structured makes the whole process too lengthy and cumbersome.

There is no quick legal way for someone hoping to start a company in the US to do all the paperwork, get the necessary approvals/stampings and get rolling in any reasonable amount of time. Startup progress is measured in weeks and delays of more than a couple of months doing legal/immigration hula hoops could ring the death knell for a startup even before they get started. I am wondering if the Immigration laws are designed in a way such that a non American will have considerable difficulty starting a tech startup in the US (in order to provide a competitive advantage to companies started by Americans).

I hope am wrong, I hope I can still figure out a way to go and start a company, I hope I can be my own boss and work on things that I am passionate about.

Wednesday, August 29, 2007

Writing an autobio

I am trying my hand at serious writing and nothing can get more serious than my life. I am going to bare it all open, write about every aspect of my life, nothing hidden, no name smudging. If I were a politician or a saucy screen diva, I am sure this autobiography would turn out controversial.

"Easier said than done" said one of my femme friends. She was not questioning my ability to write, she was rather questioning my objectivity and my promise to bare it all. My answer to her - It's ME am writing about, lady ! So expect it to be heavily biased in my favor. I might project myself as the next Lord Rama (a hindu god who was known for his righteousness). Nevertheless, I am not a columnist to be objective and stick to the facts. But I will not spin yarn, I would stick to the truth but take artistic (ahem!) liberties here and there.

A word of comfort for readers, I won't make a book out of this. It will be out in my blog probably in a couple of months (might take longer depending on myriad factors like my mood, workload and the phase of the moon).


My favorite SMS

I have received innumerable SMSes on my cell phone over the years, most of them forwards. Some of them are truly funny but a lot of them are just cruft. I was clearing up my message inbox and chanced upon an old message from my cousin (Rahul) almost 2 years ago. I didn't delete it then because I loved it, it was both funny and thought provoking (serious!). Here's the text of that SMS


If money doesn't grow on trees, then why do banks have branches ?
Why does a round pizza come in a square box ?
Why doesn't glue stick to its bottle ?
Why do you still call it building when its already built ?
If you aren't supposed to drink and drive, why do bars have parking lots ?
If it's true that we are here to help others, what are others here for ?
We are a funny bunch of people living in a seriously funny world.


Thursday, July 19, 2007

Why Men are happy

What do you expect from such simple creatures?

  • Your last name stays put.
  • The garage is all yours.
  • Wedding plans take care of themselves.
  • Chocolate is just another snack.
  • You can never be pregnant.
  • You can wear a white T-shirt to a water theme park.
  • You can wear NO shirt to a water theme park.
  • Car mechanics tell you the truth.
  • The world is your urinal.
  • You don't have to stop and think of which way to turn a nut on a bolt.
  • Wrinkles add character.
  • People never stare at your chest when you're talking to them.
  • The occasional well-rendered belch is practically expected.
  • New shoes don't cut, blister, or mangle your feet.
  • One mood all the time.
  • Phone conversations are over in 30 seconds flat.
  • You know stuff about tanks.
  • A five-day vacation requires only one suitcase.
  • You can open all your own jars.
  • You get extra credit for the slightest act of thoughtfulness.
  • If someone forgets to invite you, he or she can still be your friend.
  • Three pairs of shoes are more than enough.
  • You almost never have strap problems in public.
  • You are unable to see wrinkles in your clothes.
  • Everything on your face stays its original color.
  • The same hairstyle lasts for years, maybe decades.
  • You only have to shave your face and neck.
  • You can play with toys all your life.
  • Your belly usually hides your big hips.
  • One wallet and one pair of shoes one color for all seasons.
  • You can wear shorts no matter how your legs look.
  • You can "do" your nails with a pocket knife.
  • You have freedom of choice concerning growing a moustache.

No wonder men are happier!!

Wednesday, July 18, 2007

11 Things Women don't know about Men

1. Getting angry at us for not reading your mind is like getting angry at yourself for not being able to fly. It's not just futile, it's physically impossible.

2. Yes, we do think Paris Hilton is hot. Sometimes we're even dumb enough to admit it.

3. Don't ask us to understand your shoe fetish. Asking us to respect it is even sort of pushing it.

4. You do look good without makeup, just not as good as you look with it.

5. Ever notice how we don't fight with our male friends? That's why we get so frustrated when we fight with you.

6. You care what you're wearing infinitely more than we do. In fact, if you're naked when you open the front door, you won't hear an argument from us.

7. You don't like to get hit on in public, you don't want to date online and you don't want to be set up on blind dates. Tell us if sending messenger pigeons is an appropriate way of courting. Because if it is, we're all over it.

8. There should a statute of limitations on stupid things that we said that can come back to haunt us. We propose and recommend 24 hours.

9. Cooking dinner for a man is like buying flowers for a woman, except it takes a lot more time, effort and thought for you to do it. Thanks. We appreciate it.

10. We actually like your girly pet-names for us (but please, not in front of the guys! )

11. Just because we like looking at the women in Maxim doesn't mean we want to actually converse with the women in Maxim. Not for long, anyway.


Friday, June 15, 2007

Success and Happiness

Success is relative, they say. Though success does mean different things to different people, I would tend to define success in terms of the impact or positive change it produces in one's life. If I say am a successful businessman, it means my business has added a positive edge to my life which was absent otherwise.

Success is a much sought after and elusive goal. But the very fact that everyone is after success in some form underlines its importance in one's life. I keep wondering why success is so important. What is it that man seeks by achieving success and by reaching the peaks of glory ? Is it a primal desire to stay on top or simply a manifestation of his inner needs that propel him to reach out for success at every step and turn. What does man seek to guarantee by attaining success in his endeavors ?

The most logical explanation I can think of is, man uses success as a vehicle to achieve happiness. Success either improves one's financial standing, heightens one's powers or simply kicks one's ego in all the right places (in terms of boast value). All these rewards of success (and anything more you can think of) are merely ways to achieve one ultimate goal-happiness.

I cannot imagine any success which does not make the successful person happy, for if it does, it is deemed a failure. So if all men are engaged in a mad rush to succeed, is that driven by a lack of happiness or an unending desire for more.

Happiness to me is independent of success. It is a feeling. It is created and manifested within the mind, unlike success which usually has a physical manifestation. It is possible to measure success using physical parameters, we always do (Fortune 500). On the other hand it is difficult to measure the level of happiness. Most importantly, happiness is not a direct result of success. It is at best an indirect (albeit predictable) response that the mind triggers when it perceives success.

What if I am successful and yet have the will power to deny using the success as a catalyst for triggering a wave of happiness (though I can't imagine why anyone would like to do this). On the other hand, what if I failed and yet have the will power to not let the failure affect my otherwise peaceful or happy state of mind (highly desirable, isn't it).

Strong willed men don't let their failure affect their state of happiness. Happiness is more a state of mind rather than some physical factor and hence is entirely in the control of the wielder. Ascetics use a technique called "detached attachment". This simply means they don't let the results of their actions affect their state of mind.

Actions of the body and state of mind are two independent entities and are separately controllable. If I choose to be happy no matter what happens, I can manage to be only if I have the will power. I am not referring to the classical optimist-pessimist theory here. An optimist sees the glass half full, the pessimist sees it half empty, whereas to a yogi it simply doesn't matter if the glass is half full or half empty, because both descriptions refer to the same thing.

To a yogi success does not induce happiness or sadness, it simply doesn't matter. So if it is possible for us to reach a state of mind where success does not induce happiness and failure does not cause gloom, why do we need to strive to succeed at all ? It takes a lot of hard work and discipline to reach such a state of mind but so does reaching the peaks of success. Isn't a mad rush for success the single most important factor for bringing out the worst in man ? If mental discipline can make success or rather the need for it redundant, would we have a better world ?

Thursday, May 03, 2007

How Digg should have handled the HD-DVD fiasco

Kevin Rose and Digg.com faced a particular brand of problem I would call "mob democracy". Digg.com is a social content website that allows users to submit news stories and allows other users to vote on it (referred to as digging the story). One particular user was kind enough to submit a story on a HD-DVD decryption key that could be used to decrypt the AACS (Advanced Access Content System) encryption scheme that implements copy protection on digital media like DVD's featuring HD content.I am not going to try and explain how to use this key not because of DMCA (I live in India and DMCA can't do a thing to me) but because this information is available aplenty on the internet and would be redundant here.

It so happened that the post was "scrubbed" by the Digg admins because of legal issues. Other users noticed and responded in a way true democracies do when suppressed, they "rebelled". The rationale given by Digg.com for removing posts related to the HD-DVD key was :

"We’ve been notified by the owners of this intellectual property that they believe the posting of the encryption key infringes their intellectual property rights. In order to respect these rights and to comply with the law, we have removed postings of the key that have been brought to our attention" - Jay Adelson, Digg CEO


The rationale behind this is clear, Digg wanted to stay away from any (potential) lawsuit. We are not sure if they were served a Cease & Desist or received an official notice hand delivered (the only legal way of delivering a notice) to remove the content that infringes copyright laws of the intellectual property holders. Removing content from a website that if left alone, could potentially lead to a long and financially burdening legal hassle is common sense. I don't think I have too many objections to that.

What irked me most was the manner in which this cleanup was carried out. Digg boasts of being a democracy that allows its users to have a say in what goes into its pages. If the Digg admins found stuff that could cause trouble, the best approach was let users know that they are taking this story offline "explicitly", blare on the front page about the story, give users time to assimilate the information and then take the story down.

Instead, Digg admins resorted to what dictators do to censor flow of information. They went ahead and deleted posts that referred to the HD-DVD key ad-hoc and didn't stop there. When they found that users responded with more stories on the same topic, they started banning users and deleting user accounts. This only infuriated the crowd and spread a mood of rebellion that led to an uncontrollable flow of posts on the same topic, each one of them receiving 100's of Diggs. I don't remember seeing stories with more than a thousand diggs that often, but yesterday, I was able to see "all" stories on the front page on the same topic, each with many thousand diggs to their credit.

True breed democracies don't take any form of suppression lying down. Internet is by far the biggest and one of the most successful democracies out there. When regular digg users found that their favorite site didn't have the nerve to stand up and take a stance and instead resorted to underhand tactics and silent deletion of posts, it didn't go down with them well. I, for one, would not recommend that Digg take a stand to oppose DMCA or do anything illegal. The question is not whether DMCA is correct or not, it is law in the United States and all companies and individuals are expected to comply. If you hate DMCA and its restrictive capabilities, the place to express that is with your senator or start a protest and garner strength through the power of the internet and social wildfires like Digg, /. and Reddit.

All I would have expected from Kevin and Jay and the Digg folks is this, "take a stance, let users know what your stance is, stick to it". If Digg had taken a stance upfront that they were not going to allow content on their website that could bring a lawsuit to their doorstep, so be it. All they had to do was broadcast this information to all its users, delete the offending posts, make a request to users to desist from posting material on this and last but not the least, make it known how genuinely sorry they are to censor information. Digg failed to do any of this, I would not know the reason why they resorted to underhand tactics instead of a transparent approach. But on this day, Digg failed in its biggest claim of being a purely democratic system for the submission and sharing of information.

I know Kevin Rose posted to the Digg blog later that they would let the HD-DVD posts stay and would face the music. Needless to say, Kevin got it all wrong. People did not revolt because information they were looking for was suppressed. There are dozens of ways for anyone with half a brain to obtain this HD-DVD key using Google and other social content websites. The infuriation from users was not because of just suppression of freedom of expression on a people driven website but because of the manner in which it was executed. Slashdot, one of the oldest social content websites has faced similar issues in the past and their approach has always been transparent. The issue was not about compliance nor about content censoring, it never was. To me, the core of the issue lies in the way the whole thing was handled. Boasting of democracy and deleting posts in the background is double standards and any website that adopts double standards will lose credibility and hence its user base before it can say "cheese".

P.S. It was rumored that Diggnation had accepted sponsorships from the HD-DVD consortium in the past and Digg had reasons other than fear of a lawsuit to remove posts related to the decryption key. If this is credible information, shame on you Digg, shame on you Kevin and stop advocating democracy and a bunch of choice words when you don't care a damn about that yourself. "Walk the talk" if you want people to take you seriously.

Friday, April 27, 2007

A lonely road - Boulevard of broken dreams

There are lyrics out there that reflect one's mind set from time to time. Perhaps "Boulevard ..." is the closest to mine.

I walk a lonely road
The only one that I have ever known
Don't know where it goes
But it's home to me and I walk alone

I walk this empty street
On the Boulevard of Broken Dreams
Where the city sleeps
And I'm the only one and I walk alone

My shadow's the only one that walks beside me
My shallow heart's the only thing that's beating
Sometimes I wish someone out there will find me
Till then I walk alone

I'm walking down the line
That divides me somewhere in my mind
On the border line
Of the edge and where I walk alone

Read between the lines
What's fucked up and every thing's alright
Check my vital signs
To know I'm still alive and I walk alone

Wake me up when September ends ...


Summer has come and passed
The innocent can never last

Like my fathers come to pass
Seven years has gone so fast

Here comes the rain again
Falling from the stars

Drenched in my pain again
Becoming who we are

As my memory rests
But never forgets what I lost

Ring out the bells again
Like we did when spring began

Wake me up when September ends
For I will be gone by then
And no one can wake me up !

Pandora - delightful music discovery


Online radio has burst on to the scene in such a big way over the last couple of years with the advent of high speed broadband connection all over the world. Broadband internet was an exclusive nicety that was available only to North Americans and Europeans for over a decade. With polarities shifting and non western economies becoming stronger, countries like China, India and Middle East are jumping on the bandwagon and making broadband available to the masses.

The immediate impact of this is the widespread use of Web 2.0 services in the third world. Online radio is not exactly Web 2.0, it precedes that era but the last couple of years have seen the advent of some intelligent online radio services; Pandora and Last.FM to name a few.

I especially love Pandora and listen to it at work almost every day. Pandora is written in Open Laszo , an open source AJAX technology framework for building rich internet applications. It is conceptually like Flash and even looks a bit like Flash, but is based on AJAX. It is open source and there are quite a few OpenLaszo implementations out there already.

Pandora is an intelligent, intuitive and delightful music discovery service (funky name for online radio??). It is based on the Music Genome project which attempts to analyze music and its facets like tone, melody, harmony, instrumentation, arrangement, lyric and richness of singing and vocal harmony. The interesting thing about Music Genome project is that they don't classify artists or albums as belonging to a genre; instead they analyze each and every song individually and classify it into a genre based on various parameters.

What this means is a band like Metallica which makes heavy metal music almost always won't get classified into hard metal category ad-hoc in Pandora. Instead each song of theirs is analyzed by human(s) and classified based on various parameters.

Imagine you love soft music and chose that genre in an online radio service. Even if Metallica did a soft lilting number, you will never get to hear it because most services would have classified Metallica as a heavy metal artist; but not Pandora !

Pandora allows you to create upto 100 customized stations based on various factors like genre, artist, album or even a song. If you love songs that have a heady bass guitar like Deep Purple's "Smoke on the Water", you can put in the song name in Pandora and allow it to play you song after that song that matches the tonal quality and is largely similar to "Smoke ...".

I know Last.FM is also a hugely popular service that is based on user recommendations and some heavy duty algorithms but I didn't find the service as good as Pandora (tats my 0.02). Pandora also allows you to flag a song with a "Thumbs up" or "Thumbs down" sign that helps it learn from the user experience and helps them fine tune their classification system.

All in all, Pandora is a wonderful online radio service that helps turn mundane days into not so bad ones, keeps me awake at work especially during the aftermath of a heavy lunch and keeps me going by playing song after song uninterrupted and allows me to discover exciting new music every day.

Tuesday, March 20, 2007

Stay Hungry, Stay Foolish

The full video of Steve Jobs' now famous commencement address to students at Stanford.



This has been a truly inspirational speech for me, sort of a go-getter message that I refer to whenever the chips are down. Some ideas expressed by Steve in this speech are simply outstanding and definitely practical albeit in a raw form. The crux of this awe inspiring speech

  • Listen to your heart
  • Chase your dreams
  • Don't live someone else's life (because your time is running out)
  • Define your own success
  • Stay hungry for more
  • Stay foolish to fallacy and a heavy head

I wish I had the guts that Steve Jobs had, to just let go and follow my dreams. I hope I can chuck off run-of-the-mill jobs and go join a startup with a fresh perspective, work my @$$ off, get noticed and satisfy my karma (duty). I hope that day is not far off.

Myopic eyes (Microupdate 1)

Myopia, or nearsightedness also known as short sightedness, is a refractive defect of the eye in which collimated light produces image focus in front of the retina when accommodation is relaxed. Those with myopia typically can see nearby objects clearly but distant objects appear blurred.

Savvy ? I have had myopia since the age of 9 and have been wearing corrective lenses (eye glasses). As it stands, my degree of myopia is -6.5 (when you get to -16 or lower values, you are closer to stone blind without corrective lenses). Because I depend so much on my glasses to lead a normal life, I generally take good care of them. It so happened that out of sheer bad luck, they slipped off my nose and one of the lenses shattered into innumerable pieces. And there I was, gaping with no glasses and no spare set lying around. I could see things only half as clear as your average Joe.

I rushed to the nearest optician to get my glasses fixed and he promised to get back to me in 2 days since the day after was a national holiday. The immediate implication of this being no computers or TV or reading or travel for the next 2 days. It was hell on earth, believe me guys. I was able to put up with these 2 days largely due to my iPod (thanks to a friend who gifted it) and another patient friend who gave me company. I did attempt to watch TV with little success. I tried squinting my eyes and using my laptop, with slightly improved success but I couldn't hold myself too long.

I even had to take a day off from work and I was promising myself that I would have at least one spare set of glasses made so that such exigencies are appropriately handled. I finally got my glasses repaired today (oh ! what a relief) and decided to blog about it. I am considering corrective surgery sometime in the near future so that I don't have to worry about the EVIL effects of myopia for times to come.

P.S. I call this Microupdate 1 because this is the first of my posts which doesn't resemble a post (duh !), doesn't have a fully developed theme, more like a "quick write and publish" variety. I hope to be posting more such micro updates as and when something bloggable (?!) happens.

Monday, March 19, 2007

Dilbertization of IT !

Information Technology (IT) over the years has been a sought after industry where creative juices kept flowing. Every bright high school student wanted to get into IT because it paid well, allowed innovation and creativity and made workplace fun. IT began in garages, small units that targeted a specific area and allowed its employees to turn in new and innovative inventions and work on interesting projects.

Talk to someone who has worked in IT for decades and more often than not, they'll regale you with stories of the "good old days," when the workplace was lively and creative juices flowed.

Nowadays it's a different story, they usually say, and place their blame along any number of lines: outsourcing, off-shoring, cost-cutting, IT commoditization, reactivity where there was once pro-activity, not to mention the shoddy desks in their office in dusty room at the end of the hall with pre 1990 IBM desktops that stay there because the company has a contract with the supplier.

In the simplest terms: too many IT workplaces have become Dilbertized - micromanaged, bureaucratic and stifled creatively. It's become an environment where busy work is praised and morale is low.

IT isn't fun anymore, and while a lack of fun at work may not seem worth stopping the presses over, the long-term effects of depriving a field of appealing work may very likely look like this: Students are turning away from computer science at an alarming rate. There's a huge talent shortage across the entire field, and, in confidence, enterprise IT workers say they'd probably choose a different career path if they could go back and start over again.

Yet not as many people are speaking about how to fight this, to adapt to the market and bring value back to IT that simply can't be sent elsewhere when cost-saving impulses set in.

To blame outsourcing, off-shoring and the dot com bust is to miss the point. If you know the value chain is changing, and you continue to teach people to do things that are quickly becoming commoditized, you are doing them a disservice. It's better to figure out where the value is being added for employers, and focus on this.

When IT stops interacting with the rest of the company, stops exerting influence by no longer offering innovative technology solutions for business problems, it packages itself in an easily shipped box.

"Outsourcing is a symptom, not the problem. Outsourcing has become such an important factor because when you turn IT into a commodity, it becomes about where you can get it at the lowest cost. It's what we've done to IT that is the problem, which is taking away its chance to influence business."


Let IT be more than firefighters

IT professionals that have worked in the field for a long time often speak about a shift in their work where they have gone from tossing ideas back and forth to make for better technology solutions to fighting fires all day.

There's less emphasis on creativity, and more on maintenance. Tweak this, work on this… In being reactive not proactive, everything is a crisis. Something has to be done right now, putting out fire after fire, going a long way to making IT a less pleasant environment.

Beyond making for an unpleasant work environment for the techies already in-house, this firefighting serves as a warning to potential recruits: "You will not like this job."

The best minds are not making it into the field. To some CIOs, it is a concern; to others it is not. They're losing out on the bright young people coming into the pipeline because people have an impression that if they work in IT, they'll just be fixing passwords all day.

IT needs to get back to showing people how work can be made better through technology, and how technology can be more effective, IT professionals said.

"IT is behind the wall, and business is outside the wall, and trying to exchange ideas across the wall is nearly impossible. We've stopped asking what computers can do for us."


Better managers get better results

Although establishing guidelines and defining expectations is straight out of Management 101, many enterprise IT professionals don't know what is expected of them. Often, they only find out after they've missed a deadline or made a mistake.

People perform best when they have a clear understanding of what is expected of them and how their performance will be measured. Many high performance organizations provide their team members with written expectations and accountabilities. It takes time to write and discuss expectations and accountabilities, but it is time well spent.

People want to work some place they can be creative, have opinions, where they can make their ideas work and where the rules aren't so rigid and they won't be nailed to the wall every time there is a mistake. People will perform better if they're given some flexibility, and the opportunity to make an impact.

An essential part of making people feel better about their work is to give continuous feedback, not just waiting until a scheduled salary or performance review. You have to make people feel good about what they're doing. You don't feel good about going to the dark room at the end of the hall, chained to the help desk phone until the time you go home.

This is not about becoming more civilized; it's about how the IT career is shifting from a creative, motivating path to a bureaucratic one. Of course there is room for creativity, new ideas and concepts are popping up in a daily basis and they are making things better and easier for everyone.

Podcasts,
AJAX, BitTorrent, none of that existed some years ago and is a part of life of millions today. The problem is that most companies nowadays see programmers as commodities, creativity as risk, planning and careful deployment of systems as expenses.

They have managers that don't know anything about technology, deadlines impossible to meet, no recognition for merit and talent. The consequence is that systems crash all the time, "workarounds" are the rule and the good professionals are overloaded with work to make up for all that people that work with them that don't have a clue.

With such perspective ahead, it will be no wonder if in a near future the best brains will go to finance, law or any other profession that may offer what IT used to do.

The problem is that IT has been taken over by
Business School products. They have no grasp of science, no feel for aesthetics, they only have feel for next quarter numbers and covering their @$$. This is what Business School teaches. One needn't know anything about an industry in order to manage it, Business Schools build this into their Product. They will never ever learn a new skill unless it is something useful for climbing the corporate ladder. The best thing IT can learn is to weed Business School product out. Dilbert's boss is hiding in every last one of them.


What can be done to make IT a better proposition

IT needs to be separated into distinct engineering and operations groups.

IT engineering is what the OP obviously favors. Designing new technologies, building better solutions to existing problems, and increasing productivity through these incredible meta-tools we call computers. IT Operations is about taking these technologies, cataloging their shortcomings, and doing what is necessary to implement them and keep them implemented. Engineering is about the introduction of new ideas; Operations is about the constant war to keep those ideas safe from entropy.

Often, these goals are in direct conflict. It is only natural for a solution developer to recognize the shortcomings in their product and want to fix it. It is in the best interests of operations that a stable server not be changed unless absolutely necessary, and then only when the changes have been thoroughly tested, put through miles of red tape and human business process, and signed off on by people whose jobs are on the line if the application goes down.

The idea that you can write a program and be the person who runs it most effectively is a false one in any mission critical application. When there's money on the line, red tape and paperwork is the only way to make sure that it keeps flowing.

So to be successful in IT, we on the one hand need developers who are free to try radical new ideas in an environment that rewards creative solutions to entrenched problems, and on the other hand we need a static environment ruled by business process and red tape, which stifles unproven concepts and chokes creativity. The only solution to this is to separate these groups completely, and have development treat operations as a very stodgy customer.

Too many companies don't realize that this split is necessary to maintain their financial longevity, and have the same people who develop their applications responsible for their day-to-day operations. This situation not only leads to frustrated development staff who feel creatively stifled, it is also in the long term project suicide. In-house developers should not only be relieved of the responsibility for running their code, they should in fact not even have logins to the servers on which their code is running.

Professional standards of code release need apply, too. It's not enough to release code to production via CVS checkout; you need to write an installer with an uninstaller and an upgrade path, just like you would for commercial software. It's not enough to run an ant build on your server via an NFS mount back to the depository, you need to compile a .war or .ear file just like you would for any other customer. As a developer, operations should be your only customer, and your relationship with them should be the same as the relationship you would maintain with your most valuable and critical customer.

But one person wearing the development and operations hat? That leads to nothing but frustration, burnout, entropy, and failure.


Cynical view of IT

However, there is a cynical view of how IT works. Some people are convinced that IT is beyond repair and is becoming commoditized like Manufacturing, where employees are treated as “resources”, head count for a project is the only criteria for setting deadlines and firefighting is the only recognizable form of good work. Bad applications that require firefighting often are created, the people responsible for the bad design that caused the fire fight it out (coz they screwed it in the first place) and end up being appreciated/rewarded because they were swift in fighting the fire.

Dilbertization is INEVITABLE in any hierarchical organization. There's nothing whatsoever you can do about it.

Its causes are ultimately all within human nature. Starting with the technologists themselves, they're all in competition with one another. Each wants to be recognized as the alpha geek. Furthermore, some are lazy and some are energetic. The lazy ones hate the energetic ones because they make them look bad. The energetic ones hate the lazy ones because they're not carrying their weight. Finally, the TALENTED technologists are repulsed by the thought of being promoted into management, but the inept ones love the idea, as do the closet fascists.

The professional managers, middle-managers, "project managers" (ha!) and other undead minions of all standard IT organizations are just as dysfunctional. Secretaries are sullen, convinced that everyone thinks they're stupid (in some cases, this is astute on their part). Project managers, like the fawning little lap-dogs they are, tell management whatever they want to hear, often totally screwing over their staff by agreeing to ridiculous deadlines that cannot be met.

"Middle managers often know nothing whatsoever about technology and run their areas according to whatever management theory is currently in vogue". Worse, they often rate employees by how well they schmooze, not how well they code. Nepotism is rampant. Other minions, like managers selected to represent users in design meetings, often are in way over their heads and only want to cover their asses and contribute enough to meetings to LOOK as though they've got things under control.

The whole system is completely, hopelessly, irreparably FUBAR. It's a clusterfuck of legendary proportions. The only way to survive within it is to make yourself invisible and get your work done as efficiently as you can, while not getting drawn into any politics, never suggesting anything, and never volunteering for anything.

Personally, the biggest gripe I have with IT is this "Dysfunctional middle management".

My biggest headache at work is the nontechnical people who are mid level managers in the IT department. Some of them come from Finance, others from other non-technical departments in the company.

So what do they do? Instead of running a team like most normal managers they have to meddle to prove their worth and validate their existence. So they do dumb stuff like randomly reassign staff, change priorities every two months, and other PHB-style behaviors. They have no technical competency so they can't help out in the work, so they overcompensate and do dumb stuff.

We work exclusively on CRT monitors at workplace (ugh ! I know it's from the previous millennia) and I wanted to convince a middle manager who takes IT decisions to provide flat screens for all developers. He starts off like this "Hmm why do you guys need LCD's, what is wrong with CRT's". I try and give a detailed "technical" explanation of the advantages that a LCD display has, only to hear the usual FUBAR about lack of "funds". The biggest irony is this guy works exclusively on a nice 19' LCD display.

I would have hoped that these types of people would have filtered out of the IT department by natural attrition (new companies, etc), but they haven't and it bothers me endlessly. Most companies have a fairly well equipped top management consisting of people who have been in the trenches, who have coded stuff somewhere in the past. The biggest problem is, when these guys become C*O's (CEO, CIO, COO etc), they tend to have a disconnect from the lower echelons of the organization, where most of the useful work gets done.

Till top management starts understanding the problems faced by developers who sweat it out day after day and put in a sincere effort to improve the situation, things are going to be "status quo ante". A well equipped top management that is techie makes workplace so much better, stellar examples being Larry and Sergey and the way they run Google. I am sure Larry is not going to question the need for flat displays or any other tools of the trade that help developers turn out better work.

P.S. Google employees generally have dual 21' displays and two workstations to play with, budget is the least limiting factor for procuring and making available any developer tool or toy and developers are pampered with a host of on-site facilities and sops that make them stick to their workplaces longer and turn out better work because they generally love what they are doing. Any Google employee can rise to the position of a VP purely on technical accomplishments without getting into the willy nillies of managing a team. No wonder Google is a sought after destination for every IT guy worth his salt.

Tuesday, March 13, 2007

A gentle intro to JSON and its concepts

Out of all the technologies that have emerged in the Web 2.0 world of Ajax powered apps, JSON is perhaps one of the most important. Its implications might not be apparent but JSON is a powerful technology that allows data interchange between traditional applications in an open and human readable format without the quirks of XML.

JSON (JavaScript Object Notation) is a lightweight computer data interchange format. It is a text-based, human-readable format for representing objects and other data structures and is mainly used to transmit such structured data over a network connection using serialization.

JSON finds its main application in Ajax web application programming, as a simple alternative to using XML for asynchronously transmitting structured information between client and server.

JSON is a subset of the object literal notation of JavaScript and is commonly used with that language. However the basic types and data structures of most other programming languages can also be represented in JSON, and the format can therefore be used to exchange structured data between programs written in different languages.

Code for parsing and generating JSON (the latter is also known as "stringifying") is available for a whole bunch of languages including (but not limited to) C, C++, C#, Java, JavaScript, Objective-C, The 3 P's - Perl | Python | PHP, Ruby, ColdFusion, Common Lisp, E, Erlang, Limbo, Lua, ML,Ruby, Smalltalk, Tcl and ActionScript.

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.

Before we head into how JSON is useful in the Web 2.0 scenario, we have to elaborate a little bit on how Ajax works and where JSON fits in.

Ajax is a web development technology that makes the server responses faster by enabling the client-side scripts to retrieve only the required data from the server without retrieving a complete web page on each request, which will minimize the data transferred from the server. These requests usually retrieve xml formatted response, the xml responses are then parsed in the JavaScript code to render the results (which complicate the JavaScript code)

Need for JSON

Ajax allows Web-enabled applications to perform out-of-band client-server calls, establishing a separate channel on which to send and receive information from remote Web services. In layman's terms, updates and navigation sequences in Ajax applications are done outside the classical client-server context, which entails a complete screen refresh, with the information being received in the background (a.k.a out-of-band).

These application updates that are typically obtained from RESTful Web services, once received in a user's browser, need to be incorporated in the overall HTML page layout, which is exactly where XML proves to be more than a handful. Though the capabilities of most mainstream browsers have increased over the years with the support of scripting languages and plug-in support, many programming tasks still remain difficult or unnatural to perform, one of them being manipulating and processing text, which is typically done using DOM.

The complexity in using DOM lies in its function-based roots, with a simple modification or access to a data tree requiring numerous method calls. In addition, DOM is known for differing implementation details among various browsers; this process takes us to a very elaborate programming scheme with ample possibilities for a breakdown in cross-browser compatibility. So the outstanding question now becomes: How can a markup language be easily integrated into an HTML layout page to accommodate Ajax requirements?

The answer comes in the form of leveraging a common component in all mainstream browsers: The JavaScript engine. Instead of delivering Ajax updates in a format such as XML, which would require the use of a mechanism like DOM to access and incorporate data into a layout, a more natural and intuitive approach would be using a format that fits natively to the aforementioned engine, namely JSON.

Now that we have addressed the place JSON has with respect to XML and Ajax applications, let's take a closer look at the technical details behind JSON.

The first thing you should realize about JSON is that it remains a simple text format—just like XML—which is relatively easy to read and inspect with the naked eye. At a syntax level, what starts to set JSON apart from other formats is the characters used to separate data, which are mainly constrained to apostrophes ', brackets ( ), [ ], { }, colons :, and commas.

This listing illustrates what a JSON payload looks like:

{"addressbook": {"name": "Mary Lebow",
"address": {
"street": "5 Main Street"
"city": "San Diego, CA",
"zip": 91912,
},
"phoneNumbers": [
"619 332-3452",
"664 223-4667"
]
}
}

Human readable? Yes. Machine can parse easily? Yes. Similar to XML? Largely.

How does JSON manage to work?

The answer is that JSON works because most people don’t really need all that overhead, and because it’s often possible to do really interesting things with really simple formats. The World Wide Web has been churning along for over a decade with a markup language that originally had no standardized specification; these days it has specs, but they’re almost never enforced and are, in fact, usually thrown down to the ground and trampled upon. And it still works.

So HTML is a fast and loose format and it doesn’t have any concept of data types that the average programmer would recognize (though it does, in its own special way, have data types), and what rules it has with regards to what you can stick where are routinely ignored. And yet it works. It works really, really well. It works because most people who are using it don’t really need to do complex things with it. Most people who need markup languages for use on the web just want to do simple things like display some text and pictures. You don’t need a 500-page language specification to do that.

JSON is stricter than HTML in some ways; it expects you to obey the rules, but in exchange it gives you fewer rules to follow. And JSON works really, really well. It works because most people who are using it don’t really need to do complex things with it; most people who need data formats for use on the web just want to do simple things like fetch some data from over there and drop it into this web page here. You don’t need the massive overhead of XML-the-protocol-stack to do that.

There are people who genuinely have more complex needs, and I’m not going to try to say whether one thing or another will suit what they’re doing. But for the majority of us who are lounging around in the big belly of the web, JSON is just fine.

Benefits

Making use of JSON's data separators may not be too obvious at first glance, but there is a fundamental reason behind them: easier data access. As it turns out, the internal representation used by JavaScript engines for data structures like strings, arrays, and objects are precisely these same characters.

Where this leads us is to a more straightforward approach for accessing data than the alternate DOM technique, but let's take a look at a few JavaScript code snippets to illustrate this process. These snippets access the information in the previous JSON snippet:

  • Name access from JSON: addressbook.name
  • Street address access from JSON: addressbook.address.street
  • First phone number access from JSON: addressbook.address.phoneNumbers[0]

If you have done some type of DOM programming, you will probably notice the contrast immediately, but just in case you haven't, here is an external resource to the Document Object Model, which contains a small example for navigating across data.

An added benefit of JSON is its less verbose nature. In XML, the opening and closing of tags is a necessity just for markup compliance, but in JSON's case all that's required is a simple bracket for closure. In data exchanges comprising a hundred or more fields, this additional XML markup can add to transit times.

Additionally, JSON has garnered the attention of many developers specializing in different programming languages, giving way to libraries capable of producing this format from environments as diverse as Haskell and Lisp to more mainstream options like C# and PHP.

Constraints

Like many benefits, JSON's less verbose format can cut both ways, and in this sense lacks a few of XML's properties. Namespaces, which allow the mixing of identical pieces of information in different contexts, is clearly missing in JSON. Another differing feature is that of attributes, since every JSON assignment is done with colons (:); when transforming XML to JSON it can be difficult to distinguish between what would be considered text between tags—XML CDATA—and the actual value of attributes.

You may also find the actual creation and validation of JSON a little more complex than an average XML fragment. In this sense, it may be that XML has had a head start in terms of developing tools for its processing. Nevertheless, and to ease any worries you may have in this area, we will be exploring some of the most mature JSON developments in our next section.

Is JSON for You?

Like all design choices in software, your requirements will provide the answer to this question. If your Web services consumers will be created in a classical, full-fledged programming environment like Java, .NET, PHP or Ruby, then you can probably make due without JSON. Given the open-ended capabilities of most programming language environments, providing you total configuration control not to mention access to custom libraries, parsers, or helper classes, the difference between consuming JSON, XML, or any other type of Web services payload should be negligible.

If on the other hand, your Web services consumers will be confined to the environment of a browser, JSON deserves serious consideration. Consuming Web services in a browser is not done for the sheer fun of it but rather as a real business requirement. If your manager comes to you and requires one of those "slick Web 2.0 interfaces" that appear to load data without a delay/refresh, then you would be entering the technical realm of Ajax and Web services consumption in browsers.

In these last circumstances, you would not only be limited to the processing environment of a machine located across a network, but one under tight control of a random user, limiting even the most resourceful developer to work with a lowest common denominator for processing text in browsers: the DOM, which as outlined earlier, is a difficult undertaking when compared to accessing a JSON tree.

When you get started with JSON the comparisons with XML are inevitable. Hard core XML fans tend to remark that JSON is redundant and everything it achieves is already implemented in XML. JSON is a better data exchange format. XML is a better document exchange format. Use the right tool for the right job.

Though the x in Ajax stands for XML, and Web services have come to the forefront by the steady use of this same format, it doesn't necessarily mean such an approach is cast in stone. As we have seen, XML has a few drawbacks when applied to Ajax-enabled applications, due to the text-processing capabilities in browsers. In this sense, JSON has emerged to provide a compelling alternative in this same context.

References ::

The first two references contain more material on JSON. The rest of them are comparisons of JSON with XML and suitability of JSON for RPC.

http://www.json.com/
http://www.json.org/xml.html
http://blogs.msdn.com/mikechampion/archive/2006/12/21/the-json-vs-xml-debate-begins-in-earnest.aspx
http://www.tbray.org/ongoing/When/200x/2006/12/21/JSON
http://www.infoq.com/news/2006/12/json-vs-xml-debate
http://copia.ogbuji.net/blog/2006-12-23/Why_JSON_v