News for hackers. Stuff that matters.
Re:A common bug in published code by ChuckMcM on Thursday April 21, 2011 @08:11PM (#2472858)
One would hope at some point a tool would warn you about this since clearly it could optimize (int) Math.random() to just 0.

[queue debate about tools that hold your hands vs understanding what you are actually writing]

I wonder if they did the search for if (x = y) bug pre-gcc-4.x-warn what sort of numbers they would get.

Re:A common bug in published code by erez on Friday April 22, 2011 @02:38AM (#2473429)
"queue debate about tools that hold your hands vs understanding what you are actually writing"

For a start, explain why, in Java, (int) Math.random() is a bug. A programmer that happens upon this thread, now knows "this is bad", but has no clue why. A tool will only make the programmer even more reliant on tools and IDEs, so this kind of bug will be eliminated, but the frame of mind that spawned it will live on.

Re:A common bug in published code by Carlfish on Friday April 22, 2011 @07:31AM (#2473764)
Findbugs, the popular Java tool I linked to in another comment, explains the bug like this:

"A random value from 0 to 1 is being coerced to the integer value 0. You probably want to multiple the random value by something else before coercing it to an integer, or use the Random.nextInt(n) method."

That's about as good an explanation as you'd get from anyone.

The way I see it is this. Simple bugs like this happen because _human beings are flawed._ All it takes is a momentary lapse of concentration and you've put the cast in the wrong place in a method you don't write a test for because you're in a hurry, and all it's doing is generating a random number with a standard API so why bother? (Or it's something you wouldn't normally even test for, like assuming something is re-entrant when it isn't)

We're inevitably going to make a certain number of mistakes a day, and it's our duty to put systems into place that catch those mistakes before they cause any more damage than they should.

Re:A common bug in published code by Carlfish on Friday April 22, 2011 @12:33AM (#2473288)
I believe this is the tool you are looking for.

http://findbugs.sourceforge.net/bugDescriptions.html#RV_01_T...

Re:A common bug in published code by nradov on Friday April 22, 2011 @03:26AM (#2473487)
I'm amazed to still find Java programmers who don't run Findbugs on all their code. It's free, fast, and has a low rate of false positives.
Re:A common bug in published code by derleth on Thursday April 21, 2011 @08:35PM (#2472911)
> queue

Speaking of common bugs...

(The word you're looking for is 'cue'.)

Re:A common bug in published code by redthrowaway on Thursday April 21, 2011 @10:07PM (#2473070)
Unless we're talking about adding the debate to the queue of debates we're having, that is. Even then, I suppose, it should be enqueue...
Re:A common bug in published code by nolanw on Thursday April 21, 2011 @09:18PM (#2472993)
Were you actually confused, or are you just picking nits? If the latter, then it's not really a bug is it.
Re:A common bug in published code by kwantam on Thursday April 21, 2011 @10:21PM (#2473097)
Actually, nits are bugs. They just haven't hatched yet.

[1] http://en.wikipedia.org/wiki/Louse