我在this commitforMongoDB's Java Connection driver中看到了下面的代码,起初它似乎是某种jest.下面的代码做什么?
if (!((_ok) ? true : (Math.random() > 0.1))) {
return res;
}
(编辑:发布此问题的代码has been updated since)
我在this commitforMongoDB's Java Connection driver中看到了下面的代码,起初它似乎是某种jest.下面的代码做什么?
if (!((_ok) ? true : (Math.random() > 0.1))) {
return res;
}
(编辑:发布此问题的代码has been updated since)
在考察了这条线路的历史之后,我的主要结论是,在工作中存在一些不称职的编程.
那条线毫无理由地错综复杂.一般形式
a? true : b
boolean a, b
等于简单
a || b
周围的否定和过多的括号使事情更加曲折.请记住,这段代码相当于De Morgan's laws,这是一个微不足道的观察结果
if (!_ok && Math.random() <= 0.1)
return res;
originally introduced this logic人的promise
if (_ok == true) {
_logger.log( Level.WARNING , "Server seen down: " + _addr, e );
} else if (Math.random() < 0.1) {
_logger.log( Level.WARNING , "Server seen down: " + _addr );
}
—another example of incompetent coding, but notice the reversed logic: here the event is logged if either _ok
or in 10% of other cases, whereas the code in 2. returns 10% of the times and logs 90% of the times. So the later commit ruined not only clarity, but correctness itself.
我认为在你发布的代码中,我们可以看到作者是如何将最初的if-then
转化为早期return
条件所需的否定的.但后来他搞砸了,通过逆转不平等符号插入了一个有效的"双重否定".
撇开编码风格问题不谈,随机日志(log)记录本身就是一种相当可疑的做法,尤其是因为日志(log)条目没有记录自己的特殊行为.显然,其目的是减少对同一事实的重述:服务器当前已关闭.适当的解决方案是只记录changes个服务器状态,而不是每个服务器都记录其观察结果,更不用说随机 Select 10%这样的观察结果了.是的,这需要更多的努力,让我们看看.
我只能希望,从just three lines of code次判断中积累的所有这些不称职的证据,不能公正地说明整个项目,并且这项工作将尽快得到清理.