The Need to Reuse Proven Legacy Code
I'm often struck by the tendency that many programmers have to rewrite legacy code. I should know! I have both rewritten old working code and seen my own legacy code get rewritten by others. Although it's often justifiable to rewrite it, there is a case where the old code can and should be reused (especially when it is reliable, field-tested, and sufficiently useful for incorporation into a new project).
The SNMP-specific code that the program façade.exe calls is written in Visual C++ and makes use of the Microsoft SNMP API. As we'll see, this API is really C-based. Listing 2 illustrates an extract from the header file used to export the relevant legacy symbols.
Listing 2 Legacy SNMP Code API
#ifdef __cplusplus extern "C" int startupRoutine(int argc, char *argv []); extern "C" int dispatchOperation(int programMode, char * argumentVector[]);
Two functions make up the legacy code interface: startupRoutine() and dispatchOperation(). The caller provides the required parameters and invokes these functions to produce the output we saw in the Listing 1. This is conceptually illustrated in Figure 2.
Figure 2 Reusing the C-based API.
The left side of Figure 2 shows legacy applications written in C that call into the SNMP code. The latter then interacts with the network in the lower part of the diagram. On the right side of Figure 2, we want to strip out the entire contents of the old SNMP code and provide an interface to it for our new pattern-based C++ application.
So, rather than rewriting the SNMP code, we can simply create a C-based interface into the legacy code (as in Listing 2). This process saves a massive amount of time because it consists of repackaging source code interfaces rather than creating new code. Clearly, we save much time by reusing the old (perfectly satisfactory) code. Also, mixing languages (C++ and C in this case) is not to be avoided. Back in the days before C++ came along, it was common practice to call into C and even Assembly code libraries from languages such as Pascal.
NOTE
The guideline here is simple: Reuse your legacy code. The steady adoption of service-oriented architectures is a powerful illustration of the usefulness of working with existing systems and source code.