How to write High quality VB Code (2)

Source: Internet
Author: User
Tags error handling execution reference requires
How to write High quality VB Code (2)

12. Destruction of objects
Regardless of what software is written, programmers need to consider releasing the memory space that the software occupies after the user decides to terminate the software's operation. Unfortunately, many programmers don't seem to care much about this. The right thing to do is to destroy the object used in the program before exiting the program. For example:
Dim FSO as New FileSystemObject
Remarks: Performing Actions
Remarks: Destroying objects
Set FSO = Nothing
For forms, you can uninstall:
Unload Frmmain
Or
Set frmmain = Nothing
13. Variable length and fixed-length strings
Technically, fixed-length strings require less processing time and space than variable-length strings. But the disadvantage of fixed-length strings is that in many cases you need to call the trim function to remove the null characters at the end of the string, which in turn reduces the efficiency of the Code. So unless the length of the string doesn't change, the variable-length string is used.
14. Use class modules instead of ActiveX controls
Unless the ActiveX control involves the user interface, use lightweight objects, such as classes, as much as possible. There is a big difference in the efficiency between the two.
15. Using Internal objects
When it comes to using ActiveX controls and DLLs, many programmers like to compile them and then join the project. I recommend that you do not do this, because the connection from VB to an external object requires a lot of CPU processing power. Every time you invoke a method or access a property, a lot of system resources are wasted. If you have the source code for ActiveX controls or DLLs, use them as private objects for the project.
16. Reduce the number of modules
I agree with the fact that some people like to keep common functions in modules. But it's funny to write only twenty or thirty lines of code in a module. If you're not very much in need of a module, try not to use it. The reason for this is that VB loads modules into memory only when functions or variables in the module are invoked, and when the VB application exits, the modules are unloaded from memory. If there is only one module in the code, VB will only carry out a load operation, so that the efficiency of the code has been improved; Conversely, if there are multiple modules in the code, VB will carry out multiple loading operations, the efficiency of the code will be reduced.
17. Using an array of objects
When designing the user interface, for the same type of control, programmers should use an array of objects as much as possible. You can do an experiment: add 100 PictureBox to the window, each PictureBox has a different name, run the program. Then create a new project, also add 100 PictureBox to the window, but this time using an array of objects, run the program, you can notice the difference in the loading time of two programs.
18. Using the Move method
Some programmers prefer to use the width,height,top and left properties when changing the position of an object. For example:
Image1.width = 100
Image1.height = 100
Image1.top = 0
Image1.left = 0
This is actually inefficient because the program modifies four properties and the window is redrawn after each modification. The correct approach is to use the Move method:
Image1.move 0,0,100,100
19. Reduce the use of pictures
Pictures will take up a lot of memory, and processing pictures also requires a lot of CPU resources. In software, if possible, consider using a background color instead of a picture--which, of course, is only viewed from a technical point of view.
20. Use an ActiveX DLL instead of an ActiveX control
If you design an ActiveX object that does not involve the user interface, use an ActiveX DLL.
Compile optimization
Many of the VB programmers I've seen have never used compilation options or tried to figure out the difference between the options. Let's look at the specific meaning of each option below.
1. P-code (pseudo code) and native code
You can choose to compile the software into P-code or native code. The default option is native code. So what is P-code and native code?
P-code: When executing the code in VB, VB first compiles the code into P-code, then interprets the P-code that performs the compilation well. In a compilation environment, this code is faster than native code. Select P-code, compile-time VB will put the pseudo code into an EXE file.
Native code: The native code is the option that VB6 later launches. When compiled to EXE file, native code executes faster than P-code. When the native code is selected, VB uses machine instructions to generate EXE files at compile time.
When compiling using native code, I found that sometimes some inexplicable errors were introduced. My code was completely correctly executed in the compilation environment, but the EXE file generated with native code options did not execute correctly. Typically this happens when you uninstall a window or when you eject a print window. I solved the problem by adding a doevent statement to the code. Of course, there is very little chance of this situation, perhaps some VB programmers have never encountered, but it does exist.

There are several more options in native code:
(a) Code speed optimization: This option can compile faster execution files, but the execution file is relatively large. Recommended Use
b Code Size optimization: This option can compile smaller execution files, but at the expense of speed, it is not recommended.
c) No optimizations: This option simply converts P-code to native code, without any optimizations. Can be used when debugging code.
D for Pentium Pro optimizations: Although this is not the default option in native code, I usually use this option. The executable program compiled by this option can run faster on machines with more than 2 Pentium Pro and Pentium, and slightly slower on older machines. Considering the current use of Pentium 2 is outdated, so we recommend that you use this option.
E produces symbolic debugging information: The item generates debugging information during the compilation process, enabling users to debug compiled code using a class of Visual C + + tools. Use this option to generate a. pdf file that records the flag information in the executable file. This option is also helpful when the program has an API function or a DLL call.
2. Advanced optimization
The settings in advanced optimization can help you improve the speed of your software, but sometimes you introduce errors, so I recommend that you use them as carefully as possible. If there is a larger loop body or complex mathematical operation in the code, some of the selected items in the Advanced optimization will greatly improve the performance of the code. If you use advanced optimization features, I recommend that you rigorously test the compiled files.
A) assumes no alias: can improve the execution efficiency of the code in the loop body, but if the variable value is changed by reference to a variable, such as calling a method, the reference of a variable is the parameter of the method, and the value of the variable is changed in the method, an error is raised. It is possible to simply return a result error, or it may be a serious error that causes the program to break.
b Eliminate array binding checks, suppress integer overflow checks, and suppress floating-point error checking: If errors are found through these checks when the program is running, the error handling code handles these errors. However, if these checks are canceled, an error occurs and the program cannot be processed. You can use these options only if you are sure that the above errors will not appear in your code. They will greatly improve the performance of the software.
c) allow floating-point operations not to be rounded: Selecting this option can be a compiled program that handles floating-point operations more quickly. The only drawback is that comparing two floating-point numbers can lead to incorrect results.
D Cancellation of Pentium FDIV security check: This option is for some old Pentium chip settings, now seems to be obsolete.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.