Thursday, December 21, 2006

Re: Select you crazy Query

Are you saying the first query is wrong cause it did not get the results you expected? Cause I don't see anything technically wrong with it. A left outer join does not mean that at the end of the query (whatever it may be), you will get all foos. What you get in the end depends on your WHERE clause.

Break the first query down and you will see how it was arrived at. The cross product of the tables based on the ON clause of the LEFT OUTER JOIN "did" contain foo(1) but the WHERE clause which was applied over the cross product eliminated that record. Hence the result.

The behavior is standard and not MySQL specific. Both those queries on a different engine should return the same behavior. Also, I don't see why the ON clause should remember the composite primary key.

More on left outer join here.

Tuesday, December 19, 2006

Select you crazy Query

Lets try a small SQL quiz this time. We have two tables foos and bars with the definition below..


CREATE TABLE bars ( foo_id INT NOT NULL, bar_id CHAR(1) NOT NULL, PRIMARY KEY (foo_id, bar_id), FOREIGN KEY (foo_id) REFERENCES foos(foo_id)) type=InnoDB;

And lets do some sample data inserts..

INSERT INTO foos VALUES (1), (2), (3), (4);

INSERT INTO bars VALUES (1, 'a'), (2, 'b'), (3, 'b'), (3, 'c');

Now write query to give me all foos and those bars who have a bar_id 'b' for the same foo_id. Seems like an easy OUTER JOIN. This is what I came up with initially..

SELECT * FROM foos LEFT OUTER JOIN bars ON foos.foo_id = bars.foo_id WHERE bar_id = 'b' OR bar_id IS NULL;

And the result set was..


3 rows in set (0.00 sec)

But the result set obtained is wrong because we did not get all the foos. Get back to the query then to obtain the result set below..


4 rows in set (0.00 sec)

For the correct query just make a minor change to the above SELECT

SELECT * FROM foos LEFT OUTER JOIN bars ON foos.foo_id = bars.foo_id AND (bar_id = 'b' OR bar_id IS NULL);

I did not have enough time to search for the actual reason for this behaviour though and am not even sure if this is standard or MySql specific.

In table bars the primary key is a composite key between foo_id and bar_id. When foo_id is compared in the ON clause, it appears as if the rest of the primary key is forgotten. foos (1) joins with something like bars (1, NULL) and so the WHERE clause fails. When the ON contains all the clauses for the whole composite key, the LEFT OUTER JOIN behaves as expected.

That gives a whole new perspective to outer joins when dealing with composite primary keys then.

Thursday, November 16, 2006

Java is now under GPL

That was really some amazing news. Yesterday was like a dream thinking of how bold Sun had been. Later I was thinking of what this really means for the language.

The obvious things are better community interaction with things like bug fixes and ports(palm anyone??).

Java SE will soon be bundled in almost all Linux operating systems. I think Gnome was using some Mono based applications. Now they could actually have the Java VM distributed. A whole new range of Java based desktop apps will be released. Its important to note that version 7 has been released now and version 6 will also be released later. That means that all free implementations of Java will use version 5+ Api's and so the fancy features like Generics, Enums and foreach etc. Java 6 also has a whole range of desktop specific enhancements so the "feel" of Java apps should also improve.

I am not sure if it is possible to deploy the Java ME VM without paying Sun any license fees. I want Sun to make money of Java, but if they can deploy Java to most phones/devices that would be great. Could this be the next platform after all.. almost making Java appear as an Operating System?

Of late Java lost a bit of steam. The language is mature and there are a large number of libraries to help in most tasks but age is also showing. Dynamic languages like php and ruby(with rails) provide features for faster webapp development. Even C# has some really cool features. This announcement will provide some extra steroids. In the future it will be interesting to see how "dynamic" Java can get.

To conclude.. Sun just made a lot of hackers really happy. This will definitely have some positive effects for the company and the language.

Tuesday, November 14, 2006

High on Emacs!

For the past three weeks I have been playing around with Emacs. Right from learning how to do simple editing to finally getting the Java Development Environment for Emacs (JDEE) setup on my machine, its been a whirlwind tour of this awesome editor!

I used to be a big "vi" fan, but the sheer amount of work that Emacs allows you to accomplish is simply superb! I'm looking at a phase of experimentation, to see if Emacs will be productive for me on a daily basis. For now, the answer seems yes!

Check out:-

GNU Emacs

Emacs Tutorial

Learning GNU Emacs


Happy editing!

Wednesday, November 08, 2006

Re: Free the Platform!!

Looks like someone else had the same itch

Yet another link to the presentation above here. Get more info on the Linux phone here and here. You can get some more information on the State of Linux phones

THERE HAVE BEEN a lot of phones claiming to be 'Linux phones' and those that do run a Linux kernel, but they all miss the point of Linux: to be open. FIC is about to change that in a big way with a truly open phone, the OpenMoko.

Some of these phones are already in the market from Motorola and others. Companies like WindRiver and MontaVista also make Linux based stacks but I am not sure how open they are and whether it is possible to change/upgrade everything.

Within a few years more and more companies will release mobile frontends to their webapps. Just as Gmail recently did. As of now Symbian, Windows CE and Palm and the main OS'es for smart phones. Who then will win the battle for the dominant platform? Java has an enviable position right now as it has already been deployed in many phones already. If Sun opensources Java under GPL then there will be even more widescale adoption in the open source stacks. Nokia Series 60 also allow programming in C++ and Python now so thats another race.

Monday, October 30, 2006

Free the Platform!!

Can anyone tell me why I cant upgrade my mobile phone software?? Not some Java application on the phone, but the actual operating system. How much of a difference is there between a Nokia series 40 and Nokia series 60 phone in terms of hardware? Should I not be able to swipe the existing JVM which was based on an older Java implementation to a newer one? Will I ever be able to customise the hardware in the phone? Maybe we'll have to wait a bit longer for programmable hardware.. probably through FPGA's.

In a few years the mobile phone will be more important the the average computer. More people will use handheld devices for their daily tasks like browsing the net and listening to music. Developers will follow.. creating the next generation of killer applications.. followed by hackers to free the platform.. and by then google will own all your data

Tuesday, October 24, 2006

Ubuntu 6.06 (Dapper Drake) on Acer AS3004WLCi

I had installed Dapper Drake on my Acer AS3004WLCi Laptop a few months back. I've been happy with it but there were several things that didn't work. I've been pretty lazy but I think I've finally resolved most, if not all, of them. Here's a run down.

Firstly, what didn't work:

  • After booting up and picking Ubuntu from the grub menu list, a few kernel messages would show up and then there would be a blank screen until X Server started up with the login box. Similar issue when shutting down... didn't see any messages.

  • Same thing for Virtual Terminals... Ctl-Alt-[F1-F6] would bring up a blank screen.

  • Resolution for the 15.4 inch screen would only bring up 640x480, 800x600 and 1024x768 options. No wide-screen 1280x800 resolution.

  • Touchpad scrolling would not work.

  • Suspend/Hibernate would work the first time after booting up, but after resuming there would be a message saying there was a problem and on subsequent attemps would fail. It would look like it's doing something, screen would go blank, then all of a sudden some garbled text would be displayed at the top of a black screen and the screen saver would start up.

  • Wireless would not work. Would not detect the card.

  • Issues with sound... If multiple applications which use sound such as a music player, a flash site and skype were open, things would be very erratic. Skype would give errors like 'Problem with sound device'. Flash based sites like YouTube or Google Video would not have sound etc...

What I did to resolve these issues:

  • Turns out that the blank 'bootup/shutdown screen' and blank virtual terminals are related. When you pick Ubuntu from the grub menu list, it loads up the linux kernel which in turn loads a basic video driver to show the kernel messages. The video driver is considered to be basic and compatible with most screens. If the vga parameter is not passed to the kernel it defaults to vga=normal. It seems it wasn't with my Acer screen. So this needed to be changed. Edit /boot/grub/menu.lst. Add vga=792 to the kernel /boot/vmlinuz-<version> root=<hda> ro quiet splash line.

    HOWTO: Change bootup resolution was really helpful. It goes into further details about the bootup process etc... Also read it to understand the 792 value.

    The kernel uses the same driver for both the 'bootup/shutdown screens' as well as the virtual terminals. The vga parameter fixed both these issues.

  • The touchpad scrolling problem and the screen resolution problem had a common resolution.

    lspci | grep VGA
    VGA compatible controller: Silicon Integrated Systems [SiS] 661/741/760/761 PCI/AGP VGA Display Adapter
    on my Acer laptop.

    So we need a sis driver. Looking at /etc/X11/xorg.conf showed that it was loading the vesa driver. To fix this run
    sudo dpkg-reconfigure xserver-xorg.

    Go through all the screens and answer the questions. For most, the default will do. When you get to the video screen, pick the sis driver and the desired resolution (1280x800). Make it the default. After this is done restart X-server (Ctl-Alt-Backspace). It should start with the crisp widescreen resolution.

    The Strange screen resolution thread got me the lead on trying dpkg-reconfigure.

    Check this page for some bedtime reading on sis drivers.

    The touchpad scroll started working after going through the reconfigure. I didn't do anything specific. I think I just picked default options for the touchpad/mouse screen.

  • The Suspend/Hibernate issue was related to Wifi not working. Funny how these things are related no? The garbled text that was displayed after a failed suspend became clear after the virtual terminal issue was resolved. It was outputting bcm43xx: Error: Microcode "bcm43xx_microcode5.fw". dmesg showed the same error.

    lspci | grep Wireless
    Network controller: Broadcom Corporation BCM4318 [AirForce One 54g] 802.11g Wireless LAN Controller (rev 02)
    on this Acer laptop.

    So this is the wi-fi driver. After some searching, came upon How to: Broadcom Wireless cards (These How-Tos are amazingly helpful!). I followed the instructions and it seemed to work. iwconfig brings up the wireless network interface information + iwlst <eth> scanning shows the wireless connections available.

    After suspending and resuming, looking at the dmesg output, the errors weren't there anymore.

    Check this page for some insight into the suspend/hibernate process.

  • The sound issues took care of themselves really :) Upgrading the kernel to 2.6.15-27-386 seemed to resolve all of them. Also upgrading to Skype helped.

Most problems have solutions out there. All it needs is a bit of searching/reading/trying.

Lets see how many of these issues are resolved in Egdy Eft which comes out tomorrow!

Saturday, October 14, 2006

Monday, October 09, 2006

hd internals

A cool demo of the hardworking hd in action.

Thursday, October 05, 2006

Ubuntu on a Dell 6400

Yipee.. I recently purchased a Dell laptop - Inspiron 6400 (also known as E1505). The first thing I had to do was to install Ubuntu. Luckily for me this is a common model and comes with some standard components and most of the things work with little or no configuration.

By default I got a resolution of 1028x768 on the widescreen which supports 1280x800 natively. So things were looking pretty stretched. The i915 wiki page explains pretty well howto get the correct resolution.

Another fun thing I did was to get the GKrellM with i8kutils. GKrellM is used to monitor the system. Its something I need to look more into. i8kutils is a dell laptop specific lib for intrumentation of the processor fans. Installation was explained in the Ubuntu forums over here. However after installing everything as explained, GKrellM still does not show any fans. For that, within the GKrellM configuration, the i8k plugin needs to be enabled first. After that two fans will be visible. Clicking on the fan can be used to toggle to Manual mode for fan and set the speed as well which you can confirm by listening to the low buzzing noise!! However I dont expect the processor to burn as the author says :) as the Intel motherboard automatically sets the fan speed based on heat.

Sunday, October 01, 2006

final by default

When talking about 'good programming practices', one thing that was recommended by a good professor of mine was to declare variables that you aren't going to modify as final. It serves two purposes:
1) A self check for yourself so you don't accidentally modify the variable down the line somewhere
2) Self documents the code: Other's reading your code know immediately of your intent.

While trying to follow this (in Java) I found all these 'final' keywords all over the place. I began to feel that it wasn't really helping. Just cluttering the codebase with 'final's.

So why not just have all variables be final by default?

Along these lines, I feel providing all the abilities of C++'s const to Java's final would be a tremendous boon. Your contracts can be better defined - not just on references but on the objects themselves.

<soapbox>Code maintainability - getting up to speed on a codebase and changing someone else's code is something that is done far more than adding new code. Anything to make readability better by clearly defining contracts/intent can only be a benefit and should be strived for.</soapbox>

Monday, September 11, 2006

Shutup PHP @ $%!

OK.. I'm getting a bit carried away here.

I saw a bit of code in PHP like ..
$contents = @file_get_contents($filename);

But what is the @ before the function call?? As bad as I am with googling I was not able to get some good docs on this. The best info I got was from meeting notes which says that its called the shut-up operator.

So then I backtracked to the PHP manual and found Error Control Operators. The doc says..
"PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored."

Yet another example of a lazy programmer trying to code before reading docs

Monday, September 04, 2006

Re: On the road

Took this on ride to San Francisco on Sunday. It's directly across the Oracle HQ. There are infact two billboards along the highway. Really nice to see Ubuntu get some (well deserved) publicity.

Saturday, August 26, 2006

Internet Explorer on Linux??

You mostly want to check for web page compatibility with IE if you're doing some web app stuff. So whats the solution if you prefer to use a linux distro rather than Windoz??

Enter ies4linux. ies4linux installs IE binaries over Wine. Wine is an Open Source implementation of the Windows API on top of X and Unix. ies4linux has a script which will download the IE setup files right from the microsoft site, and install over Wine. The best part is that you can install multiple versions of IE as well ;)

Saturday, August 05, 2006

Circular dependency between Tables in SQL

Update: Added a new way to solve the problem

Q) How had to create Tables with a circular dependency between them. Like

....A_ID INT,
....B_FK INT,

....B_ID INT,
....A_FK INT,

... now this will not work as during create, table B does not exist

A) So one solution is ...

....A_ID INT,

....B_ID INT,
....A_FK INT,


A little bit of cheating does the trick :)

A) Another mysql specific solution is to use the FOREIGN_KEY_CHECKS variable.

mysql> SOURCE dump_file_name;

In the dump_file_name, tables can be created in any order without checking of Foreign Key constraints. So we can create tables with circular depenndencies

Monday, July 24, 2006

Java Generics FAQ

Java Generics FAQs - Frequently Asked Questions - This is a collection of answers to frequently asked questions (FAQs) about Java Generics, a new language feature added to the Java programming language in version 5.0 of the Java Standard Edition (J2SE 5.0).

Sunday, July 23, 2006


I've always liked Mid-Day. I was pretty happy to find their site. But the format they have is pretty sucky. They provide a pdf for each page. So to read the entier paper, each page has to be downloaded separately. Not the most convenient thing.

I had started looking at Python a bit (going from the previous post). This seemed to be a pretty good project to use it for. The script - midday - automates the process. Given the paper you want (sunday|mumbai|vashi), it downloads each paper's pdf pages to your machine. It also uses a third party app - pdftk - to combine all the pages into one pdf. It needs to be downloaded and the location it was installed in needs to be passed into the script, but its not necessary. The script still works w/o it, but the pages are in separate pdfs. Should work on both Windows and Linux, but I've tested it only on Linux.

Overall, I liked Python quite a bit. Definitely more productive that Java... for something like this. Less upfront design needed, but is still structured. All the data structures mentioned in the last post came into play. Also, being interpreted, it's pretty easy to make it work on multiple platforms. The library is pretty extensive and with the easy ability to call other programs (especially in the linux environment where the culture is to create small programs that can be stiched together), python can be used for a wide range of applications.

Re: One Array to rule them all

Pretty late reply... I've been doing a bit of python of late. And although there isn't 'one array to rule them all' there are some similarities. In python there are three main data structures - lists, dictionaries and tuples. Lists are growable arrays (ArrayList in Java) and Dictionaries are associative arrays (HashMap in Java). Tuples are just containters to hold other objects (No equivalent in Java). They are immutable.

People always talk of productivity with dynamic languages. These built-in data structures are one of the main reasons for it. Sure Java has an amazing collections framework, but it's not a language feature. It's a framework built on top of the language. In python, these are part of the language... [] creates a list, {} creates a dictionary and () creates a tuple.

Having these be part of the language makes a huge difference. In Java, if you want to return multiple values from a method... either do something ugly like return an array with the values (the values have to be of the same type) or define a new class to hold the values and return an instance of that class. Quite a pain. In python, you just put the values (of any type) inside a tuple and return. As simple as that. And as you can imagine, lists are used a lot to hold collections of data. But probably the most used data structure is the dictionary. It's a dumping ground in all sorts of situations. You would think it would be a readability nightmare but it isn't. It's almost a convention in python and when you come across code, it is immediately recognizable. And these ds's combined make for more productivity gains. Tuples, being immutable, can be used as keys. Also, python has support for sets as well.

Saturday, July 15, 2006

Right to Information

The Right to Information Act (RTI) was passed in 2005 which empowers Indians to ask the Government about anything that concerns them. You can ask questions like why the road outside your house is not repaired or why some facility is polluting your city. The official is under an obligation to respond to your query within a stipulated period.

Now things are amazing on paper. I doubt that information comes so fast and easily. There are a few sites, organisations which assist in this.


Maybe such a wiki like community site would better serve this purpose

Thursday, July 06, 2006

[Trackback]How to concentrate on writing

Something to think about..

When I am up against a deadline and I absolutely, definitely have to get on with my work, I use a few tactics to force myself to concentrate:

1. Switch off email. I don’t start Outlook (or if I do, I disable all the notifications that tell me I have new mail).

2. Isolate myself. I use Bose noise-cancelling headphones but don’t plug them into anything. The silence really is golden.

3. Greed and guilt. I remind myself how much money I’m getting paid for a particular assignment and how ashamed I will be if I miss the deadline. This actually works sometimes.

4. Stop with the blog already. When I’m pressed for time, distractions like blogging and hoovering become very compelling. Knowing this makes it easier to resist.

5. Get up early. 6am is the most productive time of day for writing. No distractions. It also feels more virtuous than staying up late with work.

6. Little treats. I bribe myself: ‘Matthew, if you write another 500 words, you can have a cup of tea and a biscuit.’

7. Chunking. Setting a timer or alarm clock for 15, 20, 30, 50 minutes and doing nothing but writing until it goes off and then taking a break seems like a good way to make progress.

8. Go full screen. Switching Word into full screen mode (from the view menu) eliminates all distractions but the piece I’m working on.

9. Shitty first draft. Splitting the work into distinct writing and editing phases breaks the job down nicely and it takes off some of the pressure to ‘get it right first time’.

10. Change location. Sometimes, if I’m really struggling to get started, taking a laptop or my notebook to a cafe and scribbling out something there - a fresh new location - is a good way to jolt-start an assignment.


Thursday, June 29, 2006

One Array to rule them all

I've been coding in php of late. So some thoughts on that...

Most languages have a few Data Structures (ds). Java has the excellent Collections framework which contains quite a few types of ds as shown in the figure

What I found in php was the array();

It can be used like...

$pasta = array('spaghetti', 'penne', 'macaroni');

In this case the array can be compared to a List or an Array.

You can also add an element to the end of an existing array with the array_push() function or remove an element from the end by using the array_pop() function.

Using the array_shift() and the array_unshift() function removing or adding elements to the beginning of the array can be done.

So now the array can work both as a Queue and a Stack.

Additionally it is possible to declare an array like..

$menu = array('breakfast' => 'bacon and eggs', 'lunch' => 'roast beef', 'dinner' => 'lasagna');

Here 'breakfast' is a key and 'bacon and eggs' is the value. Usually keys are numbers from 0 onwards. In effect our innocent array is now a Map as well.

All this is extremely great for learners. A few thoughts ..

1. The array exposes an API for all these ds'es. How can I enforce an array to act only as a Queue? Is the leakage of the abstraction too much.

2. How do they optimize? In Java I know the costs when I create a LinkedList or an ArrayList. Or use a TreeMap or HashMap. How is it done in php?

3. Also are there Sets in php where one object cannot be added twice. Maybe I am missing some ds library in php ;)

Wednesday, June 21, 2006

Tuesday, June 20, 2006

Get Functional

This article - Functional Programming for the rest of us - is like Argentina's performance against Serbia-Montenegro... beautiful.

Wednesday, June 07, 2006

Re: Have you ever felt this?

The "there's got to be more to life than this" deamon thread gets activated often. It's still at a way low priority level as compared to that dude but dunno for how long.

Sunday, May 21, 2006

Is India at the Tipping Point?

Here's the article... It's by a Microsoft employee, but talks generally about whats good/bad about the Indian market. He covers pretty much the entire spectrum. Specifically, I thought this statement holds very true...

"It's also interesting to note that many Indians from the Valley are returning to India to set up their startup companies as well as to leverage the engineering/labor-cost advantages. While many startups we talked with have big ideas, they will need coaching to pitch ideas for funding and must be willing to accept possible failure, a normal part of the entrepreneurial culture.".

I've been following a few blogs by Indians who worked in software companies in the US and have gone back to start companies (startups) of their own. These are proper software companies, products based not services/bpo companies, targeting the Indian market. I feel this is an important trend. The backbone of the Indian IT industry is services, but it probably won't be able to sustain the growth in the years to come especially as salaries increase.

But at the same time, there are challenges. You have to think differently from if you were starting a co in the US because the market is so different. Broadband penetration is not high, payment procedures are different... credit card usage is not that common etc... But the guys who go back are smart and seem to take all this into account. Another thing is that the mobile market is booming and growing way faster than pc/broadband. So what about targeting mobile devices? That would be a huge market to target right off. I'm sure the big players like google, yahoo are already planning things, but there maybe opportunities to start some small super specific services?? What dyou think?

Friday, May 05, 2006

Bruce Eckel interviews.....

Bruce Eckel interviews.....


and others.

If you want descriptions.. go here else for direct mp3's go here

Saturday, April 29, 2006

Cool Calendar - Javascript Web Based Calendar Application

Last term for a user interface design class, I made the a web-based calendar. The calendar was about 3000 lines of Javascript. It emulates Outlook in that you can drag and drop select and so forth. The calendar however was just built for the user interface and has no backend. Therefore any changes you make are only held in the client machine's memory. When you reload they are reset. Also its only Firefox 1.0* + compatible. To see pre-populated events , go back to Dec. 2005.Try it out here. -Pawan

Monday, April 17, 2006


I have been coding in JSP for a while now and worked on the Struts framework as well. The servlet API's allow adding of instances to various levels of scope within the webapp. Like Application scope for the entire webapp, Session scope for the user session, Request scope for a particular request. Request scope is useful as the particular request response can be generated in parts by chaining servlets together. Now these methods make life pretty easy. Something which will used throughout the webapp can be kept at application scope and similarly at varying degrees of granularity at other scope. Like on correct credentials for a login form, a String can be stored in session scope.

This would always disturb me. Simply because client specific data was being stored in-memory. Upto how many users can be supported in such an architecture? even if 10 bytes are stored per client, that can count to a lot of memory for say 10k users. Then the overhead of caching, maintaining state of live data and associating it with the client.

Initially storing stuff in scope would make me think of the size of the objects I used to store. Then I realised frameworks like Struts store huge amounts of data at various scope. Like the action form etc in the request scope. So that made me feel that this is not such an issue. If such relatively heavy objects can be stored then that must be fine. And now with JSF etc these object are just getting larger.

Then I read on the Php Share-Nothing architecture. So the Share-Nothing architecture advocates... simply sharing nothing about the client on the server. As simple as that. Instead of storing stuff in memory, store details in a DB and make DB calls always. Now I am not actually sure how performant this is. But DB's have been around for a long while and have been well tuned. Plus DB's can be easily made to work in parallel redundant mode and have very good features.

Again I know I am being premature to be so anti-in-memory stuff. Many large systems have been built, especially in the enterprise. But it just does not feel right. In the end it seems easier to scale horizontally with the Share-Nothing approach. The in-memory approach seems to force towards vertical scaling.

Some examples... Flickr and Yahoo are two php based webapps. And they dont get bigger than them. Ebay is the biggest java based webapp i know of. But Google for ebay architecture and they too have a custom Share-Nothing like system in place. Gmail too uses Java, but have some super optimizations in place.

Rails is one very hot webapp framework right now. This is one of the more intelligent discussions I have read. Coupled with this blog post Rails seems like something I would like to learn soon. These guys seem to be aware of both php and Java webapp dev. Any idea on their principles on this issue?

What dyu guys think? Any of you really stick to the Share-Nothing principle?

Friday, April 07, 2006

C# futures

Its been interesting to see the path static languages like C# and Java have taken over the past year with their 2.0 and 5.0 releases respectively. There has been a drive towards more 'staticness' with generics i.e. more type specification at compile time. This has generally been seen as a good thing... more descriptive, more type safe and more performant (in the case of C# ;-) code. But then you have dynamic languages like Python and Ruby which are essentially the complete opposite with no static type specification at all. All the variables only have dynamic types with everything being inferred.

What's even more interesting is that inspite of this core difference C# has borrowed features for its 2.0 release from dynamic languages with more on the way for 3.0.

Iterators are a well known design pattern for traversing collections. They are a good way to loosley couple collections from the actual iterating process. So one can have multiple iterators, iterating collections in different ways. Iterators are sprinkled all throughout the Java Collection Framework and similarly C# has Enumerators having pretty much the same interface. Creating these iterators/enumerators invovles creating classes which keep track of state. Thats pretty much all they do... some logic to know where you are and to provide the next element. C# 2.0 introduced the yield keyword which generates iterators dynamically that manage the state automatically. Ruby and Python both have it. Here's a simple ex...

class MyCollection {
  private int[] myElements;

  public IEnumerator GetEnumerator() {
   foreach ( int i in this.myElements ) {
    yield i;

Thats it. No creating a class which implements IEnumerator. It's all generated automatically dynamically. Huge productivity booster. Complements the foreach functionality nicely.

C# 3.0 which is a ways off from being released has a lot more in store. Probably the biggest annoucement was about LINQ (Language Integrated Query) which introduces new syntax within the language to work with datasets - collections, relational databases (DLINQ) and xml (XLINQ). This major feature brings with it many smaller ones which again seem to borrow a lot from dynamic languages...

Probably, the most surprising one is Implicit Typing. You can do things like

var i = 1;
var s = "string";
var d = 1.11;
var numbers = new int[] { 0, 1, 2 };

Surprising since it goes against what C#/Java type languages have been known for. But this is a feature needed to make LINQ work since you don't know the final type which will be the result of queries.

Another interesting one is Extension Methods. Ruby has this feature. You can add new methods to existing types without being part of any type hierarchy. You can even add methods to sealed classes like String. As an example, think about a method that checks if a string is a palindrome. Normally, one would create something like a Palindrome class which has a static isPalindrome method which takes a string... Palindrome.IsPalindrome( text ). Pretty inelegant. With extension methods, you can define a method like this

static boolean IsPalindrome( this string text ) { ... }
And call it like this

string text = "civic";
bool palindrome = text.IsPalindrome();

It's a pretty cool feature which, again, is needed to add some LINQ funtionality. But I think there is potential for abuse here and without proper documentation it might cause some confusion.

Lambda Expressions have been available in many languages for a while. C# is finally getting this feature in 3.0. These expressions are popular when filtering datasets and as you can imagine would be an integral part of LINQ. Here's a simple example...

List<int> numbers = new List<int>;
numbers.add( 0 );
numbers.add( 1 );
numbers.add( 2 );
numbers.add( 3 );
numbers.add( 4 );

List<int> evenNumbers = numbers.FindAll( i => ( i % 2 ) == 0 );

So FindAll() will filter the list based on the lambda expression. Syntax seems a bit strange.

A final interesting feature was Anonymous Types. Languages like Python and Ruby have this concept of a tuple which can hold multiple values. So you can have methods returning multiple values. In C# or Java this isn't possible. What many end up doing is to return an array with 2 or more values. Pretty inelegant. Or you have to actually define a type which just holds those values and return that. It's a hassle. With anonymous types you can again dynamically create types without (as the name would suggest) giving it a name...

var person = new { Name = "C Sharp", Age = 4 };
Console.WriteLine( "Name: {0}, Age: {1}", person.Name, person.Age );

Again, as you can guess this is another needed feature for LINQ.

I haven't mentioned much about LINQ itself since I've only read a little about it and seen a video by the man. So dunno a lot of details myself. What would be interesting is to see all the IL that is generated to make all these abstractions work.

Anyway, it's something to look out for. Maybe Java will also be including some data related features for their Dolphin release.

Monday, March 27, 2006


A podcast on the city I call home - Bombay, by Suketu Mehta Author, "Maximum City".

Wednesday, March 15, 2006

Java tip - Get the method call hierarchy

So here's a small tip I learnt recently which I find useful at times. Quite often it is a pain to debug an application. You just want to have a trace from where a particular method was called.

Simply use this


MyClass() {
myMethod() {
new RuntimeException().printStackTrace();


Now whenever myMethod is called; a stack trace will be printed. So you can easily get the hierarchy of the calls made to reach that execution point. Notice that the Exception was not thrown; hence no handling is needed.

Monday, March 06, 2006

JUnit Revelation

I stumbled upon this post by Martin Fowler via another blog. It's a bit dated (2004), but interesting. It reveals that JUnit creates a new instance of TestCase for each test method defined within it. The primary reason for doing this is so that tests are isolated from each other. That is tests don't share the state of objects. So they can be run in any order needed.

JUnit has two special methods setUp and tearDown (these I guess can be called anything in the newer version using Annotations) that are automatically run (if defined) before each test method. I wondered how they did this. Now it makes sense.

Monday, February 27, 2006

Re: JSTL - What's the point?


But I think the JSTL was meant to be a base framework, allowing developers to extend and write their own tags. They're called "custom tags", and they can be pretty useful.

For example, I can write a tag that handles the logic for displaying a set of page number links. Then the web designer just needs to know how to place [foo:pagination style="xyz"/] on the page, and voila, the pagination comes out. Or, you could log an advertisement impression with a simple tag like "[ads:logImpression position="${pos}"/]. Then the code behind this tag can do whatever it needs to do -- in this case, log an impression to the database. The former example results in HTML output, and the latter doesn't.

The examples you give make perfect sense. Placing custom tags which encapsulate all the display logic within them is fine. The designer and the presentation layer don't see any of that logic. This is precisely the idea behind ASP.NET web controls like <asp:DataGrid> or <asp:Calendar>. I only took issue with having tags like <c:if> and <c:foreach> ingrained within the html. I didn't see them as being any improvement over scriptlets within html.

(I had to use [ and ] above instead of less-than and greater-than - blogger wouldn't let me enter less-than and greater-than!)

Yeah < and > are special characters. The browser interprets it as being an html tag and tries to parse it. To actually display it you have to use &lt; and &gt;.

There are some frameworks built on top of servlets/JSP/JSTL that provide higher-level custom tags, such as Struts and JSF, as you mentioned. Components such as "DataGrid" as nice for rapid development, but if you need to customize that component, the built-in tags may not work well. (I don't know much about DataGrid in particular, as I'm not familiar with .NET)

I dunno how custom tags like <ads:logImpression> are developed (I assume using JSTL/EL?), but in ASP.NET these web controls are basically classes. So the <asp:Calendar> control is actually implemented in System.Web.UI.WebControls.Calendar within the .NET framework. Its 'real' code - as in implemented using C#. So all the rules of OOP apply here. All web controls directly or indirectly inherit from the base System.Web.UI.Control class which provides some common functionality. And it's really easy to customize any functionality you would want. Just create your own custom controls that inherit from one of these base controls and override away. These controls also allow you to hook in callbacks for certain events which is another way to customize the controls. Check out this article which provides a nice explanation of both processes.

Could you post or link a simple ASP.NET example showing this separation of UI/model?

One of the areas where the Servlet/JSP model wins is in the clear separation between the control code and the presentation. There are two separate components. Requests (generally) hit Servlets after which they are forwarded to independent JSPs. In ASP.NET, there is only one component - the Page. Although this page is separated between into a .aspx file (UI) and a .cs/.vb file (code) using the Code Behind Model, these are compiled down to one component. So basically all requests go to a Page and then could potentially be forwarded elsewhere. It's sort of "backwards" to the MVC model. So the Servlet/JSP API model is richer in that sense. Having said this, the Code Behind model does have its advantages. Probably the biggest one being all the UI components are represented as objects which can be manipulated in the code behind page. Here is a simple explanation of the concept.

Re: JSTL - What's the point?

What is the logic behind separating presentation from code? Apart from the "MVC pattern"/"loosely coupled principle", it is also to accommodate designers and coders....The "logic" is still ingrained within the presentation. It is still code, just with tags, instead of java. How is this any better for designers or code maintenance? Somehow this made no sense to me.

What you mentioned is very right. See this post section Using the SQL Actions

The JSTL includes a number of actions that provide a mechanism for interacting with databases. The previous sentence should, at a very minimum, send up a red flag in your architectural visions. One might ask, "Do I really want to be able to perform SQL actions such as queries, updates, and transactions from my JSP? Isn't that business logic that belongs in the model?" The answer is yes. Yes, yes, yes. To follow a Model-View-Controller (MVC) architecture, which is the predominant design pattern used in building web applications today, you definitely want to keep your model information in your business logic. This means that you don't want it in your JSPs. Why then are these actions even provided in the JSTL? Good question and one that I've discussed with various members of the JSR-53 expert group. The reason is the "C" or community in the Java Community Process (JCP). The community has asked for it, the community has gotten it.

So thats that. Even the tags Mohnish mentioned are not that great. But... its fine.

Adding to the comment ; JSTL was a another step. Jsp did something better than Servlets. Jstl added something. Then Jstl EL was added which was purposefully given a more JavaScript like syntax. Now we have JSF which gives a more component based web dev feel. I don't know much on JSF though. Also there are actually competing web frameworks in the Java world like Struts, Tapestry... So again loads of choice :)

ASP.NET has the right solution for separation with web controls.

Could you post or link a simple ASP.NET example showing this separation of UI/model?

How is webapp dev in php? Do they too generally look out for such MVC stuff?

Friday, February 24, 2006

JSTL - What's the point?

I've been taking a look at Web related stuff in Java lately. Nothing too complex, just your basic Servlets/JSP. The general concepts behind these web frameworks (ASP[.NET], PHP, Ruby etc...) are all similar. The book I'm reading (Head First Servlets and JSP) has a nice way of explaining the components of the Java system. It starts with the simplest way to accomplish your goal, then shows what's wrong with it and finally how to improve upon it.

First you got your basic requests hitting a Servlet that does some processing and spits out HTML to the client. Nice and simple, but writing all that HTML code within the Servlet is horrible. Enter JSP. JSPs can contain the presentation (HTML). So now your requests hit a Servlet that does the processing after which it redirects to your JSP which has all the HTML code. Now there's a nice separation between code (Servlet) and presentation (JSP). But the JSP is pretty static. What if the presentation needs to be dynamic and depends on the processing done in the Servlet. Enter Scriptlets. These are code segments within JSP that can make the page dynamic. Great... you get nice dynamic pages now, but your presentation is cluttered with code. Enter JSTL (Java Server Tag Library). This is a tag library which is to replace those scriplets. So instead of code you have 'html like' tags.

This is pretty new to me, so I was trying to understand the rational behind it all. The progression made sense from Servlets to JSP to Scriptlets. But somehow the point of JSTL was completely lost on me.

What is the logic behind separating presentation from code? Apart from the "MVC pattern"/"loosely coupled principle", it is also to accommodate designers and coders. Designers can work on the presentation and not have to deal with code. But when you look at JSTL, its core tags are <c:if>, <c:choose>, <c:forEach>, <c:set>, <c:remove> etc... It's just replacing code statements with tags. The "logic" is still ingrained within the presentation. It is still code, just with tags, instead of java. How is this any better for designers or code maintenance? Somehow this made no sense to me.

ASP.NET has the right solution for separation with web controls. You can place these components within a page. These are plain tags like <asp:DataGrid>, <asp:Textbox>, <asp:Labels> etc... They are just responsible for rendering plain html. The logic to decide WHAT they render is placed in a 'code behind' page. Complete separation of code from presentation. Designers don't need to see any logic disguised as tags. I've heard of Java Server Faces which is something that's similar to this thats come up recently. Rahul can expand on it. But I can't believe JSTL was considered a solution at some stage.

Wednesday, February 22, 2006

Outsourcing and Globalization

Seems like stories about outsourcing have cooled a bit. Atleast sources I check haven't been making too much noise about it lately. One of the podcasts I listen to had an interview with a guy who has a small co in NY and outsources to two cos in India - Pune and Delhi. I thought his story was pretty good. Check it out here.

Sunday, February 19, 2006

Broadband as a utility

Really blows my mind... 100 megabits for $25 per month.

Rahul had recently sent me a link to an interview with Josh Bloch/Neal Gafter (java gods) over at javapolis. One of the questions asked was about future directions about the language. Neal Gafter talked about doing more on the client side. He mentioned how the gmail experience would be much better if it could be used as a client app with better offline support. You really have to wonder if this is going to be an issue moving forward with services like what City Telecom is offering - always on connectivity at huge speeds.

Thursday, February 09, 2006

Re: One Thread to rule them all

Since we're having a discussion on threads, this article seemed topical - Threads Without the Pain

Re: One Thread to rule them all

I've used threads in C and Java. In C, I used the pthreads (POSIX threads) library. Of course, it's not as peaceful as threads in Java.

Synchronization is done using mutexes and condition variables. Mutexes allow you to avoid race conditions. Condition variables allow you to wait until any specified condition is satisfied. There's a bunch of functions that are used to do this - pthread_mutex_init/lock/unlock/trylock etc. and pthread_cond_init/wait/broadcast etc.

Of course, thread operations are completely procedural in nature - pthread library functions that take as arguments function pointers/thread variables (pthread_t) and such other stuff.

򪪪򪪪򪪪򪪪򪪪Here's a decent tutorial for pthreads.

Sometimes, it's better to just fork() processes and have them communicate using pipes, semaphores in shared memory etc.

Just a personal opinion - while a lot of things are much easier to do in Java, I would still recommend trying them out in C (or C++) atleast once. You just get a slightly 'inside' view of things... not just threads, even stuff like socket programming. However, for day to day use, Java's a better bet.

Have never used threads in Lisp but I do know that there's a package for threads. While Lisp is projected as an AI language, it has support for a whole lot of things from threads, sockets, interfacing with the OS etc. (OT - While the 'biggest deal' about lisp is its natural use to do functional programming, it also supports procedural and object oriented programming)

Monday, February 06, 2006

Re: One Thread to rule them all

The generic question now. Have you guys come across any similar stuff in other languages? Java has had good support for threading since the early days and now in Java 5 this has been greatly enhanced. What about other languages? C++, C#. And anyone have info about dynamic languages like Lisp, Python etc?

I haven't seen the pattern (WorkerThread) that you wrote about... having one worker thread manage multiple tasks. I don't think this pattern is there in .NET. From what I understand, .NET has a different framework. There is probably a one to one similarity with the Threading package (atleast pre Java 5.0). However, .NET has an asynchronous framework built into all delegates. You can invoke delegates with myDelegate.BeginInvoke() and it will run asynchronously. That is, control will be immediately returned. I believe it picks a thread from a ThreadPool and executes it on that in the background. It's pretty nice in that all your own custom delegates get this feature for free.

When talking about UIs and threads there's another important aspect. Any updates to UI controls should only be made by the thread that created it. So if you have a background thread doing some work and you want to display a message in the UI when it's done, you can't simply access the control and update it directly. You need to marshal any updates through the 'owner' thread. Here's an example in C#...

// UI
public class ClientUI : System.Windows.Forms.Form
  private System.Windows.Forms.Label lblStatus;


  private void UpdateStatus()
    if ( this.lblStatus.InvokeRequired )
      this.lblStatus.Invoke( new MethodInvoker( this.UpdateStatus ) );
      this.lblStatus.Text = "Done!";

All UpdateStatus() is doing is setting a property on a Label. UpdateStatus() would likely be registered as a callback. So when the background thread is done with its processing, it would raise an event and UpdateStatus() would get called. When it does, it can't just update the control since it is not the 'owner' thread. So it needs to marshal the call. This is done with this.lblStatus.Invoke(). MethodInvoker() is just a delegate which takes methods that don't have any arguments and returns nothing. Invoke() takes care of the marshalling.

What's the if/then statement for? InvokeRequired is a property on every UI control. It will tell you if the call was made from the thread that owns the control or not. If it does own it, it's just a direct update, if not, it needs to be marshalled.

All UI elements inherit from the Control class and all of them in turn have the InvokeRequired property and Invoke() method.

One Thread to rule them all

In UI Applications it is a requirement to run some tasks in different threads so that the user will not notice a lag while performing some operations. Also often these threads are either of not very long duration or need to be run one after the other. So how do we solve this?

A small diversion to Threads in Java

Now in Java the Runnable interface is used to create threads. Runnable contains a single method run() which needs to be implemented.

public class MyThread implements Runnable {
public void run() {
//do some task

To execute the above as a separate thread you need to call the start method of the Thread class. The Thread class can accept a Runnable instance

new Thread(new MyThread()).start()

start() internally performs some housekeeping to actually create the new thread. After performing the necessary operations, is called.

Back to our UI Application.

What is done is a simple event queue is built for handling all non-ui tasks. Which internally contains a Queue and a single thread.

public class Worker implements Runnable {

private Queue queue;
private boolean running;

private Worker INSTANCE = new Worker();

private Worker() {
new Thread(this).start();

public static Worker getInstance() {
return INSTANCE;

public void run() {
if(!running) {
running = true;
while(true) {
if( queue.peek() ) {
Runnable runnable = queue.pop();;

public void addRunnable(Runnable runnable) {


I have just given a basic skeleton here and left out the most important part of synchronizing the class. The running boolean was added to prevent new Thread(Worker.getInstance()).start(). You guys see any more errors? Threads are one of my primary weaknesses which I hope to rectify by learning about the new Java 5 Concurrency features.

For small tasks it is more efficient to use a Worker like this which internally doesn't create a new Thread for each task and re-uses a Thread or even a thread pool. This is because the start() method does quite a bit internally.

In Swing it's suggested to use a SwingWorker class which does something similar. Also Java 5 (Tiger) has added a new SwingWorker class which is additionally Generic.

The generic question now. Have you guys come across any similar stuff in other languages? Java has had good support for threading since the early days and now in Java 5 this has been greatly enhanced. What about other languages? C++, C#. And anyone have info about dynamic languages like Lisp, Python etc?

Saturday, February 04, 2006

Refactoring enhanced

Probably the single most common refactoring one does when writing code is renaming variables. Saw this cool feature in Visual Studio 2005...

Monday, January 30, 2006

Re: Google China

I think Diana Hsieh puts it very eloquently here.

I wish Google had taken a stand and said no to the Chinese Government, but ultimately its their company and they have a right to do what they wish with their search results.


Friday, January 27, 2006

Google China

Found this article (on yahoo ;-) - Google's Action Makes A Mockery Of Its Values.

You must have heard about Google sensoring their search results to meet China's demands. What dyou think? Once a company goes public, it takes on many more responsibilities. Although an admirable motto - Do No Evil - it was always going to be hard to stick to that when you have to satisfy shareholders. Growth in revenues and profits becomes your number one goal. That is automatically seen as 'evil'. Which is another interesting point... you think a super successful company can ever be seen in a favorable light?

Thursday, January 26, 2006

Re: Another greasemonkey script....

A few minor changes... Instead of an alert, a message is displayed at the top left of the screen for 5 seconds. In the code, the literal "constants" are moved outside of the for loop. The check for presence of both reference and code is moved inside the loop.

// ==UserScript==
// @name Google Analytics Detector
// @namespace
// @description Detects if a page uses Google Analytics
// @include *
// ==/UserScript==

var URL = "";
var TRACKER = "urchinTracker()";

var scripts = document.getElementsByTagName( "script" );

var refPresent = false, codePresent = false, log = false;

for ( var i = 0; i < scripts.length; ++i ) {
var script = scripts[ i ];

// Check reference if not already found
if ( !refPresent ) {
var ref = script.src;

if ( ref != null ) {
refPresent = ( URL ) != -1 );

if( log )
GM_log( "Tested ref: " + ref + " Result: " + refPresent );

// Check code if not already found
if ( !codePresent ) {
var code = script.innerHTML;

if ( code != null ) {
codePresent = ( TRACKER ) != -1 );

if( log )
GM_log( "Tested code: " + code + " Result: " + codePresent );

if ( refPresent && codePresent ) {
var logo = document.createElement("div"); = "logo";
logo.innerHTML = '<div style="position: absolute; left: 0px; top: 0px;' +
'border-bottom: 1px solid #000000; margin-bottom: 5px; ' +
'font-size: small; background-color: #000000; z-index: 100;' +
'color: #ffffff; width:200px; opacity: .75;"><p style="margin: 2px 0 1px 0;"> ' +
'<b>Google Analytics enabled</b>' +

document.body.insertBefore( logo, document.body.firstChild );

function() {
var logo = document.getElementById( "logo" );
if ( logo ) {
logo.parentNode.removeChild( logo );
, 5000 );

Re: Another greasemonkey script....

Here's the first attempt...

// ==UserScript==
// @name Google Analytics Detector
// @namespace
// @description Detects if a page uses Google Analytics
// @include *
// ==/UserScript==

var scripts = document.getElementsByTagName( "script" );

var refPresent = false, codePresent = false;

for ( var i = 0; i < scripts.length; ++i ) {
var script = scripts[ i ];

// Check reference if not already found
if ( !refPresent ) {
var ref = script.src;

if ( ref != null ) {
var URL = "";

refPresent = ( URL ) != -1 );

GM_log( "Tested ref: " + ref + " Result: " + refPresent );

// Check code if not already found
if ( !codePresent ) {
var code = script.innerHTML;

if ( code != null ) {
var ACCT = "_uacct";
var TRACKER = "urchinTracker()";

codePresent = ( ( ACCT ) != -1 ) && ( TRACKER ) != -1 ) );

GM_log( "Tested code: " + code + " Result: " + codePresent );

if ( refPresent && codePresent ) {
alert( "This smart bastard is using Google Analytics" );

Save as analyticsDetector.user.js, open in Firefox and then "Install This User Script..."

Couple things... Besides the code snippet Rahul posted, there is also an external js file that is referenced ( I check that both code as well as reference are present. Also in the code, I think "_udn" is optional. It's there on slashdot, but not on codeword, so I don't check for that. Didn't test this very much. Just on codeword, slashdot (detected) and yahoo, microsoft (undetected).

Any improvements?

Wednesday, January 25, 2006

Introduction to Great Design

Joel Spolsky is at it again. He's started a series of articles on design. This one was a bit of deja vu for me. On the flight from Bangalore to Bombay, they made the announcement to switch off all electronic devices. So off goes my cell phone. After landing, I starred at the phone for a minute before giving up and asking Rahul how to turn it on. After reading Joel's article I don't feel like as big a dumbass. How long before we see an Apple cell phone?

Also check This is Broken - "A project to make businesses more aware of their customer experience, and how to fix it". It's a nice site with daily postings on design mistakes seen everywhere.

Tuesday, January 24, 2006

Another greasemonkey script....

I submitted the following as a greasemonkey script ( idea proposal


Quite a few sites use the Google Analytics service. Sites insert javascript code like -

<script type="text/javascript">
_uacct = "UA-32013-5";
_udn = "";

(The above code is from slashdot ).

A script could be written which shows a simple notification whenever a site visited ; contains Google Analytics javascript.


Any takers??

Monday, January 23, 2006

Some free books....

I'm trying to read a bit on Lisp. This interest was spurred by an essay and a podcast by Paul Graham. Dunno how far i'll go... but trying to get a feel of other languages as well.... Also try reading some of his essays.

Basically I got a few books at You guys could also get something on your topic.

Wednesday, January 18, 2006

Book Reviews

Trying to gauge how good a book before you buy it can be a pretty hard task! Money doesn't exactly grow on trees ;o) One site I have found useful and insanely accurate in terms of their book reviews is This is primarily a C++ site but they also give reviews for C# and Java.

Before buying any book, I usually run a search through there to see how its rated.


Modern C++ Design

I would really recommend you to try out "Modern C++ Design by Andrei
Alexandrescu". It is a fantastic book delving deep into C++ Template
Techniques. Its not something a novice can read, but yet if you are
comfortable with C++ templates, then you should definitely give it a

He opens your eyes to a lot of interesting techniques and in the process
also unravels "Loki", the library (written by him) which implements all
of the techniques!

Policy class, local classes, typelists - the list just goes on, its all
there!! He shows what you can do with even the most simple template
classes viz :-

struct CompileTimeChecker
CompileTimeChecker(...) {}

template<> struct CompileTimeChecker<false> {};

This relatively simple struct can achieve compile time assertions for

Read it! Its worth it!


Monday, January 09, 2006

Use that extra bit of vegetable oil at home

This is really one of the most hilarious things I've ever seen -

Keep it cool ;o)