Harvest for reuse

Jeff Atwood writes about the the delusion of reuse where he warns of the extra effort that’s really required when writing for reuse:

“I believe writing a truly reusable class is an order of magnitude harder than writing a single use class.”

He also warns of putting in extra effort up front with the intent of creating reusable code from scratch and suggests that you can’t know if you have a strong case for making some code reusable until you’ve written or used the same piece of code three times in different projects. This all rings very true for me; we tend to achieve our reuse through harvesting, allowing duplication to grow in several projects and then, when we’re sure there’s enough commonality between the pieces of code, harvesting them into a library and reusing the single implementation in the projects. Even then it sometimes doesn’t work and I’ve ended up with forced reuse where I’m trying to use the same code in two places but both places need to pull the code in new and different directions as they evolve; by trying to reuse the code the designs of the unique code in the projects suffered.

Code reuse is a hard problem, it takes longer and costs more to create reusable code; up front design for reuse doesn’t always help. If you harvest for reuse at least you know that the code you have is suitable for use in multiple different places.