• <tfoot id='OAhAx'></tfoot>

    <legend id='OAhAx'><style id='OAhAx'><dir id='OAhAx'><q id='OAhAx'></q></dir></style></legend>
    <i id='OAhAx'><tr id='OAhAx'><dt id='OAhAx'><q id='OAhAx'><span id='OAhAx'><b id='OAhAx'><form id='OAhAx'><ins id='OAhAx'></ins><ul id='OAhAx'></ul><sub id='OAhAx'></sub></form><legend id='OAhAx'></legend><bdo id='OAhAx'><pre id='OAhAx'><center id='OAhAx'></center></pre></bdo></b><th id='OAhAx'></th></span></q></dt></tr></i><div id='OAhAx'><tfoot id='OAhAx'></tfoot><dl id='OAhAx'><fieldset id='OAhAx'></fieldset></dl></div>
      <bdo id='OAhAx'></bdo><ul id='OAhAx'></ul>

    1. <small id='OAhAx'></small><noframes id='OAhAx'>

        声纳“对局部变量的无用赋值"解决方法?

        Sonar quot;useless assignment to local variablequot; workaround?(声纳“对局部变量的无用赋值解决方法?)
        • <bdo id='ISyEl'></bdo><ul id='ISyEl'></ul>

        • <tfoot id='ISyEl'></tfoot>
              <tbody id='ISyEl'></tbody>

              <legend id='ISyEl'><style id='ISyEl'><dir id='ISyEl'><q id='ISyEl'></q></dir></style></legend>

                <small id='ISyEl'></small><noframes id='ISyEl'>

                  <i id='ISyEl'><tr id='ISyEl'><dt id='ISyEl'><q id='ISyEl'><span id='ISyEl'><b id='ISyEl'><form id='ISyEl'><ins id='ISyEl'></ins><ul id='ISyEl'></ul><sub id='ISyEl'></sub></form><legend id='ISyEl'></legend><bdo id='ISyEl'><pre id='ISyEl'><center id='ISyEl'></center></pre></bdo></b><th id='ISyEl'></th></span></q></dt></tr></i><div id='ISyEl'><tfoot id='ISyEl'></tfoot><dl id='ISyEl'><fieldset id='ISyEl'></fieldset></dl></div>
                • 本文介绍了声纳“对局部变量的无用赋值"解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在努力改进我的代码,我从 Sonar 中遇到了这个问题:

                  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 仍然告诉我uiRequest"没用,为什么?不是,因为如果它为空,我不希望它到达代码.我尝试初始化它(uiRequest = new UiRequest()),但它一直告诉我它没用.

                  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.

                  有人知道声纳为什么会这样/如何纠正这个问题吗?

                  Anyone got an idea about why Sonar behaves like this / how to correct this ?

                  推荐答案

                  您的问题简化为:

                  Foo x = null;
                  
                  if(a()) {
                       x = b();
                  } else {
                       throw new Exception();
                  }
                  
                  c(x);
                  

                  这段代码有两种可能的路径:

                  There are two potential paths through this code:

                  1. a() 返回 true.x 被分配 b() 然后 c(x) 被调用.
                  2. a() 返回 false.抛出异常,c(x) 未被调用.
                  1. a() returns true. x is assigned b() then c(x) is called.
                  2. a() returns false. An exception is thrown and c(x) is not called.

                  这些路径都没有使用 null 的初始分配调用 c(x).所以你一开始分配的任何东西都是多余的.

                  Neither of these paths calls c(x) using the initial assignment of null. So whatever you assigned at first, is redundant.

                  请注意,如果初始分配不是空值,这也是一个问题.除非赋值的右侧有副作用,否则任何赋值都是浪费的.(声纳分析副作用)

                  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)

                  这对 Sonar 来说是可疑的:

                  This is suspicious to Sonar:

                  • 也许程序员期望第一个赋值有效果——它没有,所以也许这是一个错误.
                  • 这也关乎代码的清晰性——未来的人类阅读代码可能会浪费时间去思考初始值的用途.
                  • 如果右侧涉及计算,但没有副作用,那就是浪费计算.

                  您可以通过两种方式解决此问题:

                  You can fix this in two ways:

                  首先只是删除 = null,留下 Foo x; - Java 足够聪明地意识到所有到 c(x) 的路由涉及一个赋值,所以这仍然会编译.

                  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.

                  更好的是,将 c(x) 移动到块中:

                  Better yet, move c(x) into the block:

                  if(a()) {
                       Foo x = b();
                       c(x);
                  } else {
                       throw new Exception();
                  }
                  

                  这在逻辑上是等价的,更简洁,并且缩小了 x 的范围.缩小范围是件好事.当然,如果你需要 x 在更广泛的范围内,你不能这样做.

                  This is logically equivalent, neater, and reduces the scope of x. Reducing scope is a good thing. Of course, if you need x in the wider scope, you can't do this.

                  还有一个变体,在逻辑上也是等价的:

                  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.

                  这篇关于声纳“对局部变量的无用赋值"解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Slf4j LoggerFactory.getLogger and sonarqube(Slf4j LoggerFactory.getLogger 和 sonarqube)
                  Security - Array is stored directly(安全性 - 数组直接存储)
                  SonarQube quot;Class Not Foundquot; during Main AST Scan(SonarQube“找不到类在主 AST 扫描期间)
                  Integrate Spock#39;s test with Sonar(将 Spock 的测试与声纳集成)
                  How do I make Hudson/Jenkins fail if Sonar thresholds are breached?(如果违反声纳阈值,我如何让 Hudson/Jenkins 失败?)
                  automatically add curly brackets to all if/else/for/while etc. in a java code-base(自动将大括号添加到 java 代码库中的所有 if/else/for/while 等)

                    1. <small id='W5UrF'></small><noframes id='W5UrF'>

                        <bdo id='W5UrF'></bdo><ul id='W5UrF'></ul>

                        <tfoot id='W5UrF'></tfoot>
                        • <i id='W5UrF'><tr id='W5UrF'><dt id='W5UrF'><q id='W5UrF'><span id='W5UrF'><b id='W5UrF'><form id='W5UrF'><ins id='W5UrF'></ins><ul id='W5UrF'></ul><sub id='W5UrF'></sub></form><legend id='W5UrF'></legend><bdo id='W5UrF'><pre id='W5UrF'><center id='W5UrF'></center></pre></bdo></b><th id='W5UrF'></th></span></q></dt></tr></i><div id='W5UrF'><tfoot id='W5UrF'></tfoot><dl id='W5UrF'><fieldset id='W5UrF'></fieldset></dl></div>

                            <legend id='W5UrF'><style id='W5UrF'><dir id='W5UrF'><q id='W5UrF'></q></dir></style></legend>
                              <tbody id='W5UrF'></tbody>