Lecture
Code reuse — a methodology for designing computer and other systems, based on the idea that a system (a computer program, a software module) should be composed partially or fully of parts of previously written components and/or parts of another system, and that these components should be used more than once (if not within a single project, then at least across different ones). Code reuse is the principal methodology used to reduce the effort involved in developing complex systems.
The most common case of code reuse is program libraries. Libraries provide common, fairly general-purpose functionality that covers a chosen problem domain. Examples: a library of functions for working with complex numbers, a library of functions for working with 3D graphics, a library for using the TCP/IP protocol, a library for working with databases. Developers of a new program can use existing libraries to solve their tasks and avoid «reinventing the wheel».
Programmers strive to design their systems to be as modular as possible. Functions, coroutines, classes, and protocols can all serve as abstractions on which a system's modularity can be built. A function library is a good example of an abstraction that is convenient for implementing program modularity and following the reuse methodology. The principle of hierarchical namespace construction became an important step toward achieving maximum modularity.
The command-shell tools of Unix systems and the standard Java classes, arranged within a namespace hierarchy, can serve as examples of a successful implementation of modularity and the reuse principle.
Templates (see the Standard Template Library, STL, in the C++ language) of functions and classes became an important stage in advancing the reuse methodology into the object-oriented programming industry.
Hierarchical system modularity makes it possible to implement effective development management methods based on building management hierarchies that correspond to the hierarchy of the system's own modules.
Sometimes code reuse consists of simply copying some portion of code from an existing program into another (copy-paste). This is one of the lowest-level approaches to reuse. But it has its place too, especially when it comes to code reuse «in the small» («reuse in the small»).
Such an approach is generally not recommended; instead, the repeated fragment of a program is turned into a subroutine or a macro with a set of parameters. The main argument in favor of using subroutines rather than copying code is that, if there is a bug, it needs to be fixed just once in the body of the subroutine; otherwise, in the general case, one has to fix several identical code fragments located in different places in the program. In addition, copying code usually creates a need to change variable names, which also often leads to mechanical errors. When subroutines are used, such renaming can be avoided by using local variables.
The code reuse method is an important component in implementing the principle of the metasystem transition in the development of the software industry. Putting this principle into practice allows developers to operate with high-level concepts (display an image, delete a table from a database, find all the roots of an equation, convert a file, etc.) rather than low-level ones (color a pixel red, zero out a register, add two numbers, read a character from a file, etc.).
Let us examine the advantages and disadvantages using the example of function libraries.
Using ready-made libraries has a number of advantages. First, the developer of a new system is relieved of the concern of implementing the functionality contained in that library. The entire library development cycle is carried out by the developer of that library. That developer usually takes responsibility for maintaining the library: fixing bugs, evolving and improving its operation, and testing. The code reuse method is the mechanism that allows developers to «stand on the shoulders of giants» and quickly build new complex systems out of already debugged components. The second advantage stems from the very repetition of code use, which leads to a significant reduction in the size of the resulting program, and—where the host platform has insufficient performance—to greater speed as well.
In addition to its few but very important advantages, the code reuse method has a number of disadvantages. Adding third-party libraries to a project automatically creates a need to control the compatibility between the versions of the components of the system being created and the versions of the libraries used. The most characteristic example of such an error is considered to be the crash of the «Ariane 5» launch vehicle, caused by the use of a software module developed for the «Ariane 4» rocket .
It is also important to note that many libraries are commercial and require monetary expenditure (with the growth of the free-software movement, this is gradually becoming less relevant). In addition, libraries are often not general-purpose enough and fail to implement the functionality required by the system being created, or, conversely, are too general-purpose and as a result are inefficient, inconvenient, or contain a lot of functionality that is redundant (for the given project). If the license of the distributed library permits, one can use its source code and modify it as needed. But after that, responsibility for maintaining the library's functionality shifts onto the shoulders of the developer of the new system. Also, using excessive modularity can lead to a reduction in program execution speed, when the execution speed built into a module cannot outweigh the overhead of calling that module.
Comments