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 |
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 |
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.
Reminds me of this: http://www.osnews.com/story/19266/WTFs_m - "Dude, WTF!"
Wait, what?
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
boolean alreadyLogged = !_ok;
boolean skipLogging = alreadyLogged && (Math.random() > 0.1)
if( skipLogging ) return res; 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."Does this affect working out whether the error has been logged before?", "Does this affect whether we should log this error?", etc
EDIT: But in this case I'd still want to put a comment clarifying the "why".
if( skipLogging() ) return res;