What is reusable code?
In software programming, reusable code refers to the use of the same source code collection in multiple applications or systems.
At its best, code reuse is accomplished by sharing common classes or collections of functions and procedures (this is possible in C++, but not in Smalltalk or Java). At its worst, code reuse is accomplished by copying and then modifying existing code.
There are multiple ways to write reusable code. In this article we are going to review the most common reusable code patterns.
Reusable code in Object Oriented Programming
Back in the origins of object-oriented programming, object classes were thought to be reusable for any future project under any circumstance. This, however, hasn’t been the case as code reusability is a difficult technique that is not suitable for all programming scenarios.
Object Oriented Programming is based on objects. Objects are represented by interfaces which specify the properties of the objects. The goal to code reusability in Object-Oriented Programming is to design objects in a way that can later on be used on other systems.
The truth about code reusability is that software projects frequently overrun their budgets and software is developed behind the planned schedule. This pressure on development can lead to code that is not clean becoming less reusable.
Reusable components
The evolution to object reusability is the concept of component reusability. A component is a unit that can be reused or replaced. The ideal software applications are built using connections of components.
According to the Catalysis Approach, a component can be reused when:
- High cohesion. Meaning the internal methods and properties of a component should have a strong relation and not have logic that is independent.
- Low coupling with the rest of the system. A component should not care about the environment where it is being used.
- A well-defined interface. This allows for external services to know how to interact with the component.
- It should be an extraction of a well defined concept. Components should follow the single responsibility rule and not handle multiple unrelated concepts.
Reuse code using libraries
The next step on this code reusability journey is to review code libraries. It was believed that OOP would create tons of libraries and that libraries won’t require programmers any more, just connecting existing libraries. However, that hasn’t been the case.
Too many developers underestimate the difficulty of producing a usable library, while overestimating their own ability to do so. Objects deliver better semantics, organization of code/functionality and, possibly, ease-of-use. Well designed libraries deliver on the promise of code reuse, not objects per se.
The most successful code libraries tend to be small and full-filling a well defined purpose, rather than trying to satisfy multiple needs. Libraries need to have a well documented APIs so the reusability can be done without direct access to the source code.
Benefits of code reuse
Richard John Anthony wrote key benefits of reusing code in its book Systems Programming (2016):
- You have to write less code.
- Code readability. When blocks of code are centralised and reused, the methods implementing that functionality become smaller and easier to read. This also helps to detect errors while coding.
- Improvement on the code structure. There is a prevention of duplicated code as it is been centralised. This prevents inconsistency and code redundancy. Any refactor to code will be in a central place and will benefit all.
- Reduction of testing effort as centralised code can manage its own unit testing and there won’t be a need for adding duplicate test cases when same functionality is been reused.
Reusable code problems
As with everything in software programming, there are also cons of using code reusability approach. Here are a couple of examples:
- A reusable object tends to be larger and have more lines of logic than a non-reused one. This is due to the fact that reusable objects need to be able to react to multiple situations. Non-resused objects are written with abilities to satisfy the current need.
- A research from Scaffidi (2009) specifies that web applications that use reusable libraries tend to have more lines of code than the ones without reusability.
- Maintainability of a reused code base can be difficult to manage. A simple change on the code will impact all systems using the source code.
Reusable Knowledge in Software Programming
While doing my research for this article I came across the “Reusable Knowledge” term. This has changed my way of thinking about code reuse.
Due to difficulties attached to code reusability the “reuse of knowledge” concept has been researched. Instead of reusing the lines of code what it is being reused is the knowledge on how to build a system taking into account past experiences.
An ongoing research is the KnowBench architecture. The concept lies on capturing knowledge from building a software system and applying it on future developments. Documentation is key to this type of reusability. This research uses Semantic Web technologies to generate annotations that capture knowledge and experiences that can prevent the repetition of past failures in future software development projects.
Another example of knowledge reuse in software systems is the idea of applying program transformation to a set of knowledge concepts and transforming it into software.
The code reusability journey is far from over. It will be an exciting topic to follow in the upcoming years as new and powerful technologies are constantly raising.
References