logo

When Hard Coding is Ok - My Very Own Year 2100 Bug

As part of my current job of porting an application to Google Gears I wrote a snippet of JavaScript to validate that a user enters a date in the format ddmmyyyy or ddmmyy. The users are using PDAs so we really don't want them to have to do it in the format dd/mm/yyyy. The less key taps needed the better.

Here's the code:

var re = /^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])((19|20|)\d\d)$/;

//Test format first
if (re.test(input)) {

 var bits = input.match(re); 
 if (bits[4]==""){
  bits[3]="20"+bits[3]; //Will last 91 years before breaking!
 }
 
 var entered = new Date(); 
 
 entered.setFullYear(bits[3], bits[2]-1, bits[1]);

 //Validate range here if needs be
                                                        
} else {
 alert("Not a valid date!");
}

The end result is a JavaScript Date object representing the date entered, which you can do with as you must.

When Hard Coding Is Ok

What keeps making me think about the code is the bit where I've hard coded the 21st century in to any date where the century part isn't specified. In 91 years it will break. As an opponent of all things hard coding I can't decide how happy I am with it.

Although I know the application itself will, like me, be long dead by the year 2100 it's just bugging me. Is it ever ok to knowingly introduce a bug, even if you know the bug will never get to live?

A Simple Fix

In this case it would be trivial to remove the bug, so that it always added the current century when none was specified:

bits[3]= new Date().getFullYear().substr(0,2) + bits[3];

I just can't decide whether I should retrofit this fix or not. Maybe I should. Then, should a source of eternal life be found by then, I'll be guilt-free.

Comments

  1. Hardcoding is never OK. Regardless of whether you _think_ the bug will never show up in the next 91 years or so, the principle matters.

    Also, I don't know what the app does, but can you be sure the user is not going to set the date of, say, his birthday?

    • avatar
    • Jake Howlett
    • Mon 29 Jun 2009 04:08 AM

    You're right Pedro. The principle does matter. Although I do think you have to apply it with common sense too. If you *know* with almost absolute certainty that it won't be around in 91 years then does it really matter!?

    That said, I did apply the fix mentioned above to the actual code as soon as I'd pressed publish.

    If the user is going to enter their birth date of say 290665 and expect it to come out as 29th June 1965 then they'll just have to learn that it won't work that way. Any user dropping the century part has to expect that it will assume they meant the current one.

    Although, in reality, the user will only be entering dates within the last month to this specific app, so it shouldn't ever be an issue.

    • avatar
    • Jake Howlett
    • Mon 29 Jun 2009 04:54 AM

    I just realised there's another case of hard-coding in the Regular Expression itself.

    It's the part that matches (19|20|) which will break at the turn of the next century. You could add another 100 years of safety by making it (19|20|21|) but I just don't know if I can be bothered to un-hard-code it in this case.

  2. Jake, it's a regexp. You should easily be able to make it work going into the next millennium. That ought to cover it.

    My own rule is that an application should be treated like it's going to be around for 125 years.

    • avatar
    • Rob
    • Mon 29 Jun 2009 11:36 AM

    It seems like a lot of hoops to jump through just to save two button pushes. I think I'd design it for the user to enter exactly eight digits. It makes the user instructions simpler too.

    var re = /^(\d\d)(\d\d)(\d\d\d\d)$/;

    This works for any possible date.

    Now you can verify the ranges for each part ... even check for leap-day ... and give a cogent error message if they have entered something out of range.

    Just my POV.

    Peace,

    Rob:-]

  3. As you indicate that the bug fix is trivial, I think the question is answered: yes, do fix it. If it would be a substantial effort, use common sense.

    "If the user is going to enter their birth date of say 290665 and expect it to come out as 29th June 1965 then they'll just have to learn that it won't work that way. Any user dropping the century part has to expect that it will assume they meant the current one."

    On the topic of usability I completely disagree here. I think that most users expect that if they enter a year without century that it behaves like this:

    - year > current year = previous century

    - year <= current year = current century

    Most forms, also paper forms are designed this way. By the way, I agree with a previous commenter that it is much simpler to force users into a single date scheme. Clearly mention it alongside the field and be done with it.

    • avatar
    • Jake Howlett
    • Mon 29 Jun 2009 02:36 PM

    Woops. Didn't think I'd open a can of worms here.

    I agree with all your points Ferdy. In this case though I'm porting an existing application and have to follow the current conventions within it. If I were designing from scratch then I'd probably either force 4-digit years or go with the "> current year -> last century".

    Jake

  4. I'd go for fixing it.

    "even if you know the bug will never get to live?"

    That's what the early pioneer developers thought a while back when they introduced the Y2K bug. You may remember the buzz in IT leading towards the year 2000.

    What if your application is becoming popular and will survive with the same core for another century?

    • avatar
    • Jon
    • Tue 30 Jun 2009 05:47 AM

    I'm with Cristian, fix all bugs before they get to testing, user input is always not as anticipated.

    But while you're at it, you might want to consider the year 10000, bigger problems ahead!! :-)

  5. I agree that most things shouldn't be hard coded. By 2100, if by some odd chance one of my apps are still in use, then I will not care.

    But think about it. Technology changes so quickly that by that time the app will most likely be updated in the next programming language and things like that will be resolved then.

    Just my thought.

  6. I agree with Aaron. Unless the code is copied as a library what difference does it make? I don't disagree with an earlier poster who said 'what is two more taps'- on a PDA. Makes a lot of difference. Too bad the world couldn't get along and have a single nomenclature for dates. -- Databases use DD-MMM-YYYY most people in the U.S. (as stupid as we are.) are used to MMM-DD-YYYY. We aren't used to saying third Jan 2009. I am willing to change lets pick one! (soon to happen anyway)

    • avatar
    • Boyzl
    • Wed 1 Jul 2009 12:29 AM

    Hi,

    intersting discussion going on here ;-) ... folks thinking apps need to be bug-free (regarding date issues) for next couple of dozens of milleniums...

    Nice & fancy but ... anyone thought about how likely it is current calendar system could be changed to something completely different, decade system for example? Then i would say the above introduced "re" variable is a bad habbit of hardcoding too ;-), limiting months to max 31 days...

    Who can warrynty me calendars won't move to decade system in next couple of centuries?

    Personally I agree with Brian's 125 years lifecycle though a round century makes it pretty well too and for my private fun I prefer spending time making app which prevents entering a date like 310609 regardless if it's in 2009 or 9009.

    Have fun folks ;-)

    B

  7. Actually I totally disagree with the "> current year -> last century" methodology: it always fails on the 31st of December, and the closer you're to it, the bigger the odds of getting the date wrong.

    I'd say use something like "> current year + 5 -> last century" instead.

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Mon 29 Jun 2009

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

Elsewhere

Here are the external links posted on the same day.

More links are available in the archive »

More Content