Why write framework-agnostic code

Yesterday, I wrote about writing layers in your application code and the benefits of loosely coupled code.

Something else you can do with this approach is to write framework-agnostic code.

By writing your business logic in code that isn't tied to a specific framework or CMS, with a small adapter layer, you can upgrade to a newer version of the framework, such as Drupal 7 to 10, or a different framework, keep most of the code the same and only update the parts that connect the business logic and the framework.

This is something that Commerce Guys (now Centarro) did when creating Drupal Commerce 2.0.

The logic around addressing, tax, etc., was released in separate PHP libraries, each with its own release cycle and reusable logic.

This meant the Drupal modules were much smaller, and other eCommerce systems and frameworks could use the agnostic libraries.

It's something to consider when writing your next Drupal module.

It's something I did recently and have done on client projects previously, and it can be a good approach.

- Oliver

P.S. Do you want to learn about automated testing in Drupal? Take my free 10-day email course and get daily lessons straight to your inbox.

Was this useful?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.