Programming by patching

Computer

I’ve just seen a very postmodern approach to programming in the Mozilla Roadmap blog.

  • The problem: Mozilla’s codebase is based on a fairly minimal subset of C++ that doesn’t include, for example, exceptions. This is historical; it used to be necessary to get reasonably portability. Now such things are widely supported they have a big codebase that’s hard to work with. (I’ve programmed in Mozilla’s XPCOM model and can testify it’s quite painful messing around with result codes). They’d like to convert a large legacy codebase to use exceptions and other modern C++ features.

  • The solution: use an existing static code analyzer to find locations in the source code which need rewriting. Then print out the original source prefixing each line with “-“, the desired new source prefixed with “+”, a couple of lines either side of the bit to be changed, and you have a patch file. Apply this to your source code.

A more classical way to do this would have been to build a complete abstract syntax tree (AST) of the program, apply changes and then serialize the AST. This has many disadvantages, e.g. you lose your comments unless you parse them, you lose formatting, there’s a higher risk of screwing the file’s logic up, and a lot more work to be done. Autogenerating the patch is a very neat solution.

You can read the full article on the Mozilla Roadmap blog.