Share via


Designing Code for Reusability

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

When you have mapped out the overall design for your application, spend some time thinking about how to structure your code within the application. There is always more than one way to write the code, and with a little additional effort, you can maximize the reusability of your code. The work you do now to make your code reusable, when possible, can save you and other developers time in the future. If you write code with the intention of reusing it in other scenarios, your code might be easier to maintain, because you will tend to write smaller, more compact procedures that are easier to modify.

  • Whenever practical, write procedures that are atomic — that is, each procedure performs one task. Procedures written in this manner are easier to reuse.
  • Adopt a set of coding standards, and stick to them.
  • Document your procedures and modules well by adding comments, so you and other developers can figure out later what your code is doing.
  • Group similar procedures or procedures that are called together in a module. Not only does this help you to organize your code functionally, it can enhance your application's performance. By default, Microsoft® Visual Basic® for Applications (VBA) compiles code on demand, meaning that when you run a procedure, VBA compiles that module and the modules containing any procedures that are called by that procedure. Compiling fewer modules results in improved performance. (To further improve performance, be sure to save your code in a compiled state.)
  • Develop a library of procedures that you can reuse. Use a code library tool to store procedures and code snippets, so they are available immediately when you must have them. Microsoft® Office XP Developer includes such a tool, the Code Librarian, which is an add-in for the Visual Basic Editor and the common development environment.
  • Create custom objects by encapsulating related code in class modules. You can add properties, methods, and events to custom objects, as well as organize them into custom hierarchical object models. When you have built and tested a custom object, you can treat it as a "black box" — you and other developers can use the object without thinking about the code that it contains. When you must maintain the code, you only have to modify it once within the class module.
  • Develop interfaces to extend custom objects. An interface provides a basic set of properties, methods, and events to any custom object that implements the interface. By using an interface, you can create sets of closely related, yet distinct, objects that share common code.
  • If you have Visual Basic, you can create Automation servers, which are .dll or .exe files that contain custom objects. The advantage to packaging custom objects in separate .dll or .exe files is that you can call them from any VBA project, just as you would call any Automation server, without having to include the code for the object in your project.

See Also

Designing Applications | The Design Process | Which Technologies Should You Use? | Thinking About User Interface Design | Security Considerations | Testing Your Application | Application Deployment | Sharing Code with the Code Librarian