Writing reusable code for the sake of reuse is a bad idea for the following reasons:
- It leads to far more complicated code. Specific code is simple, all code necessary is closer and simpler to understand.
- Most often we don’t know all the cases we need to handle. As we keep adding more cases, the code becomes more and more complicated.
- Generic code is many times, harder to write, understand and maintain.
Reusable code must always have a purpose. Following are some reasons:
- Make code or UI more consistent. We don’t want the same thing to be represented differently in different places.
- After writing lots of specific code, we realize that we need to handle a lot of cases in a similar way. We can then extract the common code and make it reusable.
- We don’t want to update the same code in multiple places every time. We can extract the common code and make it reusable.
- Good set of reusable abstractions can allow you to write code in a declarative way making your code easier to understand.
In essence, write specific code. Find abstractions. Create well defined abstractions. Write code in these abstractions in a declarative fashion.