It's a wonder any code is ever reused

| 0 Comments
It's rare that code can be viewed as a black box for reuse. If you include design choices and dependencies as valid parts of the code's interface then it's easier to explain why reusing nontrivial code is often harder than writing it from scratch.

One of the problems that I find when I'm trying to reuse some code is that even if the code is documented well, or written in a style where little documentation is needed, it can rarely be used as a black box. The dependencies, assumptions and 'soft' requirements are rarely documented and often at odds with those of the project into which the code is being inserted.

The code might be in C and my C++ sensibilities might be offended by how it operates. It may use exceptions and I might not wish to. It may use the C++ standard library, MFC, ATL, etc and that may not fit with the project that I wish to use it on. The error handling may be inadequate, there may be bugs, the code structure may be offensive. It may be optimised for speed and I need it optimised for space, etc...

The good thing is that by the time you've worked out all of this you've reviewed the code to the point where you can usually rework it into a style that fits with your project's requirements. If you can't then that adds more weight to the 'reuse as is and live with the consequences' side of the argument. The bad thing is that it's not always easy to explain why you needed to do that to management who feel you could have just used the code as is. Sometimes it's a close call. I've just been using some of Jeffrey Richter's code for windows performance counters from his Programming Server Side Applications book. His requirements and mine were a long way apart, but the code was complex to rewrite and relatively easy to wrap in a more acceptable interface. I wrapped it and used it as is. Now I find that I have a problem and his 'assert in debug builds' and don't check all errors "style" is causing me problems. I know that if I'd rewritten it in my 'throw exceptions on all errors and never assert' "style" that I'd now instantly know what the problem was rather than having mysterious crashes on one installation. So I'm in the process of reworking the code into my house style to help me locate the problem.

COM goes some way to help by restricting what you can do at interface boundaries and .Net goes further by assimilating the world. ;) But you still end up with the possibilities of the code you want to reuse having a poorly designed interface or inappropriate performance characteristics.

Add to all these "legitimate" reasons the fact that when we're at school and college we're always expected to create new and unique works rather than build on the work of others it's not too surprising that most programmers prefer to write new code rather than understand and use other people's code. I'm surprised any code gets reused at all... That said, there's no excuse for having mutliple versions of the same code within a project (unless one area requires radically different performance characteristics than another) and code duplication should be agressively refactored away.

Revision history

Leave a comment