The Feature Magnet

God, I hate enum in Java. (Well, I don’t, but it got your attention didn’t it!?)

Here’s a simple chain of events, leading to an odd conclusion:

  • We have a component that can do processing based on configuration
  • We decide to put the different operations it could do into an enum so that we control the valid values
  • The code that correlates from the enum to the algorithm to process each operation goes into the same library as the enum (or a parallel library, it doesn’t really matter)
  • More corners of the system use this processing-based-on-configuration because it’s good
  • So we add more operations to the enum
  • And the library gets bigger
  • And contains specific features used from across the system
  • And they have no particular relationship with each other

Somehow, the development process here has violated the open-closed principle without us noticing.

The Feature Magnet

The problem we found ourselves facing was that a popular mechanism had to accumulate everybody’s features in order to do its job. This was because it was built in a way which meant that every time it needed extending it also needed modifying.

In languages like Java, where enum is the go-to place to constrain a set of values, it’s easy to fall into the above trap. How would you ensure that all the possible operation names were unique and that only valid operations could be parsed? Yet, if you centralise this mechanism fully, you end up attracting unrelated concerns into it.

The short answer is that an enum is for a closed set of values. For more open, extensible mechanisms, you need something else. When extending a core technique, extension should be done by composition rather than modification.

Other Magnetic Properties

Generally, it’s either bad design or popularity (or both) that leads to a domain gaining things from outside of a central problem space.

If a dev team does a particularly good job building something, it’s common for the users to demand more features go into the system. This can lead to the Kitchen Drawer Application, full of loads of unrelated stuff that’s kind of useful.

Such magnets are easier to construct than they are to maintain and restructure.

Be careful of what you’re attracted to!

One comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s