vba 为什么我们在模块开始时声明变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13856351/
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
Why do we declare variables at the start of a module?
提问by Jamie Bull
As my first language and as completely taught from other's example I never questioned the standard practice in VBA of grouping all variable declarations at the start of the module, routine or function they are scoped to as in this example.
作为我的第一语言,并且完全从其他人的例子中学到,我从来没有质疑过 VBA 中的标准做法,即在模块、例程或函数的开头对所有变量声明进行分组,如本例所示。
Sub Traditional()
Dim bVariable as Boolean
Dim OtherVariable
' Some code using OtherVariable goes here
'
' Now we use bVariable
bVariable = True
Do While bVariable
bVariable = SomeFunction()
Loop
End Sub
Now I'm learning that standard practice in other languages is to declare variables as close to where they are used as possible, like this:
现在我了解到其他语言的标准做法是在尽可能靠近它们使用的地方声明变量,如下所示:
Sub Defensive()
Dim OtherVariable as String
' Some code using OtherVariable goes here
'
' Now we use bVariable
Dim bVariable as Boolean
bVariable = True
Do While bVariable
bVariable = SomeFunction()
Loop
End Sub
This seems completely sensible to me as a defensive programming practice - in that it limits both span and live time (as explained in Code Complete), so I'm wondering if there is any reason for not doing the same in VBA? Possible reasons I can think of are memory, running time (e.g. repeatedly declaring inside a loop), tradition - arguably a good reason as there must be hundreds of thousands of VBA programmers who expect to see all used variables at the start of the routine. Are there any I've missed that might explain the benefit of this practice or at least where it came from?
作为一种防御性编程实践,这对我来说似乎是完全合理的 - 因为它限制了跨度和生存时间(如Code Complete 中所述),所以我想知道是否有任何理由不在 VBA 中做同样的事情?我能想到的可能原因是内存、运行时间(例如在循环内反复声明)、传统 - 可以说是一个很好的理由,因为必须有成千上万的 VBA 程序员希望在例程开始时看到所有使用的变量。有没有我遗漏的可以解释这种做法的好处或至少它来自哪里?
采纳答案by Dick Kusleika
I declare all my variables at the top. I think declaring them closer to first use masks (at least) two other problems that should be fixed first.
我在顶部声明了所有变量。我认为宣布它们更接近于首先使用掩码(至少)应该首先解决的另外两个问题。
- Procedures too long: If you're procedure is more than fits on a screen, perhaps it's doing too much and should be broken into smaller chunks. You'll also find that unit tests are way easier to write when your procedures are small and only do one thing.
- Too many variables: If you have a bunch of related variables, consider using a custom class module or user-defined type. It will make the code more readable and easier to maintain.
- 程序太长:如果您的程序超过屏幕大小,则可能它做得太多,应该分解成更小的块。您还会发现,当您的过程很小并且只做一件事时,编写单元测试会更容易。
- 变量太多:如果您有一堆相关变量,请考虑使用自定义类模块或用户定义类型。它将使代码更具可读性和更易于维护。
If your procedures are short and you're using classes and UDTs, the benefits of declaring the variables at the point of use are lessened or eliminated.
如果您的过程很短并且您正在使用类和 UDT,那么在使用点声明变量的好处就会减少或消除。
回答by Larry
I think both way are just different coding style in VBA
我认为这两种方式只是 VBA 中不同的编码风格
In old C Standard, all Declaration must be on the top, I think many people just adopt this habit and bring it into other PL such as VBA.
在旧的C标准中,所有的Declaration都必须放在最上面,我想很多人只是采用这种习惯并将其带入其他PL如VBA。
Declaring variable on the top is clear for short list of variable names. It will be unreadable for a long list of variable name
对于变量名的简短列表,在顶部声明变量是清楚的。一长串变量名将不可读
Declaring variable close to where it's being used is introduced later. I think this practice has a clear advantage over "declare on the top" for PLs that has optimizer or more scope than VBA. (Like you can declare variables where the scope is visible in a FOR loop only) Then the optimizer will change the scope for you. (In VBA words, it may change a GLOBAL variable to a PROCEDURE variable)
稍后会介绍在使用它的地方附近声明变量。我认为对于具有优化器或比 VBA 更大范围的 PL,这种做法比“在顶部声明”具有明显的优势。(就像您可以声明仅在 FOR 循环中范围可见的变量)然后优化器将为您更改范围。(在 VBA 中,它可能会将 GLOBAL 变量更改为 PROCEDURE 变量)
For VBA, no perference
对于 VBA,没有偏好