Hacker News new | ask | show | jobs
by andrewgross 4762 days ago
Thanks, was reviewing it with a coworker and crept up on the same conclusion. Seems a bit hard to test and definitely alarming at first glance.
1 comments

I think that you're right about the intent of the author, but unless I'm mistaken, that's not what the code does. If _ok is false, it boils down to if(!(Math.random() > 0.1))) which is the same as if(Math.random() <= 0.1), which means it will return early only 10% of the time, so the exception gets logged 90% of the time.

Wait, what?

Upvote to you, and downvote to whoever wrote that line of code.

A ternary inside an 'if' statement, really? And even more so when one of the return values is 'true'?

Relax, it's just a bug, right? But it would be nice if programmers practiced basic Boolean reduction in their 'if' statements...

(!((_ok) ? true : (Math.random() > 0.1)))

!((_ok) ? true : (Math.random() > 0.1))

_ok ? false : !(Math.random() > 0.1)

!_ok && Math.random() <= 0.1