In praise of rewriting

Published .

There is value in doing old things from scratch in a new project. You learn to simplify and to correct mistakes. If you reflect well on your previous design you may achieve a sort of reverse second system effect: a more spot-on, more straight-forward replacement for what was overly complicated. The common wisdom says you shouldn't rewrite a large existing codebase from scratch and it's probably correct for the vast majority of cases. However, if you are starting a new project that has overlap with what you've done before it may be an opportunity to get right what you didn't. If your new project has a sufficiently small scale that you are not forced into reusing bits from its predecessors rewriting should be fair game.

The real world case that lead me to this conclusion was working on the build system for tcl-augeas and then tcl-duktape. The former handles a more complicated use case and abstracts away the particulars related only to the project for which it was built but it is not featureful not enough to be easily reused for any other given project. As a result it currently sits uncomfortably somewhere in the middle between a build script and a "real" build system. While it may be expanded into a "full-sized" build system later that is beyond the scope of the project that uses it. The downside that is apparent right now, however, is that its source code is not as easy to read as one would want due to the abstraction.

The latter build system, on the other hand, throws away much of the abstraction and the declarative approach to configuration that followed from it. Instead, it opts for an imperative approach by defining commands that are useful for compiling and installing Tcl extensions written in C. There are just a few settings where it is desirable to reuse certain values for the sake of DRY. As a result the code reads more like a Tcl Makefile than a Tcl SCons. The second build system's code turned out less abstract but easier to read and to maintain thanks to the developer's previous experience and his freedom to start from scratch.

Tags: software design, programming, old blog