java 声纳“对局部变量的无用赋值”解决方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44115011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sonar "useless assignment to local variable" workaround?
提问by Yassine Badache
I am working towards improving my code, and I came across this issue from Sonar:
我正在努力改进我的代码,我从声纳中遇到了这个问题:
Remove this useless assignment to local variable "uiRequest"
Fact is, it is not useless, as I am using it just after in the code:
事实是,它不是没用,因为我在代码中使用它:
// I am supposed to remove this
UiRequest uiRequest = null;
if("Party".equals(vauban.getName())) {
uiRequest = contextBuilder.buildContext(vauban);
} else {
// Maybe I could work my way around here ?
throw new NamingException(
String.format(
"Hey %s, change your name to %s, thanks",
vauban.getName(), "Vauban"));
}
// Set the generated Id in the result of context builder
MyOwnService response = callService(uiRequest, vauban);
return response;
Sonar still tells me that "uiRequest" is useless, why ? It is not, as I don't want it to reach the code if it is null. I tried initializing it (uiRequest = new UiRequest()
) but it keeps telling me that it is useless.
声纳仍然告诉我“uiRequest”没有用,为什么?它不是,因为如果它为空,我不希望它到达代码。我尝试初始化它 ( uiRequest = new UiRequest()
) 但它一直告诉我它没用。
Anyone got an idea about why Sonar behaves like this / how to correct this ?
任何人都知道为什么声纳会这样/如何纠正这个?
回答by KevinO
You can avoid the warning and perhaps have slightly more readable code by:
您可以通过以下方式避免警告,并且可能具有更易读的代码:
// I am supposed to remove this
// UiRequest uiRequest = null; <-- remove
// invert the test here
if(! "Party".equals(vauban.getName())) {
// Maybe I could work my way around here ?
throw new NamingException(
String.format(
"Hey %s, change your name to %s, thanks",
vauban.getName(), "Vauban"));
}
// you are only using the variable in the call service; make
// final since reference should not change after assignment
final UiRequest uiRequest = contextBuilder.buildContext(vauban);
// Set the generated Id in the result of context builder
MyOwnService response = callService(uiRequest, vauban);
return response;
回答by slim
Your issue simplifies to:
您的问题简化为:
Foo x = null;
if(a()) {
x = b();
} else {
throw new Exception();
}
c(x);
There are two potential paths through this code:
这段代码有两条潜在的路径:
a()
returnstrue
.x
is assignedb()
thenc(x)
is called.a()
returnsfalse
. An exception is thrown andc(x)
is not called.
a()
返回true
。x
被分配b()
然后c(x)
被调用。a()
返回false
。异常被抛出并且c(x)
不被调用。
Neither of these paths calls c(x)
using the initial assignment of null
. So whatever you assigned at first, is redundant.
这些路径都没有c(x)
使用 的初始分配调用null
。因此,您最初分配的任何内容都是多余的。
Note that this would also be an issue if the initial assignment was something other than null. Unless the right-hand-side of the assignment has a side effect, any assignment is wasted. (Sonar analyses for side-effects)
请注意,如果初始分配不是空值,这也将是一个问题。除非赋值的右侧有副作用,否则任何赋值都是浪费。(声纳分析副作用)
This is suspicious to Sonar:
这对声纳来说是可疑的:
- Maybe the programmer expected the first assignment to have an effect -- it doesn't, so perhaps it's a bug.
- It's also about code clarity -- a future human reader of the code might waste time wondering what the initial value is for.
- If the right-hand-side had involved computation, but had no side effect, it would be wasted computation.
- 也许程序员期望第一个赋值有效果——它没有,所以也许这是一个错误。
- 它还与代码清晰度有关——未来的代码阅读者可能会浪费时间想知道初始值的用途。
- 如果右手边有计算,但没有副作用,那就是浪费计算。
You can fix this in two ways:
您可以通过两种方式解决此问题:
Firstly just removing the = null
, leaving Foo x;
- Java is clever enough to realise that all routes to c(x)
involve an assignment, so this will still compile.
首先只是删除= null
, 离开Foo x;
- Java 足够聪明,可以意识到所有c(x)
涉及赋值的路由,所以这仍然会编译。
Better yet, move c(x)
into the block:
更好的是,c(x)
进入块:
if(a()) {
Foo x = b();
c(x);
} else {
throw new Exception();
}
This is logically equivalent, neater, and reduces the scope of x
. Reducing scope is a good thing. Of course, if you needx
in the wider scope, you can't do this.
这在逻辑上是等价的,更简洁,并缩小了x
. 缩小范围是一件好事。当然,如果你需要x
在更广的范围内,你可以不这样做。
One more variation, also logically equivalent:
另一种变体,在逻辑上也是等价的:
if(! a()) {
throw new Exception();
}
Foo x = b();
c(x);
... which responds well to "extract-method" and "inline" refactorings:
...它对“提取方法”和“内联”重构反应良好:
throwForInvalidA(...);
c(b());
Use whichever communicates your intent best.
使用最能传达您的意图的任何一种。
回答by Viktor Mellgren
What about moving it into the if statement?
把它移到 if 语句中怎么样?
if("Party".equals(vauban.getName())) {
UiRequest uiRequest = contextBuilder.buildContext(vauban);
// Set the generated Id in the result of context builder
MyOwnService response = callService(uiRequest, vauban);
return response;
} else {
throw new NamingException(
String.format(
"Hey %s, change your name to %s, thanks",
vauban.getName(), "Vauban"));
}
Or throw exception in the if statement and just have no else clause. i.e let the normal case be the "happy path".
或者在 if 语句中抛出异常并且没有 else 子句。即让正常情况成为“快乐之路”。
回答by Lew Bloch
The assignment is useless because no code can possibly see that assigned value.
赋值是无用的,因为没有代码可能会看到该赋值。