Freigeben über


Creating reusable code

This blog discusses just a couple of ways that you can make your code reusable.
If you frequently create similar projects, you can save yourself considerable development time and ensure application consistency by creating and using object and project templates. For example, say that you use a copywrite form or common switchboard across your application. In these cases, you can add the object to your integrated develoment environment (IDE), and then use them as a basis for new development in your projects and ensure a more consistent look and feel across all of your applications. Although Visual Basic ships with a number of templates, such as form templates, you aren't limited to those. Any standard object (for example, a form or module) you can create and save in Visual Basic can be made into an object template. Visual Basic also gives you the ability to create complete project templates. Project templates can be skeletal templates containing a small number of base objects, such as a few forms and modules, or they can be partially completed applications containing intricate code and a number of objects. To see various project templates, just start Visual Basic and select New Project. Projects such as the Standard EXE and ActiveX EXE are examples of project templates. You can also create your own custom templates.

It should come as no surprise that one of the biggest sources of problems in code and barriers to reusability are typos. So it follows that the easiest way to prevent this is to type less. For example, you should never have to type the following more than once in a project:

Set objApp = CreateObject("Excel.Application")

You should type the statement once into a boilerplate module of frequently used code and copy and paste it into whatever module you are using. If you don't already have such a module, you should start one. If this doesn't fit your style or doesn't lend itself to your situation, at least start copying statements from another procedure instead of retyping them.

Another practice that makes for more reusable code is to use variables instead of literals. For example, look at the following two code segments:

Select Case Action.Name
   Case "Approve"
      NewItem.Importance = 2
   Case "Disapprove"
      Item_CustomAction = False
   ...
End Select

Const olImportanceHigh = 2
Select Case Action.Name
   Case "Approve"
      NewItem.Importance = olImportanceHigh
   Case "Disapprove"
      Item_CustomAction = False
   ...
End Select

Looking at the value of the "Importance" entity, it's pretty clear which segment is more readable. The value 2 in the first segment is called a "magic number" because it appears with no explanation as to its meaning. Now notice the assignment of 2 in the second segment. The value 2 is assigned to a variable that describes the meaning of the value. The use of the Const construct in the second segment makes the code much more readable and reusable.

In addition to providing descriptive meaning to the literal value, using a Const construct has another advantage that helps with code reusablity. Consolidating the actual definition at the Const construct makes it easy to make changes to the value. Regardless of how many times the variable is used in the module, if the value of the literal must change, the variable can be changed in just one location.