Weird JavaScript Date Bug

Today I found a very weird bug in JavaScript – The date function. I never expected to see a bug in the standard JavaScript implementation. Here is my code. It does nothing more than adding 21 days on the reference date:

var referenceDateObject	= new Date(2011, 9, 10);
var deliveryDateObj 	= new Date(referenceDateObject.getTime() + 24 * 60 * 60 * 1000 * 21);
var deliveryDateMonth	= parseInt(deliveryDateObj.getUTCMonth());
var deliveryDateString	= deliveryDateObj.getUTCFullYear() + '-' + deliveryDateMonth + '-' +  deliveryDateObj.getUTCDate();

Given a reference date of September 10, 2011, I expect the date should be October 1, 2011. However, it ends up returning me September 31, which doesn’t exist!

I test that on Firefox and Google Chrome, both give the same problem. A quick fix will be moving the date calculation to the server side.

P.S. MySQL date function automatically convert 9/31 to 10/1, so the data on the server looks fine.

Update – That’s my fault. The Date object treats the month from 0-11 instead of 1-12. Here is a corrected version:

var referenceDateObject	= new Date(2011, 8, 10);   //That's September 10, 2011, not August 10.
var deliveryDateObj 	= new Date(referenceDateObject.getTime() + 24 * 60 * 60 * 1000 * 21);
var deliveryDateMonth	= parseInt(deliveryDateObj.getUTCMonth()) + 1;
var deliveryDateString	= deliveryDateObj.getUTCFullYear() + '-' + deliveryDateMonth + '-' +  deliveryDateObj.getUTCDate();

That will do the trick.


Our sponsors:

Enough “Rails Surprise”! Goodbye Ruby On Rails.

Couple months ago, I published an article about Ruby On Rails. I was wrong.

The idea of Ruby On Rails is really good. You can create a very simple database-driven web application (i.e., User can add, browse, remove and search records) in less than half an hour. There is no way to do something similar in such a short time using other programming languages. However – perhaps this is the tradition of the Rails development team, it comes with surprise (“Rails Surprise”) every time after upgrading the Ruby gems component. They either make your Rails application couldn’t start, or some features in your apps are not functioning after the upgrade.

What kind of people should stick with Ruby On Rails?

  • Someone is big fan of Ruby/Gems/Rails.
  • Someone has plenty of spare time.
  • Someone likes to sharpen his/her debugging skill – To debug the backward incompatibility mess introduced by the Ruby/Gems/Rails core development team.
  • Someone is patient and is willing to spend number of hours(could be days) to look for workaround.

I am neither. I think I already have enough “Rails Surprise”. And I will make my own web application framework from now on (The code name is called ICEPIZZA by the way). It will be simple, elegant and backward compatible.


Our sponsors:

Ruby On Rails gives no “surprise” in the latest version

I haven’t upgraded my Ruby On Rails and related Ruby gems for 5 months. One of the reasons is that Rails development team likes to break the backward compatibility, such as:

NameError: uninitialized constant ApplicationController

To avoid this kind of surprises, I tend to stick with a working, stable version.

Recently, I upgrade the entire Ruby On Rails family(2.3.2 -> 2.3.4), and surprisingly, my Ruby On Rails apps work perfectly fine! One exception is that the Mongrel server behaves a little bit slower than before. One way or the other, we need to use the load balancing technique to host the Rails App anyway. So it really doesn’t matter.

In case you want to try upgrading your Ruby Gems and other things:

Update the Ruby Gem Engine:

sudo gem update --system

Update other Ruby Gems:

sudo gem update


Our sponsors:

IE is “fxxking”, but it is not an excuse to leave bug in the codes

Today, my co-worker found that the web-application he developed worked only in Firefox, and it didn’t work in Internet Explorer(IE). He immediately asked me if we should ignore the IE users or not.

I know that he was working on JQuery(Javascript). It is not surprised that IE does not support the standard JQuery. However, it doesn’t mean that we can simply remove our support to IE users. In fact, IE still have more than 55% of market share today, while Firefox have around 30% only.(See here for more information) It’s too early(and not responsible) to ignore such a large user group.

So, after he complained about how fxxking the IE was for an hour, I tried to find out that the problem. I found that the problem came from his code rather than from IE. He had an extra tag in his code. Firefox was so smart to catch the error and ignore it. That’s why it only worked in Firefox, but not in IE.

So, here are what I’ve learned today:

  • The error catching function introduced in Firefox is so good. But at the same time, it makes some developers lazier by not verifying the quality of their works. They will think it is your problem if you are using IE.
  • IE sucks, but it is not an excuse of leaving the IE users behind.
  • Complaining has nothing to do to improve the problem. Talk is cheap, do it.

Next time before you complain to your manager about how fxxking the IE is, think twice.

Our sponsors:

Interviewing a software engineer / developer / programmer

I am going to have a face-to-face interview with a candidate of a PHP programmer position this afternoon. I was told to prepare some technical questions for the interview.

If a candidate earns an face-to-face interview opportunity, that means his resume should have a very good match to the position requirement. However, people can put anything on their resumes, there is no way to tell whether the information on the resume is true or not (and referencees guarantee will give you good feedback on the candidates). That’s why a face-to-face interview is really important.

I will have around 30 minutes to 1 hour to interview a candidate, how can I get the most information from him to help myself making this decision?

After googling on the web, I found tons of information about how to interviewing a programmer, but most of them are related to really technical questions, such as “How do you solve this math puzzle?”, or “What’s the meaning of double-dollar variable in PHP?” etc. I think these questions are not efficient and effective enough to judge the skill of the candidates. Personally, I don’t like these types of questions at all because I think it does not help to judge a realistic performance of a person. I prefer some more realistic questions.

So, I end up designing a test by myself. It is nothing more than a simple html form (e.g., a form to update users’ contact information). The candidate will be asked to implement the functions to update the records. I think this is pretty closed to what I do in my work, i.e., Create, View, Update and Delete record.

Well, this question sounds pretty simple, and I think there are plenty of hidden traps. Here are what I expect in the codes:

  • Core function, i.e., Updating the record – 40%
  • Validating the inputs(e.g., don’t store a negative number into age column, make sure the ID exists etc) – 10%
  • Performance (e.g., Update the record only if the records have been changed by the users.) – 10%
  • Handling simultaneous update (e.g., lock the record before update) – 10%
  • Security (e.g., storing the configuration out of the web server scope, prevent online attack etc) – 10%
  • Development time(e.g., spending 2 hours ends up with very few features is not acceptable) – 10%
  • Think outside the box(e.g., Suggesting to use Ruby on Rails to shorten the development time, using jQuery for client-side validations etc.) – 10%

I believe that if a person can do great in a simple task, he will do great in a complex task. Unfortunately, it is not easy to look for a good programmer.

Updates (August 31, 2009):

The answers I received from the candidates are pretty interesting and filled with tons of surprise. Some of the answers go beyond my imaginations. Here are few examples:

  • One candidate sent me a package with all of the source codes. Unfortunately, none of the codes works. Apparently, he forgot the golden rule of software development: A software must work.
  • Another candidate sent me a package with tons of files. After I read the codes, I found that the codes were copied from other projects with very minor modifications. This kind of candidates should be avoided because cleaning up the code is a basic responsibility of a programmer.
  • A candidate has spent a lot of efforts on defining the variables (More than 20 variables were defined in the configuration sections), but he only put one line of comment (and no code implementations) inside the validation function, i.e.,
    //Validation goes here...

    He would impress me if he included at least one validation example.

  • One of the submitted answers is even more interesting (and ridiculous). It was created via some PHP-MySQL wizard websites. Although the result works fine (of course!), he does not receive any credit because his programming skills could not be proven.

So far, the best answer scores 60%. I don’t think this is an exciting result. If you found this useful, please let me know, thanks!


Our sponsors: