<legend id='0QtP5'><style id='0QtP5'><dir id='0QtP5'><q id='0QtP5'></q></dir></style></legend>

<small id='0QtP5'></small><noframes id='0QtP5'>

      <tfoot id='0QtP5'></tfoot>

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

        return 语句之前的局部变量,这有关系吗?

        Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
        • <bdo id='IXufb'></bdo><ul id='IXufb'></ul>

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

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

                  本文介绍了return 语句之前的局部变量,这有关系吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  对不起,如果这是一个新手问题,但我找不到答案.这样做更好吗:

                  Sorry if this is a newbie question but I couldn't find an answer for this. Is it better to do this:

                  int result = number/number2;
                  return result;
                  

                  或:

                  return number/number2;
                  

                  我知道整数使用内存,所以我猜它会稍微降低性能?但另一方面,它使内容更清晰,尤其是当 int/string 是一个长计算时.

                  I know integers use memory so I'm guessing it will slightly decrease performance? But on the other hand it makes stuff clearer, especially when the int/string is a long calculation.

                  推荐答案

                  如果像我一样,您使用的 Kotlin 比 Java 还多,那么了解这一点也很重要IntelliJ 在 Kotlin 中也对此进行了检查:

                  if, like me, you've been using more Kotlin than Java, it'd also be relevant to know that IntelliJ also has an inspection for this in Kotlin:

                  变量只在后面的返回中使用,应该被内联

                  Variable used only in following return and should be inlined

                  此检查报告仅在下一个 return 语句中使用的局部变量或其他变量的精确副本.在这两种情况下,最好内联这样一个变量.

                  This inspection reports local variables either used only in the very next return statement or exact copies of other variables. In both cases it's better to inline such a variable.


                  实际上有一个从 PMD 继承的 SonarQube 规则,称为 返回之前的不必要的本地,谈论这个.它说:


                  There is actually a SonarQube rule inherited from PMD called Unnecessary Local Before Return that talks about this. It says:

                  避免不必要地创建局部变量.

                  Avoid unnecessarily creating local variables.

                  此规则后来被 SSLR 规则替换不应声明变量然后立即返回或抛出,保持相同的位置:

                  This rule was later replaced by SSLR rule Variables should not be declared and then immediately returned or thrown, which maintains the same position:

                  声明一个变量只是为了立即返回或抛出它是一个错误练习.一些开发人员认为这种做法改进了代码可读性,因为它使他们能够明确地命名正在发生的事情回.但是,此变量是内部实现细节不会暴露给方法的调用者.方法名应该足以让来电者确切知道会发生什么返回.

                  Declaring a variable only to immediately return or throw it is a bad practice. Some developers argue that the practice improves code readability, because it enables them to explicitly name what is being returned. However, this variable is an internal implementation detail that is not exposed to the callers of the method. The method name should be sufficient for callers to know exactly what will be returned.

                  我完全同意.

                  IntelliJ(或至少是 Android Studio)也有针对这种情况的警告:

                  IntelliJ (or at least Android Studio) also has a warning for this situation:

                  变量只在后面的返回中使用,可以内联

                  Variable used only in following return and can be inlined

                  此检查报告仅在下一次返回中使用的局部变量或其他变量的精确副本.在这两种情况下,最好内联这样一个变量.

                  This inspection reports local variables either used only in the very next return or exact copies of other variables. In both cases it's better to inline such a variable.


                  我认为在这种情况下根本不需要担心性能问题.话虽如此,正如@Clashsoft 在他的评论中提到的那样,JIT 很可能会内联变量,无论哪种方式,您最终都会得到相同的结果.


                  I don't think performance is something to worry about at all in this situation. That being said, as @Clashsoft mentioned in his comment, the JIT will most likely inline the variable and you'll end up with the same result either way.

                  这篇关于return 语句之前的局部变量,这有关系吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                  How to convert Integer to int?(如何将整数转换为整数?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                  Inconsistent behavior on java#39;s ==(java的行为不一致==)
                  Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)
                  Unexpected result in long/int division(意外结果导致长/整数除法)
                    <tbody id='MiC48'></tbody>
                  <i id='MiC48'><tr id='MiC48'><dt id='MiC48'><q id='MiC48'><span id='MiC48'><b id='MiC48'><form id='MiC48'><ins id='MiC48'></ins><ul id='MiC48'></ul><sub id='MiC48'></sub></form><legend id='MiC48'></legend><bdo id='MiC48'><pre id='MiC48'><center id='MiC48'></center></pre></bdo></b><th id='MiC48'></th></span></q></dt></tr></i><div id='MiC48'><tfoot id='MiC48'></tfoot><dl id='MiC48'><fieldset id='MiC48'></fieldset></dl></div>

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

                      <tfoot id='MiC48'></tfoot>

                          • <bdo id='MiC48'></bdo><ul id='MiC48'></ul>
                            <legend id='MiC48'><style id='MiC48'><dir id='MiC48'><q id='MiC48'></q></dir></style></legend>