Code reuse — To do or not to do, that is the question

What is code re-usability?

Code reuse, also known as software reuse, is the concept of using existing software, or knowledge gained from existing software, to build new software. Lazy (or rather smart) programmers have always reused segments of code, templates, functions, and procedures from existing libraries. Code reuse is aimed to save time and resources and reduce redundancy by taking advantage of the assets that have already been created in some form within the software product development process. In Parallel code reuse also addresses problems like code duplication, code management and testing. Reusing a piece of code has led to creation of somewhat bigger concepts like generic programming.

Do not reinvent the wheel

One of the most important principles mentioned in every ‘sacred text’ of coding practices is “do not reinvent the wheel”. Why write again and cry when you can reuse and fly? The major concepts that is the backbone of code reuse is generic coding. While designing any software architecture one should think of the varied possibilities and use cases of that piece of code and hence take direction towards creating a code segment that can cater to a wide range of user needs.

Problems with generic code

In the quest of writing reusable software, it’s a common mistake to make software a bit too generic. To build a single, uber flexible, monolithic solution which is applicable in extremely broad range of use cases and environments being generic is just a basic requirement. However, making software too generic causes it’s usability to suffer. When a piece of software grows too large, it becomes bloated. Besides having a negative impact on the performance, the software typically becomes too complicated to use. As a resultant of this the components will have a more extensive API and configuration. This situation created will require notched up knowledge from the client’s side, and will make debugging harder as more things can go wrong. Developing and debugging the component itself becomes harder as one needs to account for an increasing number of edge cases. Those who have had a stint working with big corp-orates in technology and have built huge software would have at-least once come across issues debugging it. In conclusion this can result in the software not being used at all, defeating the whole purpose of writing reusable software. Thus, we can say:Maximizing on generic nature complicates usage

Truth about generic code

  1. Frequent code break is an expected behaviour. If you write the code, all in one place, there are no dependencies. By reusing code, you’ve created a dependency. The more you reuse, the more dependencies you have. The more the dependencies, the more debugging is required.
  2. Its a trade-off between simplicity and usability. Though code specific to a use case is easier to follow, catering to various customer needs should not be ignored as well. The key here is composed code and not a monolithic all rounder.

Writing reusable code is not about developing generic, monolithic all-rounders. The key to writing reusable code is to write focused, compo-sable components with a high cohesion and loose coupling. An excellent example of promoting the best practices of code re-usability is npm (Node Package Manager) which provides a plethora of java-script libraries and packages which can be reused instantly in your code base by just adding a dependency in your configuration.

Keep your code sanitized. Replace outdated components. Don’t be afraid to refactor when needed. Be careful to use patterns like interfaces and inheritance proportionally, do not overuse them. Strive towards writing focused components with a low complexity: easy to understand and easy to maintain.

We here at PnP Labs learn from every little experience we have while building great products for our clients. We also love sharing our learning from the mistakes we make so that the coding community can gain from it. If you find our insights useful, or wish to share your own, or just want to be a part of our community, write to us at root@pnplabs.in or comment below.

-Akhil Aggarwal