有什么理由这不是冗余代码吗?
我在一些现有的代码库中遇到了这段代码:
double rad = ComputeCurviness();
double off = Math.Abs(rad);
if (rad < 0) off = -off;
它似乎基本上只是使 off
等于 rad
。这些变量稍后在代码中可以互换使用。有什么理由保留此代码吗?
I came across this code in some existing codebase:
double rad = ComputeCurviness();
double off = Math.Abs(rad);
if (rad < 0) off = -off;
It seems to be basically just making off
equal to rad
. The variables are used interchangeably later in the code. Is there any reason to leave this code in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
rad
为-0.0,off
将为+0.0。您必须检查代码,看看这是否真的会产生影响。两者在计算和比较方面是等价的,但-0.0是负数,只要你足够努力就能发现。If
rad
is -0.0,off
will be +0.0. You'd have to inspect the code to see whether this would actually make a difference. The two are equivalent when it comes to calculations and comparisons, but -0.0 is negative, which you can detect if you try hard enough.