“变量应该被赋予最小的范围".真的吗? [英] "Variables should be given the smallest scope". Really?

查看:50
本文介绍了“变量应该被赋予最小的范围".真的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经多次阅读以下句子:

I have read the following sentence a multiple times:

一般来说,变量的作用域应该是最小的.

Generally, variables should be given the smallest scope.

查看变量num在以下场景中的使用:

See the use of variable num in the following scenarios:

Private num As Integer
Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer.Tick
    'Your code where num is initialized and used
End Sub

或者:

Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer.Tick
     Dim num As Integer
     'Your code where num is initialized and used
End Sub

请记住,Timer_Tick 是一个循环计时器,因此第一个场景中的 num 只声明一次,而在第二个场景中,每次计时器都会声明一次滴答声.

Bare in mind that Timer_Tick is a looping timer, hence num in the the first scenario is declared only once while in the second scenario it is declared every time the timer ticks.

第一次使用在代码执行速度上真的不一样吗?哪一种更有效率?关于这些例子,忽略可读性偏好的引用句子在多大程度上是正确的?

Is the first usage really different in terms of code execution speed? Which one is more efficient? In regards of these examples, to what extent is the quoted sentence correct neglecting the readability preference?

我相信如果代码中有大量的迭代和循环,第二种情况会比第一种情况有一些性能影响.

I believe that if there is a huge amount of iterations and looping in the code, the second scenario would have some performance effect over the first.

推荐答案

一般来说,变量的作用域应该是最小的.

Generally, variables should be given the smallest scope.

我同意.

第一次使用的代码执行速度真的不同吗?

Is the first usage really different in terms of code execution speed?

不仅仅是因为范围.

哪个更有效?

如果这只是范围的问题,那么我希望它们编译为非常相似的 CIL,如果不完全相同,因此没有区别.可能会有一些小怪癖导致一个人稍微击败另一个人,但这些怪癖可以以任何一种方式进行.

If it was just a matter of scopes, then I would expect them to compile to very similar CIL, if not identical, and as such have no difference. There can be some minor quirks causing one to very slightly beat the other, but then such quirks can go either way.

在这种情况下,虽然范围的不同意味着没有like和like的比较;方法外部的字段与内部的本地字段不同.通常,使用字段比使用本地字段更昂贵,但如果初始化只需要发生一次并且本身很昂贵,那么缓存(记忆化")可能会得到回报.然而,这不仅仅是范围的差异.Scope 往往不会对性能产生明显影响,除非它隐藏了一些使其不相似的东西(例如,捕获的本地与未捕获的不一样).

In this case though the difference in scope means there is not a comparison of like with like; a field outside a method is not the same thing as a local within. Generally a field is more expensive to use than a local, though if the initialisation need happen only once and was itself expensive then that caching ("memoisation") could pay off. This isn't a difference of scope alone, however. Scope tends not to have an appreciable performance impact unless it is hiding something which also makes it not like-with-like (e.g. a captured local is not the same as a non-captured).

关于这些例子,忽略可读性偏好的引用句子在多大程度上是正确的?

In regards of these examples, to what extent is the quoted sentence correct neglecting the readability preference?

引用的句子是关于可读性以及您从允许使用的范围中知道变量使用范围的事实.范围越小,就越能更好地反映意图,这是人类可读代码应该努力的目标.

The quoted sentence is all about the readabilty and the fact that you know the scope of the variable's use from the scope of its allowed use. The tighter the scope the better it reflects the intention, which is what human-readable code should strive for.

我相信如果代码中有大量的迭代和循环,第二种情况会比第一种情况有一些性能影响.

I believe that if there is a huge amount of iterations and looping in the code, the second scenario would have some performance effect over the first.

仅仅因为它不仅在这里起作用,而且还有变量的类型.

Only because it isn't just scope at play here, but type of variable also.

这篇关于“变量应该被赋予最小的范围".真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆