I've pasted an early version of a recursive function to walk the DOM from a particular node in Javascript. If you run the code though.. your browser will hang as it goes into a recursive loop. So whats wrong with the code?
<html>
<head>
<script type="text/javascript" language="javascript">
function walk(node) {
if (node) {
//do something with node
for (i=0; i<node.childNodes.length; i++) {
walk(node.childNodes[i]);
}
}
}
</script>
</head>
<body onLoad="walk(document.body)">
<a href="http://www.parivartana.org">parivartana.org</a>
</body>
</html>
The variable i in the for loop should be declared as 'var i' to fix the behaviour. Thats when I realised that I should not really code in Javascript assuming that it is a subset of Java. (But being as lazy as I am never really studied the language).
Then I came across some fantastic videos on Javscript by Douglas Crockford at the YUI theatre which is a part of the Yahoo Javascript library YUI. Some of the features of the language are explained really well. Some key features are Objects as containers, Prototypal Inheritance and Lamda. He also explains some browser and Javascript quirks.
Now I am wondering if Javascript should be a part of my resume!
One of his recommendations is to favour minification ie removing of whitespace to reduce download size versus obfuscation. Google actually always heavily obfuscate their Javascript. Also as part of the Google Web Toolkit, the deployable code is also obfuscated. So thats a debateable topic. Time to read some GWT generated code then.
Friday, March 09, 2007
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.
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 foos ( foo_id INT NOT NULL, PRIMARY KEY (foo_id) ) ENGINE=InnoDB;
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..
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..
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.
CREATE TABLE foos ( foo_id INT NOT NULL, PRIMARY KEY (foo_id) ) ENGINE=InnoDB;
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..
| foo_id | foo_id | bar_id |
|---|---|---|
| 2 | 2 | b |
| 3 | 3 | b |
| 4 | NULL | NULL |
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..
| foo_id | foo_id | bar_id |
|---|---|---|
| 1 | NULL | NULL |
| 2 | 2 | b |
| 3 | 3 | b |
| 4 | NULL | NULL |
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.
Sunday, December 10, 2006
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.
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
JDEE
Happy editing!
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
JDEE
Happy editing!
Saturday, November 11, 2006
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
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.

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
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:
What I did to resolve these issues:
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!
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
shows
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
shows
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 1.3.0.53 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
Dependency biculturalism
Nicely put: What do dependencies have to do with Free Software?
This relates closely to Biculturalism which Joel talks about.
This relates closely to Biculturalism which Joel talks about.
Monday, October 09, 2006
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.
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:
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>
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
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
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 ;)
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 ;)
Wednesday, August 16, 2006
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
CREATE TABLE A (
....A_ID INT,
....PRIMARY KEY A_ID,
....B_FK INT,
....FOREIGN KEY B_FK
....REFERENCES B(B_ID)
);
CREATE TABLE B (
....B_ID INT,
....PRIMARY KEY B_ID,
....A_FK INT,
....FOREIGN KEY A_FK
....REFERENCES A(A_ID)
);
... now this will not work as during create, table B does not exist
A) So one solution is ...
CREATE TABLE A (
....A_ID INT,
....PRIMARY KEY A_ID
);
CREATE TABLE B (
....B_ID INT,
....PRIMARY KEY B_ID,
....A_FK INT,
....FOREIGN KEY A_FK
....REFERENCES A(A_ID)
);
ALTER TABLE A ADD COLUMN B_FK INT AFTER A_ID;
ALTER TABLE A ADD FOREIGN KEY B_FK REFERENCES B(B_ID);
A little bit of cheating does the trick :)
A) Another mysql specific solution is to use the FOREIGN_KEY_CHECKS variable.
mysql> SET FOREIGN_KEY_CHECKS = 0;
mysql> SOURCE dump_file_name;
mysql> SET FOREIGN_KEY_CHECKS = 1;
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
Q) How had to create Tables with a circular dependency between them. Like
CREATE TABLE A (
....A_ID INT,
....PRIMARY KEY A_ID,
....B_FK INT,
....FOREIGN KEY B_FK
....REFERENCES B(B_ID)
);
CREATE TABLE B (
....B_ID INT,
....PRIMARY KEY B_ID,
....A_FK INT,
....FOREIGN KEY A_FK
....REFERENCES A(A_ID)
);
... now this will not work as during create, table B does not exist
A) So one solution is ...
CREATE TABLE A (
....A_ID INT,
....PRIMARY KEY A_ID
);
CREATE TABLE B (
....B_ID INT,
....PRIMARY KEY B_ID,
....A_FK INT,
....FOREIGN KEY A_FK
....REFERENCES A(A_ID)
);
ALTER TABLE A ADD COLUMN B_FK INT AFTER A_ID;
ALTER TABLE A ADD FOREIGN KEY B_FK REFERENCES B(B_ID);
A little bit of cheating does the trick :)
A) Another mysql specific solution is to use the FOREIGN_KEY_CHECKS variable.
mysql> SET FOREIGN_KEY_CHECKS = 0;
mysql> SOURCE dump_file_name;
mysql> SET FOREIGN_KEY_CHECKS = 1;
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
Subscribe to:
Posts (Atom)

