This problem is for a piece of software that is use by companies that organise activities. When an activity is changed (status, starting time, number of persons), some people need to be notified of this (by e-mail). The code that is responsible for this started out very simple: the body of the e-mail contained all the old values and new values of the information that was changed.
Over the years, many small rules were introduced. For example: if the new status is ‘cancelled’, the subject of the mail should be: “Activity cancelled” instead of “Activity changed”. If there is no previous status (so the activity is new), and the current status is “final”, then the subject should be “New Activity on [date]” and the body should contain a full overview (so no changes).
These rules above just serve to illustrate the problem. There are quite a few more of them (concerning combinations of status/date/time/etc.), totalling up to about 500 lines of code.
The problem that occurs is that this code is currently quite hard to understand and maintain. New rules are introduced every now and then, and adding them without breaking other rules can be a pain. What would be the best way to rewrite code like this into more understandable and maintainable code? Currently, the order of the if-else branches is also really important. The first if statement is the most important, the next else-if statement is bit less important, until the final else clause for the most general case.
Sounds like something a rules engine could help with. Drools is one option, but it’s almost certainly complete overkill!
Do you have an interface representing a rule? Something like
Given this, you could implement a reduce function to apply the rules in order until you reach the end of the list.