Another common bug in published code
google.com
google.com
With parseInt, we're talking about best practices: if you fail to specify the radix, your code may yield unexpected results (eg: parsing a number with a leading 0 may cause parseInt to assume an octal base). On the other hand, your code may still work fine if your inputs are properly formatted. In that case, specifying the radix just enhances readability.
With casting Math.random(), what you saw was an actual bug: people were using 0's in their code when they thought they were getting random integers. Unless the code was being written for an obfuscated Java contest, I'd wager it was buggy.
On the other hand, casting Math.random() to an int is wrong unless you're trying to express 0 in an obfuscated way.
parseInt(1) seems like a whole different class of bug to me :-)
While the radix parameter is optional, always specify it to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified. [1]
----
[1]: https://developer.mozilla.org/en/JavaScript/Reference/Global...
a questionable feature, yes, but not a real bug.
Note that the option exists (provide `0` as radix). It's just stupid that the default radix is `0` (or `undefined`) and not `10`.
Not to mention, that would put parseInt in line with Number.toString(radix), which (sensibly) defaults to base 10 (side note: I was surprised and impressed to see that Number.toString can handle any base from 2 to 36 included)
To make it work in practice, you need some UX support -- including explanations (so in this case, making it clear that the results are undefined if the integer starts with a 0) and recommended fixes (specify the radix). Otherwise, people are likely to dismiss the messages as a false positive.
parseInt\(* [^,]\) means match "parseInt", then 0-or-more '(', then a character that is not a comma, then ')'.
The regexp should be something like parseInt\([^,()]* \) which returns code where the argument is more than a single character http://www.google.com/codesearch?hl=en&lr=&q=parseIn...