The Strangler Pattern

Currently rereading Beyond Legacy Code, and was reminded of the strangler pattern.

If one is working on a legacy application, there may be many issues with it that make you unhappy, for instance

  • Lack of test coverage
  • Brittle code
  • Tightly coupled / spaghetti code
  • Lack of design

and on, and on, and on.

It may get to the point where you think it’d be better to throw the whole thing away and start again.

However, you can find a wealth of articles like this that put forwards reasons why such an approach is risky, and that one should consider alternatives prior to making such a big commitment to change, both in terms of risk and time.

An alternative is to refactor the existing code base in chunks, allowing existing code and new code to work in parallel, and slowly transitioning over wholly to the new code base. This provides the opportunity to add in any required missing tests, continue to add customer value and make progress without needing to effectively ‘stop the world’ as the application is re-imagined and re-implemented.

If one takes this approach, this is where the strangler pattern can be of benefit.

Martin Fowler spoke about it in 2004.

I quite like this article on using it in practice, and this article that provides some case studies.