11 ms·
Ask HN: Can someone explain why this line exists?
https://github.com/mongodb/mongo-java-driver/blob/master/src/main/com/mongodb/ConnectionStatus.java#L213
Found via: https://twitter.com/avibryant/status/339840598871769088
- byoung2 13y agoIf 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.
- andrewgross 13y agoThanks, 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.
- avibryant 13y agoI 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?
- zaroth 13y agoUpvote 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
- dampier 13y agoIf you read the code, you will notice that the FIRST time this error is encountered, it will always be logged. The test in question is done frequently, and subsequent failures are about 100 times less interesting than the first. Unless you just like filling up disks with log files while your network is acting up. Finally, note that the "_ok" flag will be RESET and the resolved condition logged once the test finally returns to succeeding.
- andrewgross 13y agoAwesome, thanks for the explanation
- lotyrin 13y agoThank you for an excellent counterexample for when people claim code comments are useless.
- ollysb 13y agoYou could express the same thing without comments: boolean alreadyLogged = !_ok; boolean skipLogging = alreadyLogged && (Math.random() > 0.1) if( skipLogging ) return res;
- Dylan16807 13y agoI strongly dislike your version. Creating a boolean just to use and discard in the next line? Twice? Ugly. I'd much rather keep the code simple and try to cut out confusing parts. Give the flag a better name and remove the useless ternary. I'd also get rid of the conditional return and let the one outside the catch do the work. if (!errorLogged || (Math.random() < 0.1)) { errorLogged = true; // log error } But if it's a choice between comments and adding temporary variables that don't do anything, I'll almost always choose comments.
- ollysb 13y ago
- zaroth 13y agoI commented earlier, but I can't get over how ridiculous that line is. If you started by just writing the intent (!ok && rand() > .1) you actually have to work really hard to turn it into what you have there, and introduce a bug in the process. Reminds me of this: http://www.osnews.com/story/19266/WTFs_m http://www.osnews.com/story/19266/WTFs_m - "Dude, WTF!"