This is why I love OCaml.

I’ve been working on a big refactor of one of our core trading systems. It was a serious change. This is a system with many cooperating components, and in the old design, a certain amount of stitching work was required to integrate each new component. The end result was that the core of the system was becoming overgrown and hard to understand and extend.

So, I replaced this ad-hoc component system with a plug-in architecture where every component registered itself in a uniform way, with a flexible, type-safe way for the components to communicate. Now, no changes are now required to the core when you add a new component.

While I was at it, I entirely replaced the mechanism for querying the system with a new one that fit more cleanly into this new plug-in architecture, and at the same time providing a better, more typeful interface to clients of the system, with integrated help and support for cross-version interoperability. We also pulled out the configurations for the components into their own files with an explicit versioning scheme, to make it easier for outside tools that need to generate configs to deal with the system as it evolves.

All told, the change were pretty invasive. I had to modify virtually every file in the system, changing how the components interacted with each other, with a handful of components needing to be seriously rewritten.

A couple days ago, another developer here helped put a few finishing touches on, merged in patches that had occurred in other branches, and got it up and running in test for the first time.

And the shocking thing is, it worked.

I’m not claiming that no bugs were introduced in the refactoring; surely we have more testing work ahead of us. But I found it stunning that the whole system started up and worked without incident, with no testing whatsoever. And it’s not that I’m such a great programmer. This kind of result is pretty routine, because the type system is so effective at catching bugs, particularly bugs in code that has been written carefully to make the most of the type system.

People often talk about the value of refactoring tools that automate the tedious work of renaming method calls and changing the order of arguments. Surely that’s a nice thing to have. But it’s nothing compared to the help you get from OCaml’s type system. OCaml doesn’t automate the work of refactoring, but it does greatly reduce the number of bugs that your refactoring introduces. And in the end, that’s far more valuable.