What Is Reusable Code?
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.
Reusable code is code that can be used, without modification, to perform a specific service regardless of what application uses the code.
There are everyday objects that perform a specific service in different circumstances all around you. Think of a calendar. It gives you the ability to look up days and dates. You can use it to determine that this year your birthday falls on a Tuesday, or that Thanksgiving is on the 25th, or that there are two extra days to file tax returns because April 15 is on a Saturday. When you are building software, objects are created in code, and reusable objects that perform specific services in different circumstances are called components.
When you use Microsoft® Office to build custom applications, you write code that leverages the power of Office components. Using an Office component means you not only do not have to write the code yourself, but you are using a component that has been tested and found reliable in different conditions. In the past, developers would consider writing a custom program to check spelling. Today, you would call the spelling checker provided with Microsoft® Word. Similarly, nobody would develop custom code to calculate depreciation or determine principal and interest payments on a long-term loan. Instead, you would call the VBA built-in financial functions or use the Microsoft® Excel Application object to handle complex calculations for you.
Just as you can build custom applications based on components supplied as part of Office, you also can build them by using reusable components you have created yourself. You can think of reusable code from the perspective of the code that will call it to perform its service. This reusable code is a black box that accepts a known input value and returns a known output value. What happens inside the box (how the procedure actually works) is irrelevant to the code that calls it.
When you get into the habit of writing reusable procedures, you will find that you often have applications where groups of related procedures work together to perform a single service or a group of related services. For example, you might have a group of procedures that provides data access services or another group that consists of string-handling routines. This is an opportunity to group related procedures in their own module (or in a class module that exposes methods and properties to gain access to the procedures). Then, you can add the module to any application that requires the services it provides.
Designing Code to Be Used Again | Writing Reusable Code | Source Code Sharing