Code Organization with Multiple Related Projects

I’m working on a website project using WordPress that requires a few custom plugins. I want to keep the plugins modular, but a few of them require similar functionality. So, I find myself with conflicting goals:

  • Make each plugin self-contained and modular
  • Don’t repeat code

For example, I have a need in two different plugins to output <select> elements based on a list of terms and some arguments. I wrote a function to accomplish this, but should I duplicate the function in both plugins? That seems like a maintenance nightmare, but if I don’t, one plugin will depend on the other in a pretty arbitrary way. Is there some clever solution to this that I’m missing?

Related posts

Leave a Reply

1 comment

  1. Consider the duplicate code. In particular, consider the number of functions and their length. If both plugins share only a couple formatting methods, it’s not such a big deal to duplicate that code in order to keep the plugins self-contained.

    If the duplicate code is getting pretty hefty, start thinking about doing one of the following:

    1. Merge the plugins into a single plugin. Only do this if the two plugins fit into the same problem space. Take a good hard look at the plugins. There’s a chance that what made them seem separate before was illusory and the fundamental behaviors are closely related.

    2. Extract the duplicate code into a third plugin which is a dependency of each of the plugins that share the code. Only do this if the duplicate code is related and make sense as a plugin.