Monday, October 21, 2013

Do not design for reuse - design for loose coupling

I came across this cool line today:
"It can be better to copy a little code than to pull in a big library for one function."-- Go at Google by Rob Pike
Nowadays we keep hearing about all the virtues of code reuse, so much that it's almost becoming unheard of to question it. People may look at you funny when you suggest that some DTO class should be a separate class even though it happens to contain all the same properties as an existing model class.

It can be difficult to determine where to draw the line, but remember: just because two classes happen to look the same doesn't mean that it is necessarily the same thing. Sometimes copying & pasting is better than reuse e.g. using inheritance.

Reuse means coupling (or a dependency) when there is no need for that dependency.

I've always like this OOAD 101 blog post about coupling and cohesion:
"DO NOT design for reuse. Design for loose coupling, high cohesion. Do that and reuse will happen on its own."
Also see: Is code reuse a lie?
Post a Comment