In this post we are going to show you how to extend DSL Natural Language to suit your needs and to use your business language as the basis for rule modeling. Then we are going to implement and refactor the logic for calculating the salary of an employee based on the following components:
Let’s say we have the following logic for the salary calculation in Natural Language form:
If you are not familiar with Natural Language you can find it here. Here, this logic calculates the total amount based on employee Salary.
Now the issue with this logic is that it does not really describe the method of calculation and how it is achieved! Despite a couple of comments and a name for the logic of “Salary calculator”, we are expected to guess.
Business Friendly Statements
One of the main benefits of this domain specific language using Natural Language in modeling logic is that you can wrap complex or unclear statements into business friendly, meaningful expressions.
So for the above logic we can add a different section describing when to call this to make it more meaningful. What we need to do for this calculation is to:
- Add base salary
- Add bonus
- Add commission
At this stage, your comments are all gone. You don’t need them because the logic is self-descriptive. And more importantly, it can be reused.
Let’s assume those 10% and 30% bonus and commissions are variables, so we need to change them based on different scenarios. So in that case our new logic should accept a parameter. In the logic of Salary calculation, therefore, we should pass values for those parameters, as follows:
Now it is apparent that this logic can handle different variables of commissions and bonus.
There are many benefits to using this approach:
- No comments are required – business rules are self-descriptive
- You’ve made a reusable, parameterised business rule
- You can test and debug each aspect of this logic independently
- And more importantly – you modelled an executable business rule that everyone understands in their own spoken language
Using this method, you can model business rules that not only document your business, but also are executable. You can run, test and deploy these to your production environment. DSL allows you to build executable business rules that are modeled based on your business language. Now your business logic (i.e., rules, decisions, etc.) implementation is not separate from its documentation and specifications.