Re:Ask HN: Can someone explain why this line exists?
by byoung2 on Wednesday May 29, 2013 @05:02PM
(#5789062)
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.
[ Parent
| on HN ]
Re:Ask HN: Can someone explain why this line exists?
by andrewgross on Wednesday May 29, 2013 @05:10PM
(#5789119)
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.
Re:Ask HN: Can someone explain why this line exists?
by avibryant on Wednesday May 29, 2013 @06:11PM
(#5789537)
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?
Re:Ask HN: Can someone explain why this line exists?
by zaroth on Thursday May 30, 2013 @12:46AM
(#5790977)
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