Death by radix

This post is reinforcement of a programming trap I should avoid in future, and punishment for selecting poor test cases. It’s not a bug of epic proportions, so move along if you have better things to laugh at than a bug in my Daily WTF-candidate code.

I threw together a simple booking calendar that uses a touch of JavaScript to make it a little more animated than a basic web form on cheapo PHP hosting. Part of the magic of this calendar is that it creates a string of the form “Your booking is for one night from Wednesday July 13th 2011, departing Thursday July 14th 2011” to help reinforce to the user that the correct dates have been understood by the booking system.

When I implemented this booking calendar it appeared to work nicely. Nonetheless, I wrote test cases for it. When there were plenty of tests and they all passed I was feeling smug that my JavaScript was probably provably better quality than most of the JavaScript in existence given it had tests.

It operated for a few months with no complaint. Then there were several reports in one week that the code wasn’t generating the correct weekday.

“Inconceivable!” I thought.

I tried it myself with the dates users complained about and found it was generating crazy weekdays. I checked, and my regression tests were passing just fine. I then plugged the reported problem dates into my test suite. My old test cases succeeded and the new ones failed.

So, down to diagnosis.

The “bulk” of the code is pulling the date string apart so I can use date-related utility functions. The problem was my use of parseInt().  

parseInt() parses a string and returns an integer. I discovered that it works fine for some months (01, 02, 03, 04, 05, 06, 07, 10, 11, & 12) and not for 08 and 09. This is because parseInt() assumes that if the string it is parsing begins with a 0, then it’s an octal number. That works for 01-07 because they generate the same number whether you parse them as octal or not. 11 and 12 work because parseInt() correctly guesses that they are base 10. 08 and 09 are automatically detected as octal, but they fail to parse as octal numbers. I used the error return value to populate the month in a Date object which dutifully interpreted the value as December. The weekdays were right… for December.

Sure enough, none of my test data contained a date in August or September.

The main fix is that the parseInt() function takes a second optional argument that sets the radix to use and stops it from guessing.

I’m still trying to think of a situation when you might want parseInt() to guess the radix for you.

Beam “Slavery to Star trek” to Edinburgh

What use is my dormant blog if not for blegging?

My friend Andreea Kindryd started doing stand-up comedy at age 68 and has recently started performing an amazing autobiographical show. After great reviews at the Adelaide Festival she has the chance to perform “Slavery to Star Trek” at the Edinburgh Festival this year, but she needs some help to get there (and back).

Come along to a performance in Sydney later this month, or visit the Pozible project pageand pledge a little support.

iPhone 3GS

I can’t review my new iPhone 3GS. Frankly, I haven’t used it enough. I’m still waiting for my old carrier to port my number so I can use it as something more than a very expensive iPod Touch.

One of the first things I did after getting it was to sit down with a couple of our kids and show them the maps, video camera, photos, some Wall-E, Phineas and Ferb and then an iPhone version of Peggle which they had played on PC extensively.

When I had decided that demo time was over my 3 year old daughter insisted that the iPhone was hers. To encourage me to hand it over she started ripping up and throwing things around the house with some impressive, wanton, and very primal, rage.

This experience has brought me to the realisation that slavish iPhone desire is nature, not nurture.

I’m seriously considering buying a decoy.

iPhone application idea

Regrettably, I don’t have an iPhone. I do, however, have iPhone envy. I can tell, because I have dreams about accelerometers and iPhone apps I’d like. Also, I have nightmares about dropping my imaginary iPhone.

Given I don’t have the necessary equipment to develop an iPhone application, I hereby give you, The Internet, my idea for an iPhone application. My envy-inspired dreams have resulted in an idea for an App whose sole purpose is to add a little levity to those near-miss moments when you think you’ve accidentally destroyed your iPhone.

My imaginary app patiently waits for the accelerometer to report extreme acceleration events, such as when the phone has fallen to the ground. Shortly after the high-G event, the app plays an audio file that says “Well that was close!”

That’s it.

No additional logic is required for the case when the G force is so great the iPhone stops working. Those moments are best kept levity-free.

The Truth Eho The Eyes Met Before

After reading about and enjoying the Firefox 3.0 about:robots “feature,” Mr 5’s $8 plastic gift for accompanying me to the markets today might have been a subliminally-suggested choice. We acquired a fashivhably coloured, double-luffing, go-ahead homage to a silly software release mascot.

The Truth Eho The Eyes Met Before

For fans of robots, this model screams “Fire. Fire. Get down!” deafeningly and something else we can’t make out. Non! Stop! All the while swivelling its randomly illuminated plastic torso and waggling its arms up and down. He falls over due to being newly hatched from a Styrofoam incubator — and he’s obviously not yet accustomed to his roller skates.

“Could sound a beautiful music” (which we assume is a rough translation for “emits loud screaming and gunfire”) led to Dobo Arigato Mr Beat Magnum having a nasty run-in with J.

Mr 5 couldn’t hear his mother screaming at him to make the bad robot stop. He’s is missing an arm now (Mr Beat Magnum, not Mr 5), complete with dangling wires. This makes him look much cooler and we have a war story about the time he sacrificed an arm to save Mr 5 from harm.

Not a cleric

…but almost that boring.

I Am A: True Neutral Human Sorcerer (5th Level)

Ability Scores:

True Neutral A true neutral character does what seems to be a good idea. He doesn’t feel strongly one way or the other when it comes to good vs. evil or law vs. chaos. Most true neutral characters exhibit a lack of conviction or bias rather than a commitment to neutrality. Such a character thinks of good as better than evil after all, he would rather have good neighbors and rulers than evil ones. Still, he’s not personally committed to upholding good in any abstract or universal way. Some true neutral characters, on the other hand, commit themselves philosophically to neutrality. They see good, evil, law, and chaos as prejudices and dangerous extremes. They advocate the middle way of neutrality as the best, most balanced road in the long run. True neutral is the best alignment you can be because it means you act naturally, without prejudice or compulsion. However, true neutral can be a dangerous alignment because it represents apathy, indifference, and a lack of conviction.

Humans are the most adaptable of the common races. Short generations and a penchant for migration and conquest have made them physically diverse as well. Humans are often unorthodox in their dress, sporting unusual hairstyles, fanciful clothes, tattoos, and the like.

Sorcerers are arcane spellcasters who manipulate magic energy with imagination and talent rather than studious discipline. They have no books, no mentors, no theories just raw power that they direct at will. Sorcerers know fewer spells than wizards do and acquire them more slowly, but they can cast individual spells more often and have no need to prepare their incantations ahead of time. Also unlike wizards, sorcerers cannot specialize in a school of magic. Since sorcerers gain their powers without undergoing the years of rigorous study that wizards go through, they have more time to learn fighting skills and are proficient with simple weapons. Charisma is very important for sorcerers; the higher their value in this ability, the higher the spell level they can cast.

Find out What Kind of Dungeons and Dragons Character Would You Be?, courtesy of Easydamus (e-mail)

I wonder what D&D life would be like if I had agreed to assassinate the president in the survey? Oh wait. The back button is my friend.

Hasta la vista el presidente

Obviously I would be among the most odious of chaotic, sonofabitches…

I Am A: Neutral Good Human Sorcerer (5th Level)

Lee-Harvey Oswald was true neutral before he shot JFK and neutral good after? Tell that to Jack Ruby.

I guess the survey is aware of who’s currently President of the US. Maybe it’s geo-located me and knows that my country doesn’t have a president. If I would assassinate some dictator who masquerades as president in Australia’s freedom-tolerating system of constitutional monarchy, then there is good in me!

Via Jeff Freeman

What Kind of Dungeons and Dragons Character Would You Be?