It is inevitable that domain objects in your software project will contain at least one status field. Quite often, however, a domain object may have multiple status fields with multiple values. Each status field and value adds another permutation to the total number of status combinations available in your software. How many of those different combinations does the business-side really care about?
If you find yourself working in a project that has many status fields, it can be quite maddening. New bugs and regression can occur when the meaning and/or business rules change around those status. A project that I am working on has just such an instance. We have about 12 different fields that are checked and rechecked to verify if a user can perform different actions. I recently broke one of the actions due to a simple change that had ripple effect.
One potential solution is to consolidate all of the rules regarding statuses into one place. Whether it be a single method that accepts an “Action” enum value; or a class with methods to validate each action. This style is a permissions with context approach. It differs slightly from a simple permission-based approach. In the latter approach, a user is simply assigned a static permission; usually in one field or bundled into a role. In the context approach, several fields are evaluated in real-time to determine if the user has permission to perform the action.
In our current project, these status checks are sprinkled all over the codebase. We are attempting to consolidate them into a single class with a method for each action. This does not reduce the complexity or number of statuses in our system, but it will consolidate the rules into one place. It will be tested and verified to work and should help eliminate more regression bugs.
So when you are tempted to add another status field or even just a status value, think twice. Think about the number of new test cases needed for the new combinations. Think about the test setup and think about your sanity.
I’m curious how many status-like fields do you check for any action. Let me know in the comments.