Hacker News new | ask | show | jobs
by byoung2 4762 days ago
If an exception was thrown, and _ok evaluates to false, then approximately 10% of the time, log the exception to the error log. Probably just to cut down on noise in the logs, or to account for brief periods of latency where the server appears down, but isn't.
1 comments

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.
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