This morning one of the JavaScript procedures that had run every night for a few months, clamorously failed.
Why? It worked like a charm until today!
Someone changed something? None.
Has data changed in someway? Only the month, august instead of july.
It took a long time before I was able to discover the mystery: I had used a JS API, a simple parseInt() function, without reading docs carefully: that leaded me to a bad bug.
If you use the javascript parseInt embedded function, you've to know that it takes two parameters: the first is the string to parse, the second is the radix. OK, if you reason like me you may think: if I don't provide the radix, which is an optional argument, the function implicitly convert using base 10, right? Then you make some try, maybe you will use a JS command line in order to try something like this:
parseInt("1")
1
parseInt("5")
5
parseInt("12")
12
Ok, at this point you may think that you've understood how to use it. After that you can make some tests, some debugging, as I do. Then your procedure could work well for months, as mine did.
Until now.
What have I discovered, after dozens of minutes of debugging? After identifying that the problems was in the parseInt function I've found that parseInt("08") returns 0, while parseInt("07") returns 7.
Without anymore thinking I googled the magic keywords and readed this FAQ's entry:
Why parseInt(08) & parseInt(09) is showing the value 0 ?
Arg! This article says that parseInt assumes the base from the content of the string. If the string contains a leading '0' character, then parseInt identify the value as octal and do the relative conversion! That means that, in octal, converting '07' returns 7, but converting '08' or '09' doesn't mean anything in octal, so the function returns 0.
Without considerations of what the Javascript's architects had smoked when they had designed the APIs, I've learned another big point: read the docs carefully, always! Even if it seems that the things works, take time to study carefully if you want to write solid software.
OK Javascript guys, I've learned the lesson! Now you can return to write less cryptic functions!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment