問題描述
對不起,如果這是一個(gè)新手問題,但我找不到答案.這樣做更好嗎:
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;
我知道整數(shù)使用內(nèi)存,所以我猜它會(huì)稍微降低性能?但另一方面,它使內(nèi)容更清晰,尤其是當(dāng) int/string 是一個(gè)長計(jì)算時(shí).
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 還多,那么了解這一點(diǎn)也很重要IntelliJ 在 Kotlin 中也對此進(jìn)行了檢查:
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:
變量只在后面的返回中使用,應(yīng)該被內(nèi)聯(lián)
Variable used only in following return and should be inlined
此檢查報(bào)告僅在下一個(gè) return 語句中使用的局部變量或其他變量的精確副本.在這兩種情況下,最好內(nèi)聯(lián)這樣一個(gè)變量.
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.
實(shí)際上有一個(gè)從 PMD 繼承的 SonarQube 規(guī)則,稱為 返回之前的不必要的本地,談?wù)撨@個(gè).它說:
There is actually a SonarQube rule inherited from PMD called Unnecessary Local Before Return that talks about this. It says:
避免不必要地創(chuàng)建局部變量.
Avoid unnecessarily creating local variables.
此規(guī)則后來被 SSLR 規(guī)則替換不應(yīng)聲明變量然后立即返回或拋出,保持相同的位置:
This rule was later replaced by SSLR rule Variables should not be declared and then immediately returned or thrown, which maintains the same position:
聲明一個(gè)變量只是為了立即返回或拋出它是一個(gè)錯(cuò)誤練習(xí).一些開發(fā)人員認(rèn)為這種做法改進(jìn)了代碼可讀性,因?yàn)樗顾麄兡軌蛎鞔_地命名正在發(fā)生的事情回.但是,此變量是內(nèi)部實(shí)現(xiàn)細(xì)節(jié)不會(huì)暴露給方法的調(diào)用者.方法名應(yīng)該足以讓來電者確切知道會(huì)發(fā)生什么返回.
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:
變量只在后面的返回中使用,可以內(nèi)聯(lián)
Variable used only in following return and can be inlined
此檢查報(bào)告僅在下一次返回中使用的局部變量或其他變量的精確副本.在這兩種情況下,最好內(nèi)聯(lián)這樣一個(gè)變量.
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.
我認(rèn)為在這種情況下根本不需要擔(dān)心性能問題.話雖如此,正如@Clashsoft 在他的評(píng)論中提到的那樣,JIT 很可能會(huì)內(nèi)聯(lián)變量,無論哪種方式,您最終都會(huì)得到相同的結(jié)果.
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.
這篇關(guān)于return 語句之前的局部變量,這有關(guān)系嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!