2007-06-04

Do not write reusable code - write rewritable code

Do not write reusable code. Write rewritable code.

With reusable code I think of code that without retesting can be used in new ways. "Without retesting" means it has been tested for this "new" way of use beforehand.

The problem is that the code obviously has been written and tested for a usage not applied by the time of writing. To me it seems like a waste of time.

One could write the code reusable but not test it for any other usage than the present. I would recommend this since the source will then contain untested code which even might be interwoven with the tested. Mixing tested and untested code and increasing the complexity of the code by handling possible future situations will make the code harder to understand.

Better then is to write the code so it absolutely fits the needs and instead put some time into making the code simple and clear. Comment it even!

These thought boils down to
Simple code - easy to update.
Complex code - hard to update.

So instead of writing an if statement testing for a flag or inheritance or whatever insert a comment "If you plan to do this or that - here is the place to write the code."