To boost programmer’s productivity, we reuse codes. But it is not always a good idea have code reuse
Reused code must have clear responsibility
The usage of the code may be well defined and having documentaion on the abstraction layers. It is dangerous if other using the code without understanding it. The code may be used in some tricky or unexecpted ways and cause bug difficult to fix.
Reused code should not be too complex
If the feature of the reused code is too complex, productivity may not be improved. Learning how to re-use code take time. If the code is too complex, it will take programmer lots of time to understand how to reuse codes.
Reused code must be well tested, at least testable
The cost on refactoring or fixing reused code is very high. It is because everytime the reused code update, we have to test all components depends on it. After that, we have to clean up workarounds and hotfixes in components. That’s why before reusing code, we have to test and build automated test if possible.
Consider factor out codes when it is reused three times or more
In most cases, some works need to be done before reuse. Such as testing, update the function interface for different scenarios, documentation, etc. If we make all code reusable, it will become over-engineering. There is a rule of thumb that only generalize the code which is used at least three times. TODO: add reference
Reference
十年磨一剑—序: Spent too much time on studying a powerful but complex log system
Reuse Myth - can you afford reusable code?: Code reuse can be expensive