How to increase code reusability?
We all know about code reusability, its advantages, importance, etc. But do we really reuse our code? If yes then to what extent?
Although, only following the best practices already in the industry can make do everything you want, but even following that comes with a price(lower short term productivity) and requires skill or more of a habit that comes with time.
Meaning, unless you not working on a big project you are better-off asking do you really need to improve it rather than focusing on how and start learning new architectures.
So, let’s talk about what part of code is doing nasty but really need a check following with the ways to code reusability and the tools come with different programming languages and paradigms.
How to find reusable code?
Basically, every block, every line, and even every statement whichever repeats more than once is reusable, it’s only up to you how much you want to your code to be concise. This is because there is always a trade-off between Efficiency and Effectiveness.
That being said, finding reusable code is more of a selective process rather than an identifying process. Just think what is bothering you the most and you just can’t live with it.
Few questions to ask while deciding what part to Dedup.
- Can you make it better (both algorithmically and syntactically)?
- What if I have to change it in the future?
- If not sure what if you find a better way tomorrow?
- What’s the trade-off? Is the current code is too naive and must be fixed soon enough?
- Would changing it in the future would be easy?
- Does a block of code is repeated or common but can be extracted out?
Tools and techniques
Object-Oriented Programming has made code reusability a breeze and most of the techniques for better code follows it not only but which we gonna explore next. Although, code reusability is nothing new and in functional programming, there are multiple tools to do it as well (e.g. function as a variable). Not only this, today even OOP languages are following the functional way (e.g. the development of Java over time and how the interface resemble a function as an argument) with the motive of making the best out of both paradigms.
Some of the common tools and techniques to look for are:-
1. Abstraction
To remove boilerplate code or reduce the number of lines from a source you might be using frequently (even twice), the best thing to think about first is pulling out the abstractions. And to do so, inheritance may work as your magic wand!
2. Polymorphism
This is actually the most common one. From constructor overloading function overriding we often encounter one of its usages.
3. Dependency Injection
Abstraction can only do a lot but not everything. Now after you have separated out the common code, you look for external dependencies just to make your implementation customizable.
4. Injecting logics
This is where interfaces come into play. You write an implementation and pass it to others. An example would be to think of as if you got a block of code which is almost the same with few lines changed depending on a condition(say variable), then you can simply replace it with a functional interface with the condition (or variable) as an argument.
5. Generics
Sometimes, the whole implementation is just the same but it is the only the data type which may change. This can simply be handled with generic types.
6. Separation-of-concern
Code with similar concerns should be cubbed together(e.g. APIs, UI, business logics, etc.). It won’t only help with better navigation but also hinder you to repeat the same lines.
Tips
- All the tools that your language has are only to write better code. When ever you want to reuse a piece of code just think about some of the tools your programming language has provided you and if any of them can be helpful.
- Writing efficient code is not always gonna work and increase productivity. Although it does prove to be effective in the longer run, one must still think that how long it can run!
- Do not focus a lot on reducing the LOC or the number of files. Sometimes you write more code just to not repeat it in the future.
- Extract the duplicate code with the dependencies. (e.g. operation to mutate object is better placed inside the class definition but helper classes are preferred to inject dependencies)
Conclusion
What I have given above are just a few examples. If you see, every other tool or feature that your programming language provides you can be used to reduce code duplication. Comparing to how to find reusable code which is more of a selective process, this is an identifying process. You need to identify which tool can be used to remove duplication.