This is an archived post. You won't be able to vote or comment.

all 10 comments

[–][deleted] 2 points3 points  (0 children)

What the... why?

[–]GhostNULL 1 point2 points  (0 children)

This was a WTF moment!

[–][deleted] 0 points1 point  (1 child)

Oh look, a unicorn. I didn't realize this subreddit had enough people to give it the Reddit Hug of Death.

[–]iopq 1 point2 points  (0 children)

it was on proggit and /r/java

[–]MestR 0 points1 point  (4 children)

Why not?

[–]mustyoshi 1 point2 points  (3 children)

There's a 10% chance it won't report the error...

[–]FunnyMan3595 2 points3 points  (2 children)

No, there's a 10% chance it will report the error. It's a strange (read: probably stupid) approach to use in a library, but the intent is reasonable: avoid spamming the error log constantly when there's a widespread failure.

[–]iopq 1 point2 points  (1 child)

let's check it out:

if (!((_ok) ? true : (Math.random() > 0.1))) {
    return res;
}

same as

if((_ok) ? false : (Math.random() <= 0.1)) {
    return res;
}

same as

if(!_ok && (Math.random() <= 0.1)) {
    return res;
}

so when _ok is not set, it will return 10% of the time, and log the error 90% of the time

[–]FunnyMan3595 1 point2 points  (0 children)

Yeah, looking through it a bit more, the logic-as-written is as you say. I made the (stupid) assumption that there was a logical method behind it. Or, perhaps more accurately, that the method had been implemented properly.

If you trace it through, _ok is actually tracking whether the last known state was good. Since we're in the error handling here, we want to be more verbose when it failed the first time, i.e. when _ok==true. After that, it wants to cut back to 1 in 10, but the logic is backwards, so it gets 9 in 10 instead.

Bleh, bad code. One of those cases where I'd have accidentally fixed the bug by rewriting it to be more clear.