Coding, is? Fun!

Sunday, February 25, 2007

Daylight Saving Time Patch from Microsoft breaks apps

The Microsoft patch applied by knowedge base for switching the Daylight Saving Time (DST) from April to sceond Sunday of March starting 2007 caused some damage to our applications. The MS Java Virual Machine is used by us and the Java Date string format looks like "Sunday 25 Feb 12:23:39 AM EST 2007" in an unpatched server. If we pass this to Javascript to initiate a new Date object, it works.
That is, if you did Date date = new Date() in Java and applied the toString() method on it, it renders the string with timezone as "EST".
BUT, if you patched the server with the above Microsoft patch, then the Date string looks like:
"Sunday 25 Feb 12:23:39 AM GMT - 05:00 2007". So, note that now the timezone is rendered with respect to GMT. If you pass this to Javascript, it returns NaN.
That is, some change that Microsoft made to the OS in the above DST patch seems to change the way the Java Date object renders itself as a string. I do not know if this affects the Sun JRE.
How do we fix this? As of now, Microsoft has not been talking about a fix. We plan to fix it by overriding the Date object's toString method and creating our custom one.
Relevant links are in:
One of my colleagues contacted Microsoft and they replied Microsoft had no planes to fix this patch.
There is also information for .NET applications in the above links.

Sunday, February 18, 2007

To Saral Or Not To Saral

I believe there are two kinds of men – those who look forward to filing Form-16 (saral or whatever) and those who get diarrhea thinking about it.
My friend Ramesh is of the first kind – he said he feels the thrill of adrenaline as he contemplates a tax form; he knows Tax law better than most lawyers. He has a separate tax chair where he sits and pores through the ancient lore of tax evasion; loopholes; deductibles;HRA; nothing upsets him.
I, on the other hand, think deductibles are sea monsters.
I believe tax evasion has a long history – if you asked American scholars (Howard Bloom, as an example) they may trace the earliest tax evasion to Athens; and go on and on about how they fought against taxes and the Persians. In American history, everything traces back to Athens; then they move onto Rome and then jump straight to 1776.
But why do people feel the need to not pay taxes?
I interviewed an anonymous source (Ramesh) for this and he explained I am looking at this the wrong way – it is not that he is avoiding tax; he said tax loopholes are similar to the Easter Egg celebration. It is an annual game in which the government hides some statement such as “Whereas the wherewithal for hereupon the party of the first thereafter habeas corpus Ramesh no pay tax” in the 1179th page of the budget. And then they go, “Now go find it”, to all the kids. Ramesh wants to be the first to find it.
What does finding that information give him (apart from lots of money)?
To find out, I asked him, “Why do you find loopholes in tax law?”
And he said, “Because they are there”.

My company’s accounts department helpfully sent all employees an excel sheet with formulae for everything so that we could easily figure out the tax we owed.
I opened the sheet and looked at rows and rows of “HRA (18-C)” and thought, “This must be simple”.
I entered all my information and scrolled down eagerly to look at my tax liability.
It came up to 200000036749.50.
It seemed huge even to me; I experimented with adjusting the numbers and got it down to 174642397.8.
At this point I thought I needed to get out of the country fast because I seemed to be paying the most tax in the country. I had two choices – either get out of the country or go to the accounting department at 4th floor.
I steeled myself and decided to get out of the country.

I had heard horrible stories of the 4th floor accounting section – there were people who went there and never returned. Some came back permanently scarred waking up screaming from nightmares in the middle of the night.
The accounting section also sent weird emails with lots of explanations (that they probably thought were helpful). I put them in junk mail and was now paying for it (literally).
I was packing up to go to some place where there were no taxes (Moon – I know that the astronauts in the International Space Station pay taxes; it is called tax deduction from space) when a couple of other tax-challenged zombies suggested going up and talking to the accounting guys. I went up.
I have always noticed that accountants have the same smirk on their face when talking to employees as auto mechanics. Have you ever tried to explain what is wrong with your car to a mechanic? He scratches his ass when you are talking, shares jokes with other mechanics, blows his nose, spits on the ground and then blows cigarette smoke in your face. The reason is they believe you are a moron – if you do not know that the carburettor power screw nuts are slightly bent, then how good a programmer are you?
Accountants are the same way – if you do not know that section 418C is one of the three health savings account deeds under which you can claim standard exemption – what good are you? Why do you even exist? You are the reason the country is going to the dogs.
(By the way, both automobile servicing and accounting are apparently men’s jobs. I have never seen a single female accountant yet.)
But accountants do not scratch their asses while you are talking to them. You have to give it to them. They are much more sophisticated.
So the accounter guy stared at me with thinly vined contempt.
“This is your excel sheet?” he asked me, showing the printout.
He surveyed it and then showed it to the guys sitting next to him. They all had a hearty laugh.
He then asked me what I paid last year.
I told him.
He smiled again and said, “I did not ask your yearly tax; I am asking about the monthly tax”.
I said, “That is the monthly tax”.
Now all of the department was paying attention to me. I smiled to myself; “so you bastards now know how important I am”.
But the accountant said, “You cannnot have these kinds of savings for that monthly tax. You need to buy a home”.
I was still smiling. “I am going to buy one soon”.
The accountant nodded, looked at the excel sheet and did some calculations. Then he said, “You are not going to be paid for the next 4 months.”

“And we need your passport.”

Tuesday, February 13, 2007

Thinkers and Designers

I was chatting with a potential recruit to my company. He was thought to show promise by HR; he was confident, expressed himself clearly. Around 5 years of experience.
I asked him what his role was in his company. He said he was a designer. (Ofcourse, he meant software design). I asked him if I could ask him coding questions.
He gave me a condescending look and said he had not coded for the last 9 months. He purely "designed".
I asked him how he "designed" without reference to technology; he dismissed that question. He just decided how things should work. It was upto lesser "programmers" to implement his design.
He also informed me that his company had a special design team; that people were promoted to it for "outstanding" performance. They apparently stopped the lowly job of coding on reaching that team; they just designed.
I asked him specific questions on a couple of simple technologies and he had no clue. He had no knowledge about design methodologies; software development processes or any knowledge of design as a discipline.
I have always been surprised by this - how can technical people, in the beginning of their career, reject coding as menial labor and style themselves as designers? Isn't that career suicide? I would never hire a person to a technical position if they did not know hands-on coding. It is my belief that you can never really design a good software system without knowing the technologies used to some degree. Technology-agnostic design does not exist.
Does this mean a .NET architect can never design a good Java based enterprise system? It does not mean that. But it is essential to have a good grasp of atleast one suite of application technology.
As an example, let us say you are asked to create a data warehouse that aggregates data from different systems. It does help to know the concepts of Extraction, Tranformation and Loading; it helps to know multi dimensional querying; it helps to know dimensions and measures and aggregate queries. If you did not take into account specific technologies; if you have not even tried your hand at MDX; I would claim you cannot come up with a useful design.
All that you can do is draw a few blocks and say this little box talks to that big box and sound knowledgeable. CTOs can do that - not system designers. Design is more than that. It is actually creating an integrated system taking into account functional and non-functional requirements. If someone says "I sit and design a web application without being skilled in any programming language", then that person is a worthless hire.
I had the same experience with a guy in my neighborhood. He was 3 years out of college and he said his job was to "think". I am serious - he said his company pays him to think. I pictured him sitting in his cubicle contemplating the monitor. He thinks and he thinks and then he said he comes up with a solution. He then kindly shares it with the dumb programmers and they go and just do the simple job of coding. He said he did not do coding. I was surprised by this and he explained to me that he does code - sometimes - because he takes it upon himself to do a module. He was careful to clarify that nobody actually makes him code the module; he does it out of the goodness of his heart.
Actually that is what this attitude comes down to - these guys are ashamed to say they code because then that means they take orders from someone. They bow to no one. And they want to be tech leads.
That is the other aspect of interviewing - I have noticed that some people love to be questioned on "concepts". To them, a set of coding questions is insulting - one guy I interviewed in Philadelphia said it was like a quiz. I think this is just a sham attitude. If you did hands-on coding, it is the duty of the interviewer to find out. I can keep on asking vague questions about your last project, your attitude to team work, your strengths and weaknesses and crap like that - it does not help in judging your value as a technical person. A technical perosn must know coding and must answer specific coding questions, period.

Wednesday, February 07, 2007

Brief Introduction - OR Mapping

Varun asked me some details on a database access layer. This post is an attempt to introduce developers to the complex field of Object Relational(OR) Mapping. I conducted a presentation on this subject with reference to EF internal frameworks. I have also given a set of links at the end.
For the purpose of this article let us consider that we are asked to design the middle tier and database structure of an online book store. A customer can order different books in multiple quantities. There is a Customer table; an Order table linked to the Customer; a Book_lkp table that contains the list of books displayed in the website. A Customer places multiple Orders of different books.
The Middle Tier has to provide the ability to:
1. Load a set of books for display
2. Load a Customer data
3. Save an Order for a Customer
One of the ways to design a data access layer for the above problem (in .NET) is:
Load a Dataset with the Customer and Orders and then save after making changes.
This approach is fine for simple websites - but you have to handle business logic in a set of external classes. Strongly Typed Datasets help you add type safety.
The problem with the simple approach is that for complex websites with intricate relations, your business logic will be very distributed and incoherent.
Typically your middle tier should model your business - you will need an object model. Unfortunately, databases are relational. They are not object based. For example, databases do not capture inheritance. In the above example, let us say a Customer can be a corporation also. In the middle tier, you can describe a Customer base class and derive an Individual class and a Corporate class from it. It is difficult to do this in a databse structure. Databases also do not have object identity; they do not handle relations intrinsically.
So, fundamentally, a relational database represents data and its relations completely differently from a object model. The Middle tier needs a good object model. Therefore, the problem is to map the object data to relational data. This is the problem that Object Relational mapping patterns try to solve.
One of the patterns for solving this problem is called Active Record.
Active Record
When you use this pattern, you decide that your object model in the middle tier is going to mirror the database model - that is for every table you create a class and then handle foreign key relations through child collections.
So, in the Book Store example above, you will have a Customer class, an Order class and Product class. The Customer class will have a child collection of Orders.
When you use Active Record, each class is responsible for saving and loading itself. The Customer communicates with Customer table, Order with Order table and so forth.
Commonly, in frameworks, the code for these classes are generated from the database itself. For example, a Customer class will be generated with a property for each column in the Customer table. Also, since you do not want to embed the actual mechanism of communicating with the database in each class, all these classes will derive from a base class - which handles the connections and executes the queries. (Ruby on Rails uses this pattern).
Using this pattern, you will first start with a database design and you will not spend much time on an object model design.
Since the database structure is replicated in the middle tier, the design of your object model cannot include the subtleties of a pure object model. For example, Active Record makes subclassing difficult to handle.
If you are a purist and do want to design a object model that mirrors your business instead of the database, you can use another set of patterns called Mapper.
Data Mapper
When you design an object model independent of your database model, you will still want some mechanism of saving the data to the database. Using Mapper, it is the responsibility of external classes to "map" your object structure to the database. The Mappers will handle database access and executing queries.
In the above example with the Individual and Corporate classes, you will write a base mapper for the Customer class and then derive a mapper each for the Individual and Corporate classes. The Individual and Corporate classes themselves will have no knowledge of the database. They can contain business logic; but they will be passed to their Mapper classes for saving and loading. The mappers would contain the logic to map the properties to columns. The mappers will contain the database stored procedure names, typically.
Contrast this with Active Record where the mapping is done by the middle tier objects themselves.
One way to understand this is to contrast the code given below. In the first line, a Customer object saves itself. In the second, a mapper saves the Customer object:
Customer c = new Customer(); = "Ram";;

c = new Customer(); = "Ram";

OR mapping has different shades of the above patterns. There are many open source OR mapper frameworks such as Hibernate. All of them promise configuration driven mapping; but Ted Neward and others think there cannot be a perfect mapper and that each of these need some customization. Ted Neward has even compared OR mapping to the Vietnam war here.
There are lot more details in the Wikipedia article here.
Developing your own mapper framework is not an easy task. You may be better off working with an open source framework or use Active Record with code generation.
There a whole set of patterns related to these mappers such as Lazy Loader (for loading child collections at the point of usage); Assembler (for assembling the objects); Separated Interface (for isolating the mappers). The best book reference for these is "Patterns of Enterprise Application Architecture" by Martin Fowler.

Monday, February 05, 2007

Object Oriented Javascript - an introduction

Working in Photon, I get a constant stream of queries from developers about Object Oriented Javascript (OOJ). Many, of course, come across OOJ syntax in some maintenance code - and completely panic. I don't blame them - this was my reaction too when I first saw syntax as below:


What the hell does the above mean? What is prototype? Why is the function keyword placed after an equals sign?
I will present here a very essential guide to OOJ. At the end of it, hopefully you won't be scared by OOJ syntax; will be able to maintain such code properly; and even create your own classes in Javascript. To create Ajax applications, it is essential to be able to create client side object libraries.
Some of the abilities of an OO language are:
- to define classes;
- to define constructors for initialization code;
- to define methods attached to such a class;
Let us see how these work with an example. Let us say you want to create an address book. The address class has three properties - address1, city and pin code. You would like to populate the addresses from a html page. Any validation will be handled by the address class.
In OOJ, you create a class and a constructor at the same time:
function Address()
this.address1 = "";
this .city = "";
this.pincode = "";

You have defined a Address class. It looks just like a function; but Javascript knows it is a class if you use the above class like this:
var addr = new Address();
Note that you define the constructor at the same time; the above function is also the constructor. You can pass parameters to it thus:
function Address(address1, city, pincode)
this.address1 = address; =
this.pincode = pincode;

var addr = new Address(addr1, city, pin);
Now, the members of this class are declared within the class. This is similar to C# or Java:
var addr = new Address();
addr.address1 = "1 second street";
You can refer to member properties using the "." operator.
So, we have defined a class; a constructor; an some properties. Now it is time to attach some functions. Let us say there is a function to validate the pincode; there are two ways to attach it.
The first is:
function Address(address1, city, pincode)
this.address1 =
address; =
this.pincode = pincode;
var re = /\d{6}/g;
return re.test(this.pincode);
So, validatePin is a method of class Address. To use it:
var addr = new Address();
addr.pincode = "600020";

You can handle parameters too.
The second way to attach methods is using the prototype object. In Javascript, every object has a internal hidden property called the prototype. You use the prototype property to attach methods to a class:
function Address(address1, city, pincode)
this.address1 = address; =
this.pincode = pincode;

Address.prototype.validatePin = function()
var re = /\d{6}/g;
return re.test(this.pincode);
The weird syntax of functions confuses people. But, in Javascript functions are first class objects and can be declared anonymously.
In both the above examples, note that you refer to other properties in the class using the "this" keyword.
What if you wanted to call a method from another method?
var addr = new Address();
addr.pincode = "600020";
Address.prototype.validate = function()

return this.validatePin(this.pincode);
Address.prototype.validatePin = function(pin)
var re = /\d{6}/g;
return re.test(pin);

The most common mistake in OOJ programming is to forget to use the this keyword when referring to other members. This will throw a syntax error.
The above illustrates creating and using a class using OOJ. Classes can use other classes; therefore you can create a proper class library. As an example, you can have an address collection as below:
function AddressList()
this.addresses = new Array();
this.currentAddress = null;
this.defaultAddress = new Address();
this.addresses[this.addresses.length] =
and so forth.
These class libraries are the basis for creating Rich Internet Applications (RIA). If you used global functions and properties everywhere, you will rapidly lose the ability to maintain.

OOJ does not have the ability to automatically handles setters and getters. It also does not have the ability to handle access modifiers such as private, public or protected. This does cause some problems from a pure OO perspective.
OOJ will work in all browsers that can handle Javascript - it is a fundamental feature of the language.

OOJ and Events
When calling methods on a class you do not have to worry much about object references. But, when you dynamically create user interface elements such as divs, you may have to handle events on them. It gets somewhat tricky to handle this:
function TextBox()

The above code will work.
I suggest that when you work with Javascript, and you use external files, consider using OOJ; it adds lots of clarity to the code.

Friday, February 02, 2007

The Multi-lingual Textbox concept

A few days back, one of the PMs in the SME division in Photon got in touch with me - they had a client who was asking for a multi-language capable textbox. The idea is that an user can type in a language they do not know or are learning(such as Portuguese) ; as they type, they get a dictionary of words and their meanings. They can choose a word and it should be highlighted.
The client was technical - so he had already created a basic textbox along with several Javascript classes to manage the dictionary. The problem was he wanted a textbox in which styles can be applied. Now, the basic HTML textbox does not let you style. The solution is to use a that would be editable.
The client thought we can use a cursor gif file (that is an artificial cursor) and then manipulate it back and forth as the user types. It was obvious to me that this approach would be difficult to maintain; further, the client also wanted to manage Arabic with Right-to-Left text using this approach.
So I researched a little bit and found that the div tag has a property in IE - called contentEditable. Setting this property to "true" has the effect of making it editable. Unfortunately this property is only available in IE (and in Safari, I think). Firefox won't work with it.
Firefox and other browsers try to implement such editable controls using a property called designMode. Setting designMode to "on" has the effect of making a control editable.
This kind of editable divs are, of course, the essence of any Content Management System (CMS). For example, most CMS have a Rich Text Editor (RTE); in fact the blogger window I am typing this in, is an RTE. Most RTE out there (even Google's mail composer) use the above two options - contentEditable in IE and designMode in Firefox.
Both the above properties are not part of any DOM standard.
Because I used contentEditable, handling Arabic was extremely easy - it was reduced to setting the dir property:

divTag.dir = "rtl";

There are a few gotchas about using these properties:
  1. In Firefox, setting designMode to "on" has the effect of reloading the document. Typically you use an iframe to host the editable region. If you set designmode to "on" in the window load event handler, it will reload the document and refire the load event and so on in an infinite loop. I managed this by using a hack - making sure the document does not load twice by using a global flag.
  2. In IE, the markup that IE generates for the inner HTML in the editable div is very non-standard. You can read more about this here:.

You can find more details on building an RTE using Mozilla from the Midas spec