Everytime we need a new feature, code need to be added. Adding a new feature is not difficult, the most difficult part is how to add the new code. Can the new code bug be easily extended? Can the new code be reuse?
When come to reusability, things become tricky. I always try to integrate the new feature into current routine, to make the code “reusable”. But it is not a good idea everytime.
When the function is shared by many components, the new feature may not be compatable with all components. It takes lots of effort the handle the conflict.
After spending effort to integrate the new feature, we may find we need to customize some component, we have to break the function into two. The effort on making the code “reusable” is wasted.
It is time consuming to refactor the same piece of code again and again. That’s why i come up with a question: When to reuse code?
Functional code vs glue code
I divide the code into two types, functional code and glue code. Functional code is the core logic. It can be business logic, database logic and other logic which is ‘working’. Glue code is the code which join different components, libraries and functional code together. It perform wrapping the interface, data formating.
Never reuse glue code, even the code is exactly the same
Glue code is doing integration. It join two components together, it can never be decoupled from that components. In other words, glue code is tailor made for and depends on specific components. The glue code cannot be reused from the ground up. Sometime you may found the glue code are similiar, but it is just a coincidence. After a few iteration and development the glue code can be completely different. Don’t waste time on making it reusable.
Comment
Aug 27, 2018
This problem is mentioned in chapter 14 of Domain-Driven Design: Tackling Complexity in the Heart of Software
To reuse code. we have to organize the business logic into a “domain model” first. Everytime we change code, we need to re-visit the domain model. Whether reuse the domain model or not, depends on model integrity.
For the glue code which stick domain model together, we don’t need to consider reusablility