简体   繁体   English

如何在VBA Excel 2016中查看已编译子例程的大小?

[英]How can one see the size of the compiled subroutine in VBA Excel 2016?

After finding a "procedure to large" error on a specific subroutine of a module, I am refactoring the code in the module. 在模块的特定子例程中找到“大过程”错误后,我正在重构模块中的代码。 I was curious on how effective the refactoring is at reducing the compiled code size. 我很好奇重构在减少编译代码大小方面的效果。

So I was wondering if there is a way to see the size of the compiled code of a subroutine in a Module of Excel 2016? 所以我想知道是否有办法在Excel 2016模块中查看子程序的编译代码的大小?

My attempts so far have been limited to searching online for methods to determine the size of a compiled module or subroutine in VBA. 到目前为止,我的尝试仅限于在线搜索确定VBA中已编译模块或子例程大小的方法。

It led me to the limits of VBA as listed in here . 它使我达到了这里列出的VBA限制。 It does not mention a way to see the size of the procedure compiled, nor does it mention whether that is (practically) possible. 它没有提到一种方法来查看编译过程的大小,也没有提到这是否(实际上)可行。

(As was mentioned in the comments, the current maximum size of the compiled procedure is 64K as stated here .) (正如在评论中提到,编译程序的当前最大大小是64K的规定在这里 。)

  1. I think the compiled procedure size is not 1 on 1 related to the number of lines. 我认为编译过程的大小不是1对1与行数有关。 (Because that does not take into account short or long lines. But I am currently unsure about how the vba-procure is compiled, and consequently how lines contribute to the compiled file size, otherwise a solution could possibly be to compute the compiled file size.) (因为这没有考虑短线或长线。但我目前不确定如何编译vba-procure,以及因此线如何贡献编译文件大小,否则解决方案可能是计算编译文件大小。)
  2. Nor is the procedure 1 on 1 dependent on the size of the code as stored as a '.txt' file. 过程1对1也不依赖于存储为'.txt'文件的代码大小。 (because it can contain comments which do not contribute to the compiled code size) (因为它可以包含对编译代码大小没有贡献的注释)

Disclamer - I am well aware of the shortcomings of the old code I am modifying. Disclamer - 我很清楚我正在修改的旧代码的缺点。 That it is poorly written, I think this quote of Bill Gates illustrates it quite well: 写得不好,我认为比尔盖茨的这句话很好地说明了这一点:

Measuring programming progress by lines of code is like measuring aircraft building progress by weight. 通过代码行测量编程进度就像按重量测量飞机构建进度一样。

I think refactoring, and breaking the code up in shorter subroutines is an appropriate first step. 我认为重构,并在较短的子程序中打破代码是适当的第一步。 To monitor this process, in combination with the hard bottleneck presented by VBA as the Procedure too long - error led me to this question. 为了监控这个过程,结合VBA作为Procedure too long的硬瓶颈 - 错误引发了我这个问题。

No, you can't do that. 不,你做不到。 VBA is compiled in several stages, it's part p-code, part interpreted, and at the end of the day the size in kb of the compiled procedure isn't what you need to work on. VBA在几个阶段编译,它是部分p代码,部分解释,并且在一天结束时,编译过程的kb大小不是你需要处理的。


You need to read about Abstraction , urgently. 你需要紧急阅读有关抽象的内容 A procedure that triggers this compiler error is over 10K lines of code. 触发此编译器错误的过程超过10K行代码。 A healthy procedure at the appropriate abstraction level might be somewhere between 500 and a thousand times smaller than that (not kidding) - the size of the compiled code is absolutely meaningless . 在适当的抽象级别上的健康过程可能比那个小500到千倍(不是开玩笑) - 编译代码的大小绝对没有意义

If you're worrying about the size of the compiled code, you're not writing the code for a human. 如果你担心编译代码的大小,你就不会为人类编写代码。

Code isn't written for a compiler to crunch and a runtime environment to run. 代码不是为编译器编写的,而是为了运行而运行的运行时环境。 Code is written for a human maintainer to read, understand, follow, debug, modify, extend, etc. Without any level of abstraction, code is a boring, excruciatingly mind-numbing series of executable statements that are invariably redundant, inefficient, and annoyingly bug-prone. 代码是为人类维护者编写的,用于阅读,理解,跟踪,调试,修改,扩展等。在没有任何抽象层次的情况下,代码是一个无聊的,令人难以忍受的令人烦恼的一系列可执行语句,总是冗余,低效和烦人容易出错。

There are 3rd-party tools you can use to analyze VBA code. 您可以使用第三方工具来分析VBA代码。 MZ-Tools 3.0 was free, but the latest version isn't. MZ-Tools 3.0是免费的,但最新版本不是。 It has a feature that can tell you how many lines of code are in each procedure of each module, how much of it is commented-out, whether you have unused variables, etc. 它有一个功能,可以告诉你每个模块的每个过程中有多少行代码,有多少代码被注释掉,是否有未使用的变量等。

Rubberduck is free and open-source, under active development (disclaimer: I own the project's repository), and has a code metrics feature (distinct from code inspections ) that can probably help you identify the most problematic areas (although, parsing a 10K-liner module might take a while): Rubberduck是免费的开源,正在积极开发中(免责声明:我拥有项目的存储库),并且具有代码度量功能(不同于代码检查 ),可以帮助您识别问题最严重的区域(尽管解析10K-衬管模块可能需要一段时间):

Rubberduck代码指标

Lines is your "lines of code" metric; 是你的“代码行”度量; Cyclomatic Complexity is a rough indicator of the different possible execution paths in your code (how much sense that metric makes at a module-level aggregate is debatable); Cyclomatic Complexity是代码中不同可能的执行路径的粗略指示(度量在模块级聚合中产生的多少意义是有争议的); Maximum Nesting is also an indicator of how badly "arrow-shaped" code might be. 最大嵌套也是“箭头形状”代码可能有多严重的指标。

High values in these metrics are a sign that you may need to extract methods . 这些指标中的高值表示您可能需要提取方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM